Intent extras are empty in onReceive of BroadcastReceiver
Asked Answered
P

1

9

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?

Preliminary answered 6/11, 2017 at 20:41 Comment(1)
Hi Denis, i recommend this link https://mcmap.net/q/1321236/-is-it-right-way-to-set-alarm-at-every-30-seconds , it has all the information for you to understand how to use alarmManager.Claudiaclaudian
M
-1

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)
Magbie answered 7/6, 2020 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.