A few years ago, I wrote a tiny application to apply some settings changes to my phone when the charging state changed.
To do this at the time, I registered a receiver
in the manifest:
<receiver android:name=".services.PowerStateReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
This receiver ran a few lines of code, and that was it. It worked perfectly.
Since API 26, this no longer works. I have to register the receiver at runtime, and it only seems to work while the app is open and for a while after it leaves foreground. I tried to switch methods, then tried using a Service
to register the receiver with START_STICKY
, to no avail.
How can I achieve the same effect with the current API restrictions?