Broadcast Receiver And Pending Intent : Show A Toast
The following is the code for an alarm that has to hit the BroadCast Receiver : Intent intentWithData = new Intent(context, TokenActivity.class); intentWithData.putExtra(Constants
Solution 1:
You're mixing 2 things. If you want your receiver to get the intent:
IntentintentWithData=newIntent(context, TokenBroadcastReceiver.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, 7, intentWithData, 0);
AlarmManageralarmManager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
if you want your activity to get the intent:
IntentintentWithData=newIntent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntentpendingIntent= PendingIntent.getActivity(context, 7, intentWithData, 0);
AlarmManageralarmManager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
Plus, make sure your receiver is registered in your AndroidManifest.xml
Solution 2:
You are setting the pending intent to open an activity as per your code
Intent intentWithData = newIntent(context, TokenActivity.class);
and displaying the toast in broadcast receiver. Please correct your code and it will start working.
IntentintentWithData=newIntent(this, TokenBroadcastReceiver.class);
intentWithData.putExtra("id",5);
intentWithData.putExtra("position", 4);
PendingIntentpendingIntent= PendingIntent.getBroadcast(this, 007, intentWithData, 0);
AlarmManageralarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
Don't forget to register your broadcast in manifest
<receiverandroid:name=".broadcastReceiver.TokenBroadcastReceiver"/>
Post a Comment for "Broadcast Receiver And Pending Intent : Show A Toast"