以编程方式检查节电模式 [英] Checking for Power Saver Mode programmatically

查看:197
本文介绍了以编程方式检查节电模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个应用程序(AutoWifiSwitch),如果启用省电模式,我计划添加的其中一项功能会自动禁用我的应用程序中的wifi扫描服务。



<我知道Android L应该实现省电(以前HTC和三星会将这些功能添加到软件中)。据推测,现在这意味着Google将为其添加某种API。理想情况下会添加一个新动作,所以我可以听取它。



我还想知道HTC / Samsung API是否可以实现上述功能,如果可行,我如何使用它们。



我一直在寻找上述问题,但绝对没有运气,应用程序SecureSettings(Tasker的插件)能够挂钩无论如何,进入HTC /三星API以实现省电,我不太确定他们是如何做到的。



编辑:电源保护程序值可以从Android L中的PowerManager获得,但不确定是否有针对它的操作。

解决方案

我'我们最终想出了如何用HTC和三星设备做到这一点。两者都将他们的电源管理器设置存储在Settings.System中。



HTC(Sense)使用密钥 user_powersaver_enable
三星(Touchwiz)使用密钥 psm_switch



两者都将布尔值存储为字符串, 0是假的,1是真的。然后,您可以使用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);
/ /做某事
}
}
});

然而这只适用于Android L发布,当L发布时HTC和三星很可能转移到AOSP电池保护程序,这意味着您将能够使用L中的新电池保护程序api。


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.

I know Android L is supposed to have Battery Saving implemented (previously HTC and Samsung would add the features themselves to the software). Presumably this now means Google will have added some sort of API for it. 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.

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.

Edit: The power saver value can be gotten from the PowerManager in Android L, not sure if there is an Action for it though.

解决方案

I've eventually figured out how to do this with HTC and Samsung devices. Both store their power manager settings in Settings.System.

HTC (Sense) uses the key user_powersaver_enable. Samsung (Touchwiz) uses the key psm_switch.

Both store the boolean as a String, "0" being false and "1" being true. You can then listen for changes using a ContentObserver like so (requires API level 16 or higher):

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.

这篇关于以编程方式检查节电模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆