Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble sending Bundle with PendingIntent to a Broadcast Receiver, data lost

I am adding some basic alarm functionality to my program via the use of AlarmManager and a BroadcastReceiver class (named AReceiver.java). My problem is that the data I add to the bundle attached to the Intent creating the PendingIntent appears to be lost. The only bundle data I can access in the AReceiver class is a android.intent.extra.ALARM_COUNT=1.

Here is the basic code in the main activity class creating the Intent, PendingIntent and the AlarmManager: [Code in main activity - Notepadv3]

Intent intent = new Intent(Notepadv3.this, AReceiver.class);         
intent.putExtra("teststring","hello, passed string in Extra");               
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);     
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);           
am.set(AlarmManager.RTC_WAKEUP, timeOfNextPeriod.getTimeInMillis(), alarmIntent);

[Code in the BroadcastReceiver - AReceiver]

public void onReceive(Context con, Intent arg1) {
Bundle extrasBundle = arg1.getExtras();
Log.d("broadcast","contains teststring = " + extrasBundle.containsKey("teststring"));
Log.d("broadcast","is empty? = " + extrasBundle.isEmpty());
Log.d("broadcast","to string = " + extrasBundle.toString());
    }   

Debug messages say that contains teststring is FALSE, is empty is FALSE and when outputting the whole bundle, I get the android.intent.extra.ALARM_COUNT=1 value.

Any help would be greatly appreciated.

Cheers, Tom

like image 661
Tom Gustafson Avatar asked Sep 16 '10 17:09

Tom Gustafson


People also ask

What is PendingIntent FLAG_ IMMUTABLE?

Flag indicating that the created PendingIntent should be mutable. This flag cannot be combined with FLAG_IMMUTABLE . Up until Build. VERSION_CODES. R , PendingIntents are assumed to be mutable by default, unless FLAG_IMMUTABLE is set.

What is intent and Pending intent?

PendingIntent is a wrapper of Intent . The foreign app that receives the PendingIntent , doesn't know the content of Intent which is wrapped by PendingIntent . The mission of foreign app is to send back the intent to owner when some conditions are met (For example: alarm with schedule, or notification with click...).

How do I send data from BroadcastReceiver to activity?

Step 1. Open your project where you want to implement this. Step 2. Open your BroadcastReceiver class from where you pass data to activity inside your onReceive() you need to start intent and pass data inside intent and start sendBroadcast() as shown bellow.


1 Answers

You have to change this line

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);

into this

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

otherwise the data is lost

like image 94
Add Avatar answered Oct 13 '22 00:10

Add