2

Even though I am setting the alarm and registering the broadcast receiver the alarm is not firing the intent.

I am registering the receiver here:-

private void registerOfflineEntitlementUpdateReceiver(Context context) {
    IntentFilter intentFilter = new IntentFilter(ENTITLEMENT_OFFLINE_ACTION);
    LocalBroadcastManager.getInstance(context).registerReceiver(new EntitlementOfflineUpdateReceiver() , intentFilter);
}

The alarm is set here:-

private void setAlarm() {
    Intent intent = new Intent(mContext, EntitlementOfflineUpdateReceiver.class);
    intent.setAction(ENTITLEMENT_OFFLINE_ACTION);
    mPending = PendingIntent.getBroadcast(mContext, Constants.ENTITLEMENT_OFFLINE_ALARM, intent, 0);

    mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
    mAlarmManager.cancel(mPending);

    Calendar cal = Calendar.getInstance();
    long cncNextPollTimeInMillis = ApplicationPrefs.getInstance(mContext).getNextCncHelloPoll();
    cal.setTimeInMillis(cncNextPollTimeInMillis);
    int a_SEC = 1000;
    int a_MIN = a_SEC * 60;
    cal.add(Calendar.MILLISECOND, a_MIN);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, cal.getTimeInMillis(), mPending);
    }
    else {
        mAlarmManager.set(AlarmManager.RTC,cal.getTimeInMillis(),mPending);
    }
}

The broadcast receiver class: -

public class EntitlementOfflineUpdateReceiver extends BroadcastReceiver {
    private String TAG = "SMC.EntitlementOfflineUpdateReceiver";
    @Override
    public void onReceive(final Context context, Intent intent) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "performing offline entitlement update ...");
                EntitlementEngine.getInstance(context).performOfflineEntitlementUpdate();
            }
        }).start();

    }
}

What is the actually error that I'm getting?

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
  • The code that is supposed to execute on the receiver is not being executed. I also dumped the alarm in a text file and found that the alarm did go off. +5ms 0 wakes 2 alarms, last -1h36m28s137ms: *alarm*:entitlement_offline_action – BharatProteemGogoi Aug 01 '18 at 09:00
  • I haven't touched Android for months, but I think this might have something to do with Doze. – Ricardo Costeira Aug 01 '18 at 09:07
  • that is why I used setExactAndAllowWhileIdle, it will take care of the doze mode. – BharatProteemGogoi Aug 01 '18 at 09:11
  • Try replacing `AlarmManager.RTC` with `AlarmManager.RTC_WAKEUP` when calling `mAlarmManager.setExactAndAllowWhileIdle()`. See here: https://developer.android.com/reference/android/app/AlarmManager.html#RTC_WAKEUP – Arseny Levin Aug 01 '18 at 11:36
  • it works if I pass an activity instead of a broadcastReceiver in the intent. – BharatProteemGogoi Aug 01 '18 at 12:17
  • Do you have a `` entry in the manifest for your `BroadcastReceiver`? – David Wasser Aug 10 '18 at 09:34
  • Starting a `Thread` in a `BroadcastReceiver` isn't a good architecture. if your app is not running when the alarm triggers, Android will create a new OS process for your app, instantiate the `BroadcastReceiver`, call `onReceive()` and then kill the OS process again. Your thread may or may not run to completion. You should use a `Service` instead. – David Wasser Aug 10 '18 at 09:37

0 Answers0