Android Pendingintent Extras, Not Received by Broadcastreceiver

Broadcast receiver not receiving extras

Ah. You can't put custom serializable objects as "extras" in an Intent that you send to AlarmManager. You can only put know types in there. If you need a custom type then you will need to serialize the custom object into a byte array and add that to your Intent as an "extra".

Android PendingIntent extras, not received by BroadcastReceiver

I guess there are some issues with registering and unregistering in the code. Instead I registered the two BroadcastReceivers in the AndroidManifest.xml file and was able to successfully pass extras.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myexample" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.WRITE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:debuggable="true" android:icon="@drawable/ic_launcher_icon"
android:label="@string/app_name">
<activity //Main activity...
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity //Activity 2 ... </activity>//More acitivies ...

// Send Receiver
<receiver android:name="com.sendit.receivers.SendBroadcastReceiver">
<intent-filter>
<action android:name="SMS_SENT" />
</intent-filter>
</receiver>
//Delivery Receiver
<receiver android:name="com.sendit.receivers.DeliveryBroadcastReceiver">
<intent-filter>
<action android:name="SMS_DELIVERED" />
</intent-filter>
</receiver>
</application>
</manifest>

SendBroadcastReceiver.java

public class SendBroadcastReceiver extends BroadcastReceiver {
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private static final String ACTION_SMS_SENT = "SMS_SENT";

// When the SMS has been sent
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (action.equals(ACTION_SMS_SENT)) {

switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();
Bundle b = intent.getExtras();
Log.d(DEBUG_TAG, "sendBroadcastReceiver : b is " + b);
if (b != null) {
String value = b.getString("extra_key");
Log.d(DEBUG_TAG, "sendBroadcastReceiver : value is " + value);
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "No service", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
break;
}

}
}

}

DeliveryBroadcastReceiver.java

public class DeliveryBroadcastReceiver extends BroadcastReceiver {
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private static final String ACTION_SMS_DELIVERED = "SMS_DELIVERED";

// When the SMS has been delivered
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (action.equals(ACTION_SMS_DELIVERED)) {

switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}

}

MainActivity.java

public class MainActivity extends Activity {
private static String ACTION_SMS_SENT = "SMS_SENT";
private static String ACTION_SMS_DELIVERED = "SMS_DELIVERED";
private static int MAX_SMS_MESSAGE_LENGTH = 160;

private static Context mContext;
private final String DEBUG_TAG = getClass().getSimpleName().toString();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mContext = getApplicationContext();

setContentView(R.layout.main);
// ... Bind views and set up onClick listeners ( for example, one to call sendSMS() )

}

// ---sends an SMS message to another device---
public static void sendSMS(String phoneNumber, String message) {
Intent sendIntent = new Intent(ACTION_SMS_SENT);
sendIntent.putExtra("extra_key", "extra_value"));

PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_DELIVERED), 0);
SmsManager smsManager = SmsManager.getDefault();

int length = message.length();
if (length > MAX_SMS_MESSAGE_LENGTH) {
ArrayList < String > messagelist = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
} else
smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
}
}

//More methods of MainActivity ...

}

Broadcast Receiver not receiving intent (RemoteViews / PendingIntent)

I am posting the answer so it will help others as well. I was using implicit intent for broadcast receiver which Android Oreo does not suggest. So, I have to use explicit intent and after that it started working fine.

Manifest.xml:

<receiver android:name="com.appedia.flashlight.torch.Notification_Receiver" />

MainActivity.java:

public void NotificationItemsClickIntent()
{
Intent notification_intent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notification_intent, 0);

Intent notification_deleted_intent = new Intent(this, Notification_Receiver.class);
notification_deleted_intent.putExtra("notification_delete", "notification_delete");
delete_pending_intent = PendingIntent.getBroadcast(this, 1, notification_deleted_intent, PendingIntent.FLAG_UPDATE_CURRENT);

/* Pending Intent for Notification Flashlight Button */
//Intent with flashlight button for triggering Broadcast Receiver when clicked from notification bar
Intent notification_flashlight_intent = new Intent(this, Notification_Receiver.class);
notification_flashlight_intent.putExtra("notification_flashlight", "Flashlight");
PendingIntent notification_flashlight_button = PendingIntent.getBroadcast(this,2,notification_flashlight_intent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.notification_flashlight_button, notification_flashlight_button);

/* Pending Intent for Notification SOS Button */
//Intent with sos button for triggering Broadcast Receiver when clicked from notification bar
Intent notification_sos_intent = new Intent(this, Notification_Receiver.class);
notification_sos_intent.putExtra("notification_sos", "SOS");
PendingIntent notification_sos_button = PendingIntent.getBroadcast(this,3,notification_sos_intent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.notification_sos_button, notification_sos_button);
}

BroadcastReceiver Intent delivered without extras

After @ShlomiKatriel commented above and pointed me to a similar question, I have come to a solution.

The reason this happens is because Intent does not know how to serialize and deserialize every type of object.

In my case, the problematic line was:

intent.putExtra(ARG_REMINDER_TYPE, type)

Because the type that I was trying to pass as extra was actually an enum. What makes this very confusing is that if a single serialization fails, it loses ALL of the data, not just the one that failed. That's why my ARG_DATE_TIME was being lost as well.

After I've replaced the enum extra with a primitive type, it started to work as expected and I could access my other extras as well.

intent.putExtra() in pending intent not working

Try this

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);

PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
id,
aint,
// as stated in the comments, this flag is important!
PendingIntent.FLAG_UPDATE_CURRENT);

I could not get data from Intent after broadcast received

Change your sendSMS code into:

public void sendSMS(final String message, final String tele) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

SMSStatus sentStatus = new SMSStatus();
SMSDelivery deliveryStatus = new SMSDelivery();
//broadcast these during send and delivery
PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this, 0, new Intent(SENT).putExtra("telephone", tele), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.this, 0, new Intent(DELIVERED).putExtra("telephone",tele), PendingIntent.FLAG_UPDATE_CURRENT);

registerReceiver(sentStatus, new IntentFilter(SENT));
registerReceiver(deliveryStatus, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(tele, null, message, sentPI, deliveredPI);
}


Related Topics



Leave a reply



Submit