2

i have code that calls a broadcast receiver, in the receiver, i try to get the extra data from the intent, but it's null.

so,

     Intent intent = new Intent(MainActivity.this, CallAlarm.class);
                          intent.putExtra("medicine", "kkk");

                          PendingIntent sender=PendingIntent.getBroadcast(
                                  MainActivity.this,0, intent, 0);

                          AlarmManager am;
                          am = (AlarmManager)getSystemService(ALARM_SERVICE);
                          am.set(AlarmManager.RTC_WAKEUP,
                                 c.getTimeInMillis(),
                                 sender
                                );

and in the BroadcastReceiver.onReceiver(),

    String name = i.getStringExtra("medicine");

but medicine is null. what am i missing?

Jingyue Hu
  • 23
  • 3
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – Etienne Lawlor Jan 31 '13 at 07:51

2 Answers2

3

Just create the PendingIntent with flag PendingIntent.FLAG_UPDATE_CURRENT,

 Intent intent = new Intent(MainActivity.this, CallAlarm.class);
 intent.putExtra("medicine", "kkk");
 PendingIntent sender=PendingIntent.getBroadcast(
                                  MainActivity.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

This will enable you to pass the data to the BroadCastReceiver.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

I was trying the above code, I get value always null. My broadcast receiver starts on bootup so apart from the above code I also add intent.setAction("android.intent.action.BOOT_COMPLETED");

Still it gives me null.

user1810931
  • 638
  • 2
  • 11
  • 33