Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android M startActivity battery optimization

I'm developing an app that should alert an user if is near a place. and of course have to do that also if the phone is in idle. With DOZE now I understood that I have to whitelist my app, and to do that I saw that I can start an intent with the action request thanks to Buddy's answer in this post

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);

well this should be too easy...because google doesn't like this approach and if you do it, your app should be banned from the play store...no comment... Ok so the way should be to drive the user to the battery settings and manually add your app in the DOZE's white list...yes this should be a big wall to climb...anyway seems to be the only way...now the answer is: I Can use an intent to go to the power usage summary, in this way (thank you Chris):

Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
    ResolveInfo resolveInfo = getPackageManager().resolveActivity(powerUsageIntent, 0);
// check that the Battery app exists on this device
    if(resolveInfo != null){
        startActivity(powerUsageIntent);
    }  

But how to directly go at the list of app for choosing the battery optimization?

Thanks for any answer.

like image 704
Ast Avatar asked Jan 11 '17 16:01

Ast


People also ask

What does android battery OPTIMIZATION do?

Battery optimization helps conserve battery power on your device and is turned on by default. Devices running Android 6. x and higher include battery optimization features which improve battery life by placing apps in Doze mode or App Standby.

Whats adaptive battery?

True to its name, Adaptive Battery's main function is to learn your phone usage habits. As odd as it sounds, it takes into account what apps you use the most, how long you use them, and any other important information about how quickly your battery drains when not optimized.

What is adaptive battery in pixel 6a?

Adaptive charging uses a smart way to tackle this problem by keeping the battery at 80% for most of the night. This feature allows the battery to get fully charged just before the user wakes up to avoid the constant cycling between 99-100 per cent throughout the night.


1 Answers

To open list of apps for choosing battery optimization you can use this code sample:

private void openPowerSettings(Context context) {
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    context.startActivity(intent);
}

It doesn't need to have <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/> permission, so it should be ok to publish it to Google Play (for details please check this thread and comments to this question).

NOTE

Adding this line

intent.setData(Uri.parse("package:" + mContext.getPackageName()));

will cause crash "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }". User has to find the app in the list, it seems no way to jump directly to our app.

like image 91
Grimmy Avatar answered Sep 30 '22 23:09

Grimmy