This question is related to the Android 6.0 Preview 3 which will be final released at the end of this month.
I'm testing some stuff in Android 6.0 in the preview 3 from Google on Nexus 5 'hammerhead'.
The new feature is the "doze mode" - something like deep sleep mode when the network is disabled and phone sleeps, only the SMS, calls or high priority GCM messages can wake it up. But like WhatsApp - in the doze mode it receives the messages after 2 hours or more depends on the timers. But there is a list of 'not optimised' apps called "white list" where u can manually add app.
Ok, I'd like to find a way to add my application programmatically without user interaction to the "white list app list" which exists in the device in battery settings.
Trying to use the reflection to get into it I found:
Within the android.os.IDeviceIdleController there is a method:
public abstract void addPowerSaveWhitelistApp (String packageNameOfApp)
But this is an interface... So we can not make an instance of interface.
There is not yet documentation about this Interface or about methods, or any inheritance tree.
Maybe you have some idea where i should look for a possibility of programmatically add there my app?
There is also a method
public abstract boolean isPowerSaveWhitelistApp (String packageName)
Which i think should be possible to access somehow?! to check if the app exist on the White List and maybe at the very end hopefully ASK user to add it to the White List.
So my question is, have anyone of you tried to make something with better result ?? cuz I'm stuck and i think its a dead end.
for more info: https://newcircle.com/s/post/1739/2015/06/12/diving-into-android-m-doze
Add permission
request whitelist your app
As far as I know , you cannot whitelist yourself from Doze. Ignore battery optimizations does not disable doze. See here: https://code.google.com/p/android-developer-preview/issues/detail?id=2225 UPDATE: In the release build of M , you can request ignore battery optimizations which will at least give you normal access to the internet while in Doze mode.
If we have root permissions,then whitelisting can be done programatically. To add device in whitelisted applications,run following command from application:
To remove device from whitelisted applications,run following command from application:
It is not possible to disable battery optimizations (=whitelist application for doze mode) without user interaction on Android M preview 3.
It can be done with user interaction this way:
I think this helper class should cover all your needs.
To use it to request the OS to white-list your app, you can use prepareIntentForWhiteListingOfBatteryOptimization. If you get null, it means you don't need it, or that you can't use it. You can use the other function to query a better state of what you are in.
UPDATE
When the device is stationary again, with screen off and on battery for a period of time, Doze applies the full CPU and network restrictions on PowerManager.WakeLock, AlarmManager alarms, and GPS/Wi-Fi scans.
Visit Use Cases for Whitelisting for more detail.
android n developer says
Doze is particularly likely to affect activities that AlarmManager alarms and timers manage, because alarms in Android 5.1 (API level 22) or lower do not fire when the system is in Doze.
Android 6.0 (API level 23) introduces two new AlarmManager methods:
setAndAllowWhileIdle()
andsetExactAndAllowWhileIdle()
. With these methods, you can set alarms that will fire even if the device is in Doze.Note: Neither
setAndAllowWhileIdle()
norsetExactAndAllowWhileIdle()
can fire alarms more than once per 15 minutes per app.Testing with Doze and App Standby