7

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

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
user2679041
  • 241
  • 2
  • 13
  • check this https://stackoverflow.com/questions/19999833/how-to-open-battery-use-in-about-device-part-of-settings-programatically-in-andr – Sanjun Dev Jan 16 '18 at 12:39
  • `Intent.ACTION_POWER_USAGE_SUMMARY` opens the battery overview in android settings. Isn't it that you was looking for ? – Diego D. Jan 18 '18 at 08:31
  • 2
    Hi, in some devices like pixel it works, but in samsung s7 for example when sending the intent, instead of getting the battery screen, I'm getting the power usage screen – user2679041 Jan 21 '18 at 08:16
  • @user2679041 If the answer underneath answers your question, it would be nice to mark it. – schlenger Oct 12 '19 at 18:41
  • @user2679041 Hi, Did you find any workaround of it? I'm facing the same problem. Thanks – Muhammad Farhan Dec 16 '19 at 08:17

3 Answers3

6

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"

schlenger
  • 1,447
  • 1
  • 19
  • 40
  • Do you know of a way to find out these activity names on other phones? (Phone manufacturers have a tendency to implement their own take on battery management.) – Razor_alpha Jul 16 '19 at 14:02
  • @Razor_alpha Not really - However, you can fetch the system's intents and debug it regarding the cases named above. In addition there are some repositories out there, taking care of issue such as these and providing a basic functionality manufacturer wise. – schlenger Oct 12 '19 at 18:40
3

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alon Minski
  • 1,571
  • 2
  • 19
  • 32
  • That works beautifully indeed, however using direct activity is "dangerous" because some manufacturers are changing them if not removing them so it needs a try/catch and a fall back solution to be usable. Thanks. – 3c71 Mar 18 '21 at 14:47
  • There was no 'mCurrentFocus' in my output, but doing this got the answer I needed: dumpsys window windows | grep -E 'Window #' – hemisphire Jun 14 '23 at 14:57
0

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);
Dmitry Grushin
  • 701
  • 5
  • 6