8

I am sending a request code through this to an alarm manager

 Intent broadcast_intent = new Intent(this, AlarmBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, rowId,  broadcast_intent, PendingIntent.FLAG_UPDATE_CURRENT);

I was wondering, that in the broadcastreceiver, how can I retreive the requestcode (rowId) that I used to setup pendingIntent?

Thanks

4

3 回答 3

3
Intent broadcast_intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, rowId,
                              broadcast_intent,PendingIntent.FLAG_UPDATE_CURRENT
                              );

Best would be to pass the extras while referring broadcast_intent within getBroadcast() - broadcast_intent.putExtras("REQUESTCODE",rowId) ; as follows :

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, rowId,
                              broadcast_intent.putExtras("REQUESTCODE",rowId), 
                              PendingIntent.FLAG_UPDATE_CURRENT);
于 2014-04-06T10:16:55.253 回答
3

The requestCode used when creating a pendingIntent is not intended to pass on to the receiver, it is intended as a way for the app creating the pendingIntent to be able to manage multiple pendingIntents.

Suppose an alarm app needed to create several pendingIntents, and later needs to cancel or modify one of them. The requestCode is used to identify which one to cancel/modify.

To pass data on, use the putExtra as described above. Note you might very well want to use RowId for both the requestCode and the Extra data.

于 2016-04-04T16:44:58.650 回答
1

I was searching for the same thing. One way would be to pass requestcode as extra in your Intent.

intent.putExtra("requestcode", rowId);

However, if the app is killed there is no way to retrieve the data passed by the Intent.

So you need to pass rowId as an URI , and use Intent Filter.

于 2012-12-30T23:14:28.600 回答