Intent extras are empty in onReceive of BroadcastReceiver

Viewed 1739

I want to get some arguments from Intent in onReceive() method of BroadcastReceiver class. But there's only int ALARM_COUNT = 1, although I put two args: my Parcelable Alarm object and test int (for the case when there is some problem with alarm object).

I set alarm like this:

private void setCurrentAlarm(Alarm alarm) {
        long time = System.currentTimeMillis() + getClosestTimeDifference(alarm);

        mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, createPendingIntent(alarm));
    }

There is how I create PendingIntent variable:

private PendingIntent createPendingIntent(@NonNull Alarm alarm) {
        Intent intent = new Intent(mContext, AlarmBroadcastReceiver.class);

        intent.putExtra(KEY_ALARM, alarm);
        intent.putExtra("TEST", 1010120012);

        return PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

And onReceive() method in my AlarmBroadcaseReceiver class:

    @Override
    public void onReceive(Context context, Intent intent) {
           Alarm receivedAlarm = intent.getParcelableExtra(KEY_ALARM); //is always null
           int receivedInt = intent.getIntExtra("TEST", -1); //the same empty, -1
    }

As you can see Intent contains only some ALARM_COUNT extra, but there aren't my extras.

What to do? How can I get them?

1 Answers

Using flags as shown in the examples below was helpful for me.

retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_IMMUTABLE)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_ONE_SHOT)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
Related