Android Question Doze / Standby

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the BleExtended Library and I am able to scan and connect to a Bluetooth device and get it's services etc.

When I get the service from the device I am then sending a SMS message.

All this is working and I am doing this from a service within my app so it can do this in the background while the app is not showing on the screen.

The problem I have now is that Android 6.0 has added a new feature called Doze. Doze will stop my app from running in the background while the screen is off and while the device it not moving.

Starting from Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users by managing how apps behave when a device is not connected to a power source. Doze reduces battery consumption by deferring background CPU and network activity for apps when the device is unused for long periods of time. App Standby defers background network activity for apps with which the user has not recently interacted.

http://developer.android.com/training/monitoring-device-state/doze-standby.html

However, I noticed there is a way to allow the app in a whitelist:

Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.

Would I be correct in saying that the above will allow the app to run in the background by enabling this feature?

If so, how can I do the following in my app so it prompts the user when they run the app ?

An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog.
 

DonManfred

Expert
Licensed User
Longtime User
Would I be correct in saying that the above will allow the app to run in the background by enabling this feature?
i would say yes

If so, how can I do the following in my app so it prompts the user when they run the app ?

I dont know but it may be possible wih javaobject and or reflection.

I found on Stackoverflow

B4X:
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

But i dont know how to convert this using reflection or javaobject
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thank you. I added that. Hopefully I won't be late for work tomorrow... (Doze mode is screwing up my alarms)
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Nope, didn't go off... If I wasn't using an old device as backup I'd have been late for work. Thanks Google...
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Just StartServiceAt, it worked fine for years till doze mode came around. Now the alarm goes off after I wake up and pick my phone up.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Nope, it's a flaw with Android:

https://plus.google.com/+AndroidDevelopers/posts/94jCkmG4jff

And secondly, you may have work to do that is time-sensitive. But even AlarmManager (http://goo.gl/HDqEwd) is not exempt from Doze mode. However, just because the alarm will not fire during Doze mode does not mean it is lost. For example, an alarm set with setExact() will actually not trigger while Dozing, but will fire as soon as Doze mode exits.

We found something you can't actually fix. ;_;
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Ah, maybe you can

you can rely on setExactAndAllowWhileIdle() to wake your app even during Doze mode. Check out our previous pro-tip (https://goo.gl/IENTID) on working with alarms and background work for more details.
 
Upvote 0
Top