Android cannot pass intent extras though AlarmManager

前端 未结 2 796
轮回少年
轮回少年 2020-11-27 15:49

I am trying to put an extra message in my intent to pass to AlarmManager to be triggered at a later time. My onReceive triggers correctly but extras.getString() returns null

2条回答
  •  生来不讨喜
    2020-11-27 16:09

    UPDATE: Please see Vincent Hiribarren's solution


    Old Answer... Haresh's code is not the complete answer... I used a Bundle and I tried without Bundle but I got null's either way when I attempting to obtain the strings from the extra's !!

    The exact problem, in your code, is with the PendingIntent !

    This is wrong if you're trying to pass extra's :

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0);
    

    Because the 0 for the flags is what will cause you a headache

    This is the right way to do it - specify a flag !

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    This is probably such a popular problem because Google's sample code neglected to include Extra's in an Alarm.

提交回复
热议问题