简体   繁体   English

以编程方式检查节电模式

[英]Checking for Power Saver Mode programmatically

I have written an app (AutoWifiSwitch) and one of the features I plan to add is automatically disabling the wifi scanning service in my app if power saving mode is enabled.我编写了一个应用程序 (AutoWifiSwitch),我计划添加的功能之一是在启用省电模式时自动禁用我的应用程序中的 wifi 扫描服务。

I know Android L is supposed to have Battery Saving implemented (previously HTC and Samsung would add the features themselves to the software).我知道 Android L 应该实现了省电功能(以前 HTC 和三星会将这些功能本身添加到软件中)。 Presumably this now means Google will have added some sort of API for it.据推测,这现在意味着谷歌将为它添加某种 API。 Ideally there would be a new action added so I could listen for that.理想情况下会添加一个新动作,以便我可以倾听。

I would also like to know if the above is possible with HTC/Samsung APIs and if so, how do I use them.我还想知道 HTC/Samsung API 是否可以实现上述功能,如果可以,我该如何使用它们。

I've been searching everywhere for the above questions but had absolutely no luck, the app SecureSettings (an addon for Tasker) is able to hook into the HTC/Samsung APIs to enable the power saver anyway, I'm not quite sure how they do it.我一直在到处寻找上述问题,但绝对没有运气,应用程序 SecureSettings(Tasker 的一个插件)能够连接到 HTC/Samsung API 以启用节电功能,我不太确定它们是如何实现的做吧。

Edit : The power saver value can be gotten from the PowerManager in Android L, not sure if there is an Action for it though.编辑:节电值可以从 Android L 中的 PowerManager 获取,但不确定是否有针对它的操作。

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean powerSaveMode = powerManager.isPowerSaveMode();

Docs: developer.android.com/.../PowerManager#isPowerSaveMode()文档: developer.android.com/.../PowerManager#isPowerSaveMode()

Added in API level 21 (Android 5.0)API level 21 (Android 5.0) 中添加

I've eventually figured out how to do this with HTC and Samsung devices.我最终想出了如何使用 HTC 和三星设备来做到这一点。 Both store their power manager settings in Settings.System.两者都将其电源管理器设置存储在 Settings.System 中。

HTC (Sense) uses the key user_powersaver_enable . HTC (Sense) 使用 key user_powersaver_enable Samsung (Touchwiz) uses the key psm_switch .三星(Touchwiz)使用密钥psm_switch

Both store the boolean as a String, "0" being false and "1" being true.两者都将布尔值存储为字符串,“0”为假,“1”为真。 You can then listen for changes using a ContentObserver like so (requires API level 16 or higher):然后,您可以像这样使用 ContentObserver 监听更改(需要 API 级别 16 或更高级别):

getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, new ContentObserver(){
  @Override
  public void onChange(boolean selfChange, Uri uri){
    super.onChange(selfChange, uri);
    String key = uri.getPath();
    key = key.substring(key.lastIndexOf("/") + 1, key.length());

    if (key.equals("user_powersaver_enable") || key.equals("psm_switch")){
      boolean batterySaverEnabled = Settings.System.getString(getContentResolver(), key).equals("1");
      // do something
    }
  }
});

However this will only be applicable until Android L is release, when L is released HTC and Samsung will likely move over to the AOSP battery saver which means you will be able to use the new battery saver api in L.然而,这仅适用于 Android L 发布,当 L 发布时,HTC 和三星可能会转移到 AOSP 节电模式,这意味着您将能够在 L 中使用新的节电模式 api。

It did not work for me on my Samsung S8, Android 9, instead I used:它在我的三星 S8、Android 9 上对我不起作用,而是我使用了:

  • to set: $adb shell settings put global low_power 1设置: $adb shell settings put global low_power 1
  • to get: $adb shell settings get global low_power获取: $adb shell settings get global low_power

...and it worked! ...它奏效了!

Translating it into java it goes something like this:将它翻译成 Java 是这样的:

final String result = Settings.Global.getString(getContentResolver(),"low_power");

"power saving mode" is not an official AOSP feature and there are no ways to detect it via official SDK. “省电模式”不是官方的 AOSP 功能,也无法通过官方 SDK 检测到它。 it is manufacturer specific.它是制造商特定的。 check this link 检查此链接

This answer is only for the Samsung power saver mode, since as usual Samsung still chooses to mess with the standard APIs.这个答案仅适用于三星省电模式,因为像往常一样三星仍然选择与标准 API 混为一谈。

iKeirNez posted an excellent answer that confirmed a suspicion of mine. iKeirNez 发布了一个很好的答案,证实了我的怀疑。 This still happens on lower end devices you can buy that have 5.1.1.这仍然发生在您可以购买的具有 5.1.1 的低端设备上。 I have a list compiled of the devices which I know of that suffer from a camera crash when on power saving mode, but I have not yet checked for all the possible keys that relate to power saving mode.我有一个我知道的设备列表,这些设备在省电模式下会遭受相机崩溃,但我还没有检查与省电模式相关的所有可能的键。

I know about these ones so far:到目前为止,我知道这些:

content://settings/system/powersaving_switch content://settings/system/power Saving_switch

content://settings/system/psm_switch content://settings/system/psm_switch

You can register a ContentResolver and listen for changes OR you can check it when necessary:您可以注册一个 ContentResolver 并监听更改,或者您可以在必要时检查它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM