简体   繁体   中英

Alarm Manager broadcast not received when app is not in memory (One Plus 3)

Alarm Manager broadcast is not received in some devices when app is not in memory ( swiped off from app-tray ) . Device in which it is not working runs with API 25 and is ONE PLUS 3 device. however same thing works in Nexus 6 (with API 25).

Strange thing is , I can see in system logs that Alarm is being triggered. I can see log below when alarm is supposed to go off. Just that broadCast is not received. again, this happens only when app is not in memory. Broadcast is received properly when app is in memory.

V/AlarmManager: Triggering alarm #0: 0 when =1508843147121 package=net.IntAppoperation =*walarm*:com.intapp.receivers.NOTIFICATION_ALARM

Here is how I'm setting alarm

In Manifest :

<receiver android:name="com.intapp.receivers.ReminderReceiver"
              android:exported="false"
              android:enabled="true">
        <intent-filter>
            <action android:name="com.intapp.receivers.NOTIFICATION_ALARM" />
        </intent-filter>
</receiver>

And then When I want to set alarm :

public static final String ACTION = "com.intapp.receivers.NOTIFICATION_ALARM";

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.setAction(ACTION);
int randomNum = new Random().nextInt(Integer.MAX_VALUE - 1 + 1);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, randomNum, intent, PendingIntent.FLAG_ONE_SHOT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
     alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent); // I get time from calendar instance in my function
  } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
            }
}

Here's my implementation of Receiver :

public class ReminderReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Receiver called ");
        // my implementation 
     }
}

EDIT:

Seems like problem is a little bigger. I have also registered for CALL_STATE by which I can listen to incoming/outgoing calls. That broadcast receiver is also not firing when app is not in memory. Plus, This happens on Samsung device too running on API 25. Is this (Manifest broadcast not working) known problem for API 25 on some devices?

I have an OnePlus 3 as well... And yes, this happens a lot to me as well. Even when I register for a BootReceiver . I fixed my problem by uninstalling and reinstalling my application.

I would suggest to implement a WakeLock in your Application , to prevent the device from killing your app, so the BroadcastReceiver remains active.

Let's hope OnePlus will do it better in the future.

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