user2679041
user2679041

Reputation: 251

android open battery settings programically

I'm looking for a way to open the battery settings screen from an android app.

So far I found the two intents :

Intent.ACTION_POWER_USAGE_SUMMARY

Settings.ACTION_BATTERY_SAVER_SETTINGS

but none of them open this screen.

I was wondering if anyone knows of such a way. It sounds strange that an intent for something so simple doesn't exist

Upvotes: 8

Views: 11778

Answers (3)

Dmitry Grushin
Dmitry Grushin

Reputation: 851

For Xioami (Redmi Note 12) the following works:

Intent intent = new Intent();
intent.setComponent(new ComponentName(
           "com.miui.powerkeeper",
           "com.miui.powerkeeper.ui.HiddenAppsContainerManagementActivity"));
startActivity(intent);

Upvotes: 1

Alon Minski
Alon Minski

Reputation: 1581

I know this is quite old. But a trick I use is going to the appropriate settings screen in the device settings and then while connected to the phone run:

adb shell
dumpsys window windows | grep -E 'mCurrentFocus'

This returns the package name and Activity name currently in focus. Using that I can check in code if the intent is callable. If it is, I launch it. If it isnt, I might have better luck with a different screen that is near by or explain to the user he needs to do something manually etc... Obviously the more devices you have, the more Intents you can create and check at run time. Im sure there is a list of Intents for different devices online.

Upvotes: 3

schlenger
schlenger

Reputation: 1557

Settings.ACTION_BATTERY_SAVER_SETTINGS on "plain" Android versions will show the settings page you want to show.

Intent.ACTION_POWER_USAGE_SUMMARY will lead to the overview page showing the battery consumption.

Some manufactures such as Samsung build their own implementation over the system one, e.g. in this the "Battery" page. On Samsung devices, you can call this by calling the SmartManager interface directly. An code example:

if (Build.MANUFACTURER == "samsung") {
    val intent = Intent()
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
        intent.component = ComponentName("com.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity")
    } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
        intent.component = ComponentName("com.samsung.android.sm", "com.samsung.android.sm.ui.battery.BatteryActivity")
    }

    try {
        activity?.startActivity(intent);
    } catch (ex: ActivityNotFoundException) {
        // Fallback to global settings
        startActivity(Intent(Settings.ACTION_SETTINGS))
    }
} else {
    startActivity(Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS))
}

It can be the case that you need additional cases for Huawei or Xiaomi as well.

Huawei can be "com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"...

...and the MIU based ones "com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"

Upvotes: 7

Related Questions