我試圖通過PendingIntent
發送一些額外的數據,如:如何通過PendingIntent將數據發送到廣播?
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);
然後在Broadcast
試圖趕上數據:
@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....
}
我看到Broadcast
onReceive()
叫 - 這意味着Broadcast
註冊以適當的方式,但仍然額外是空的。
任何想法?
我在這裏做過類似的事! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – toobsco42