問題描述
我已經編寫了一個應用程序 (AutoWifiSwitch),并且我計劃添加的功能之一是如果啟用了省電模式,則自動禁用我的應用程序中的 wifi 掃描服務.
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.
我知道 Android L 應該實現了省電功能(以前 HTC 和三星會將這些功能自己添加到軟件中).現在大概這意味著谷歌將為其添加某種 API.理想情況下,會添加一個新動作,以便我可以聽.
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.
我還想知道 HTC/Samsung API 是否可以實現上述操作,如果可以,我該如何使用它們.
I would also like to know if the above is possible with HTC/Samsung APIs and if so, how do I use them.
我一直在到處尋找上述問題,但絕對沒有運氣,應用程序 SecureSettings(Tasker 的插件)能夠連接到 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.
編輯:節電值可以從Android L中的PowerManager獲取,但不確定是否有Action.
Edit: The power saver value can be gotten from the PowerManager in Android L, not sure if there is an Action for it though.
推薦答案
我最終想出了如何用 HTC 和三星設備做到這一點.兩者都將其電源管理器設置存儲在 Settings.System 中.
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) 使用密鑰 user_powersaver_enable
.三星 (Touchwiz) 使用 psm_switch
鍵.
HTC (Sense) uses the key user_powersaver_enable
.
Samsung (Touchwiz) uses the key psm_switch
.
兩者都將布爾值存儲為字符串,0"為假,1"為真.然后,您可以像這樣使用 ContentObserver 監聽更改(需要 API 級別 16 或更高):
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
}
}
});
但這僅適用于 Android L 發布之前,當 L 發布時,HTC 和三星可能會轉移到 AOSP 省電模式,這意味著您將能夠在 L 中使用新的省電模式 api.
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.
這篇關于以編程方式檢查省電模式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!