简体   繁体   中英

Intent extras are always null

Im making an app that works with an AlarmManager. I use a BroadcastReceiver to get when the alarm is received. But firstly, I call the method setAlarms() in my Main Activity. I have checked with breakpoints and the values that I'm passing to the broadcastreceiver are not null, but when I try to get them in the BroadcastReceiver itself, it returns null. This is my code:

//MainActivity
 public void SetAlarms()
    {
        ArrayList<TaskItem> dates = TaskUtils.getInstance().getDatedTasksInMillis(this);
        ArrayList<TaskItem> repeating = TaskUtils.getInstance().getRepeatingTasksInMillis(this);

        Intent intent;
        PendingIntent pi;
        AlarmManager manager;

        for (int i = 0; i < dates.size(); i++)
        {
            TaskItem current = dates.get(i); //TASK ITEM IS THE CLASS WHERE I GET THE VALUES
            long millis = current.getDateInMillis();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                intent = new Intent(getApplicationContext(), NotificationReceiver.class);

                Bundle b = new Bundle();
                b.putInt("ID", current.getID());
                b.putString("Title", current.getDescription());
                b.putString("Description", current.getDescription());
                b.putInt("Tag", current.getTag());

                intent.putExtras(b);

                long minus = 1000 * 60 * current.getRemind();
                millis -= minus;

                pi = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                manager = (AlarmManager)getSystemService(ALARM_SERVICE);
                manager.setExact(AlarmManager.RTC_WAKEUP, millis, pi);
            }
      }
}

//BroascastReceiver Class
public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        //THIS METHOD ACTIVATES WHEN THE ALARMMANAGER GETS TO CERTAIN TIME

        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Bundle b = i.getExtras();

        int id = b.getInt("ID");
        String title = b.getString("Title");
        String description = b.getString("Description");
        int tag = b.getInt("Tag");

        //BUILDING NOTIFICATION
        NotificationHelper helper = new NotificationHelper(context);
        NotificationCompat.Builder nb = helper.getTaskChannelNotification(id, title, description, tag);
        int RandomNumber = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        helper.getManager().notify(RandomNumber, nb.build());
    }

In the NotificationReceiver you are trying to read the extras from a newly created intend instead of the received as a parameter in the method onReceive.

Instead of i.getExtras() it should be intent.getExtras()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM