Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data through PendingIntent to Broadcast?

I'm trying to send via PendingIntent some extra data, like:

MyMessage message;
//...
Intent intent;
SmsManager sms = SmsManager.getDefault();
intent = new Intent(Constants.SENT_PLAIN);
intent.putExtra(Constants.EXTRA_RAW_ID, message.getId()); //putting long id (not -1L)
PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent, 0);
intent = new Intent(Constants.DELIVERED_PLAIN);
intent.putExtra(Constants.EXTRA_RAW_ID, message.getId());
PendingIntent deliveredPI = PendingIntent.getBroadcast(activity, 0, intent, 0);
sms.sendTextMessage(phoneNumber, null, message.getBody(), sentPI, deliveredPI);

Then in Broadcast trying to catch data:

@Override
public void onReceive(Context context, Intent intent) {
    String message, prefix = "";
    String action = intent.getAction();
    long id = intent.getLongExtra(Constants.EXTRA_RAW_ID, -1L);  //here I receive id=-1

    // blah-blah.... 
}

I see that Broadcast onReceive() called - which means that Broadcast registered in a proper way, but still extras are empty.

Any ideas?

like image 336
Barmaley Avatar asked Jul 19 '11 18:07

Barmaley


People also ask

How do I send data from BroadcastReceiver to activity?

Intent intent = getIntent(); String message = intent. getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.

Why would you use a PendingIntent?

By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity).

What is broadcast receiver in Android Studio?

Definition. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


2 Answers

Put data in intent you are using in pending intent as Extras. You will get this intent in onReceive Method of BroadCast receiver. Try to define Pending intent as below.

PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
like image 176
om252345 Avatar answered Sep 22 '22 12:09

om252345


as said on pending intent :

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

There are two typical ways to deal with this.

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent\[\], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT to either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.

like image 43
FxRi4 Avatar answered Sep 21 '22 12:09

FxRi4