Android 8.0 Oreo AlarmManager with broadcast receiver and implicit broadcast ban

I have critical reminders that are configured through the Alarm Manager (it should work just like the alarm application).

I used to have the following in Android Manifest:

<receiver android:name="com.example.app.AlarmReceiver" > <intent-filter> <action android:name="${packageName}.alarm.action.trigger"/> </intent-filter> </receiver> 

Broadcast Receiver:

 public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive( final Context context, final Intent intent) { // WAKE LOCK // BUILD NOTIFICATION etc... } } 

How to set the alarm:

 final PendingIntent operation = PendingIntent.getBroadcast( mContext, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); if (PlatformUtils.hasMarshmallow()) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation); } else { alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation); } } 

In Android 8.0, I can no longer use implicit broadcasting as defined in the manifest. In this case, the alternative is to manually register as follows:

 final BroadcastReceiver receiver = new AlarmReceiver(); final IntentFilter intentFilter = new IntentFilter(ALARM_RECEIVER_INTENT_TRIGGER); context.registerReceiver(receiver, intentFilter); 

This does not seem logical to me.

  • The alarm receiver will be bound to the context lifetime. This causes a problem when they say that the application is killed due to memory pressure or when the device restarts. I need my alarms to light up every time, as they are critical to the user's health.

  • Even if I listen to “android.intent.action.BOOT_COMPLETED” and register the alarm receiver, the application will be killed shortly after that and no alarm will be launched. I also do not see my alarm through

    adb shell dumpsys alarm

How to create a custom broadcast receiver that receives implicit broadcasts to give an alarm when setting up Android O (8.0)? Can someone enlighten me with a code example or a link to the documentation. How does the Timely app or any other alarm app work when targeting O?

+5
source share
2 answers

Modify your code a bit to make the broadcast explicit and not hidden, and you'll be fine (if this is an Activity link or some other Context ):

 Intent intent = new Intent(ALARM_RECEIVER_INTENT_TRIGGER); intent.setClass(this, AlarmReceiver.class); 

The limitation in Oreo is the implicit broadcast registration of Intent , that is, that you send it on air, only the action, category or data will be indicated. You make this an explicit translation by specifying the class that should receive the translation.

+12
source

If you guys are checking to see if the alarm has already been registered, be sure to do the same with this check:

 public boolean isAlarmBroadcastRegistered(Context context, String action, Class clazz) { Intent intent = new Intent(action); intent.setClass(context, clazz); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null; } 
0
source

Source: https://habr.com/ru/post/1271946/


All Articles