Snake
Snake

Reputation: 14668

Retrieve requestcode from alarm broadcastReceiver

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

Upvotes: 8

Views: 6873

Answers (3)

John Price
John Price

Reputation: 186

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.

Upvotes: 4

DoOrDoNot
DoOrDoNot

Reputation: 1156

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);

Upvotes: 3

Raymond Chenon
Raymond Chenon

Reputation: 12712

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.

Upvotes: 1

Related Questions