1

I need to access to programmatically turn ON & OFF Power Saving Mode in android. Is there any API for this ? As I can see Power Saving mode option is not available for all mobile devices, is there any code to check these settings availability and if available then turn it ON and OFF ?

Also is there any work around ?

snehal_penurkar
  • 273
  • 1
  • 3
  • 15
  • 1
    [http://stackoverflow.com/questions/18609978/power-saving-mode-in-android-controlled-programatically][1] sorry but you can't do that [1]: http://stackoverflow.com/questions/18609978/power-saving-mode-in-android-controlled-programatically – Wishmaster Apr 21 '14 at 10:28
  • Yes I gone through it. But wanted to know has Samsung provided any such APIs for Power Saver Mode enabled devices ? – snehal_penurkar Apr 21 '14 at 10:41
  • You can use the PowerManager.https://developer.android.com/reference/android/os/PowerManager.html. – Ajit Kumar Apr 21 '14 at 10:48

5 Answers5

8

If you want to test the Power Saving Mode (PSM):

The following works with Android Studio 3.4.1 and a Google Pixel 2 XL phone:

$ adb shell dumpsys battery unplug # a charging device cannot enter PSM
$ adb shell settings put global low_power 1 # enter Power Saving Mode (PSM)

This only works on devices that has the standard Power Saving Mode, like Samsung's, but not Huawei's, for example. On some Samsung devices, this setting is stored in global setting keyed psm_switch, as mentioned above.

You can leave PSM by setting low_power to 0 or by enabling charging again with:

$ adb shell dumpsys battery reset

Source: https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze_and_app_standby

hb0
  • 3,350
  • 3
  • 30
  • 48
sirius
  • 81
  • 1
  • 2
1

It's bad practice to control the user's device settings from your app.

Rather consider suspending background services when battery levels are low.

Or to notify the user of low battery power and advising the user to switch of unnecessary settings like Bluetooth or WiFi.

marienke
  • 2,465
  • 4
  • 34
  • 66
  • dude maybe somebody may need to because there is an issue with finding the option – barlop Sep 04 '18 at 03:55
  • Firstly, that answer was written in 2015 when a lot of things were still restricted or not available yet, so there's that, but secondly, theoretically, you could use the PowerManager (https://developer.android.com/reference/android/os/PowerManager). The point was always that messing with the user's device settings was considered an invasion of privacy... – marienke Nov 08 '18 at 18:47
  • It may be a questioner's phone, / a case of user=the programmer, and it's not considered an invasion of privacy if you are the user/programmer and it's your phone. So just saying "bad practice" and not saying how, is avoiding the question. – barlop Nov 09 '18 at 09:43
  • That's fair. I could have written a comment on his question rather than an answer like this. However, if the asker wanted it for use in their app (which they're not clear about in the question), then my answer would have been legit. – marienke Nov 12 '18 at 13:21
0

Most devices won't have a built-in power-saver API, so you could try to make your own power-saver mode by turning off the bluetooth if it's on, the wi-fi, etc, deactivating location in settings, or checking for other power-eating options enabled.

Valdrinium
  • 1,398
  • 1
  • 13
  • 28
  • But when I run my own Power Saver application on devices which are having in-build Power Saver Mode, then what will be the behavior of settings ? Also the in-build Power Saver does some extra battery saving work like decreasing display refresh rate, turning off half of the CPU core.. that wont be done by coding right ? – snehal_penurkar Apr 21 '14 at 10:45
  • You are talking about a Samsung, most likely. Well, in that case I recommend this http://developer.samsung.com/develop – Valdrinium Apr 21 '14 at 10:50
  • Yes I checked SDK too but havn't find any reference. – snehal_penurkar Apr 21 '14 at 11:06
0

There are no available API's to do this in android SDK. however You can control/save the power by Turning off Blutooth,Wi-Fi and other unused sevices inside your coding.

Ajit Kumar
  • 534
  • 3
  • 8
  • Can you just check my response to @WishMaster. – snehal_penurkar Apr 21 '14 at 10:47
  • In my experience Samsung, as well as HTC, is one of the manufacturers that modify Android OS in most unpredictable ways. "power saving mode" is not an official AOSP feature and there are no ways to detect it via official SDK. There are some possible ways how you can work around it though. – Ajit Kumar Apr 21 '14 at 11:23
  • Ok. Thanks All. I think I need to go for option two. – snehal_penurkar Apr 21 '14 at 11:55
0

You can't set the power saving mode programmatically. Allowing this would be a bad idea.

On certain Samsung devices you can check if it is enabled:

final String result = Settings.System.getString(getContentResolver(), "psm_switch");
Log.v("Debug", "Powersaving active: " + TextUtils.equals(result, "1"));

See this for more info: https://stackoverflow.com/a/39296959/3600178

Community
  • 1
  • 1
ViciDroid
  • 3,555
  • 4
  • 14
  • 16