1

I have the following code for a one off alarm...

        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
            Bundle bun = new Bundle();
            bun.putString("data", "hello this is my message...");
            intent.putExtras(bun);
            PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this,
                    0, intent, 0);

            // We want the alarm to go off 5 seconds from now. TODO
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
            return false;
        }
    });
}



public class OneShotAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO
    Toast.makeText(context, "Alarm! " + intent.getExtras().getString("data"), Toast.LENGTH_SHORT).show();
}

}

The alarm correctly goes off, but the "data" extra isn't getting retrieved for some reason, and is being set to null.

Thanks for the help!

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
Jonny
  • 97
  • 2
  • 6

3 Answers3

1

Try:

Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
intent.putExtra("data", "hello this is my message...");
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
0

Possible duplicate of getExtra from Intent launched from a pendingIntent

If that doesn't help, please tell us a bit more about your files (you need to have an activity where you set up the alarm, a broadcastreceiver that starts a service, and the service where you tell the app what to do when the alarm goes off)

Community
  • 1
  • 1
erdomester
  • 11,789
  • 32
  • 132
  • 234
0

Citation from the doc for putExtra function:

name The name of the extra data, with package prefix.

Couldn't it be the reason?

Gangnus
  • 24,044
  • 16
  • 90
  • 149