Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast charger disconnected in Android O

Now that the final API for Android O is released and none of the following broadcasts is whitelisted I have the following problem:

In my application (targets API 25) I currently have a BroadcastReceiver which listens for system events of ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED. Now I would like to update my app to target Android O but with this release comes a huge change in broadcast behaviors:

Apps that target Android O can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically.

Since both broadcasts are implicit I can only register for them via the Context.registerReceiver() method but with this comes the problem: As soon as the process of my app is killed by the system or as soon as system clears my app's memory (as a result of low-memory of device) the broadcast registration will be lost.

To avoid this problem I can use the JobScheduler API with the setRequiresCharging method for ACTION_POWER_CONNECTED but for ACTION_POWER_DISCONNECTED I have to use the registerReceiver method.

Since my app controls the volume of the device (based on these events) it's really important that none of these events is missed. So how can I safely listen for disconnected power events in Android O?

Btw. I have the same problem with WIFI disconnect events.

EDIT: I would love to do this without a notification from a foreground service

like image 265
Cilenco Avatar asked Jun 11 '17 20:06

Cilenco


People also ask

What is the broadcast receiver in Android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

Does broadcast receiver work in background?

ACTION_BOND_STATE_CHANGED)); The alternative is to register the receiver in the AndroidManifest. This ensures that the BroadcastReceiver runs all the time, even when the app is backgrounded. I chose this option because I want my application to receive Bluetooth events even when the app isn't in use (i.e. backgrounded).

Where do I register and unregister broadcast receiver?

Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context. registerReceiver() and Context. unregisterReceiver() methods.

What is implicit broadcast in Android?

An implicit broadcast is one that does not target your application specifically so it is not exclusive to your application. To register for one, you need to use an IntentFilter and declare it in your manifest.


1 Answers

Jobs can be delayed without any constraint by the system. The setOverrideDeadline method uses just a best-effort policy, so in this case you can keep your target SDK to 25 or use a foreground service. Foregournd services can be killed by the system but with really low probability.

like image 143
greywolf82 Avatar answered Oct 01 '22 17:10

greywolf82