3

I have following method which is saving three values into intent which is appended to created alarm.

public static Boolean setUniqueAlarm(Long alarmId, String occurenceTime, Context context) {
        Boolean saveResult = null;
        try {
            DateTime dt = TimeHelper.getDateTimeObject(occurenceTime);
            Logger.d("Year: " + dt.getYear() + ", month: " + dt.getYear() + ", day: " + dt.getDayOfMonth() + ", hour: " + dt.getHourOfDay() + ", minute: " + dt.getMinuteOfHour());
            Logger.d("Occurrence time to save: "+occurenceTime);
            AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
            Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);
            // Pass the intent type and other additional values into bundle
            Bundle bundle = new Bundle();
            bundle.putString(Constants.Global.ALARM_TYPE, Constants.Global.ALARM_TYPE_UNIQUE);
            bundle.putString(Constants.Global.ALARM_OCCURRENCE_TIME, "123456");
            bundle.putLong(Constants.Global.ALARM_UNIQUE_ID, alarmId);
            alarmIntent.putExtras(bundle);
            PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
            alarmManager.set(AlarmManager.RTC_WAKEUP, dt.getMillis(), pendingAlarmIntent);
            saveResult = true;
        } catch (Exception e) {
            Logger.e(e.getMessage());
            saveResult = false;
        }
        return saveResult;
    }

In receiver i have following code:

@Override
    public void onReceive(Context context, Intent intent) {
        try {
            Logger.d("ALARM RECEIVED!!!");
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            // Device battery life will be significantly affected by the use of this API.
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
            // Acquire the lock
            wl.acquire();
            //Release the lock
            wl.release();
            // Get the intent type (or stored variables)
            Bundle extras = intent.getExtras();
            mAlarmType = extras.getString(Constants.Global.ALARM_TYPE);
            mAlarmId = extras.getLong(Constants.Global.ALARM_UNIQUE_ID, 0);
            mOccurenceTime = extras.getString(Constants.Global.ALARM_OCCURRENCE_TIME, "nothing");
            triggerActionBasedOnTheAlarmType(mAlarmType, mAlarmId, mOccurenceTime, context);
        } catch (Exception e) {
            TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
                    Constants.ExceptionMessage.EXC_CANNOT_PROCESS_RECEIVED_ALARM, true);
        }
    }

Problem is that variable mOccurenceTime is always null, rsp. "nothing".

I tried to get values from intent and from Bundle but still without the success. Is not bundle count of items limited?

How can i get value mOccurenceTime in the right way?

Many thanks for any advice.

redrom
  • 11,502
  • 31
  • 157
  • 264
  • did you assign to ALARM_OCCURRENCE_TIME an unique value ? – Blackbelt Jan 06 '16 at 17:03
  • Yes, it is hardcoded Constant (key), but value can be different or same (based on variable value) – redrom Jan 06 '16 at 17:05
  • are the keys you use to put the values in the bundle all different? (in particular is `Constants.Global.ALARM_UNIQUE_ID` different from `Constants.Global.ALARM_OCCURRENCE_TIME`) – njzk2 Jan 06 '16 at 17:16
  • Yes, but with same "prefix" alarm_unique_id AND alarm_exectime – redrom Jan 06 '16 at 17:20
  • just to make sure: in the documentation ( Intent.putExtras() ) it says about the Bundle: "The keys must include a package prefix". Do your keys start with the package name? – Bö macht Blau Jan 06 '16 at 18:28
  • Some example of package prefix? I dont know what is the "package prefix". – redrom Jan 06 '16 at 20:23
  • sorry for the delay, package prefix would be something like "com.example.myapp", just like the "package" value in the Manifest. Prefix because only after that you would append your "normal" constant values. – Bö macht Blau Jan 07 '16 at 18:24

1 Answers1

0

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

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

Instead of the:

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

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

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

Solved here:

Android cannot pass intent extras though AlarmManager

Community
  • 1
  • 1
redrom
  • 11,502
  • 31
  • 157
  • 264