Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 701
Snake Avatar asked Sep 20 '12 05:09

Snake


2 Answers

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);
like image 83
DoOrDoNot Avatar answered Sep 20 '22 09:09

DoOrDoNot


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.

like image 34
John Price Avatar answered Sep 22 '22 09:09

John Price