生活随笔
收集整理的這篇文章主要介紹了
android应用申请加入电池优化白名单
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,在 AndroidManifest.xml 文件中配置一下權限:
|
1
|
<uses-permissionandroid:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
|
可以通過以下方法,判斷我們的應用是否在白名單中:
1
2
3
4
5
6
7
8
9
|
@RequiresApi(api = Build.VERSION_CODES.M)
privatebooleanisIgnoringBatteryOptimizations() {
booleanisIgnoring =false;
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if(powerManager !=null) {
isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName());
}
returnisIgnoring;
}
|
如果不在白名單中,可以通過以下代碼申請加入白名單:
01
02
03
04
05
06
07
08
09
10
|
@RequiresApi(api = Build.VERSION_CODES.M)
publicvoidrequestIgnoreBatteryOptimizations() {
try{
Intent intent =newIntent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+ getPackageName()));
startActivity(intent);
}catch(Exception e) {
e.printStackTrace();
}
}
|
申請時,應用上會出現這樣一個窗口:
可以看到,這個系統彈窗會有影響電池續航的提醒,所以如果想讓用戶點允許,必須要有相關的說明。如果要判斷用戶是否點擊了允許,可以在申請的時候調用 startActivityForResult,在 onActivityResult 里再判斷一次是否在白名單中。
加入后臺運行白名單的多廠商適配方法
7.1基本說明
Android 開發的一個難點在于,各大手機廠商對原生系統進行了不同的定制,導致我們需要進行不同的適配,后臺管理就是一個很好的體現。幾乎各個廠商都有自己的后臺管理,就算應用加入了后臺運行白名單,仍然可能會被廠商自己的后臺管理干掉。
如果能把應用加入廠商系統的后臺管理白名單,可以進一步降低進程被殺的概率。不同的廠商在不同的地方進行設置,一般是在各自的「手機管家」,但更難的是,就算同一個廠商的系統,不同的版本也可能是在不同地方設置。
最理想的做法是,我們根據不同手機,甚至是不同的系統版本,給用戶呈現一個圖文操作步驟,并且提供一個按鈕,直接跳轉到指定頁面進行設置。但需要對每個廠商每個版本進行適配,工作量是比較大的。我使用真機測試了大部分主流 Android 廠商的手機后,整理出了部分手機的相關資料。
首先我們可以定義這樣兩個方法:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
/**
* 跳轉到指定應用的首頁
*/
privatevoidshowActivity(@NonNullString packageName) {
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(intent);
}
/**
* 跳轉到指定應用的指定頁面
*/
privatevoidshowActivity(@NonNullString packageName,@NonNullString activityDir) {
Intent intent =newIntent();
intent.setComponent(newComponentName(packageName, activityDir));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
|
以下是部分手機的廠商判斷,跳轉方法及對應設置步驟,跳轉方法不保證在所有版本上都能成功跳轉,都需要加 try catch。
7.2華為
廠商判斷:
1
2
3
4
5
6
7
|
publicbooleanisHuawei() {
if(Build.BRAND ==null) {
returnfalse;
}else{
returnBuild.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor");
}
}
|
跳轉華為手機管家的啟動管理頁:
1
2
3
4
5
6
7
8
9
|
privatevoidgoHuaweiSetting() {
try{
showActivity("com.huawei.systemmanager",
"com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
}catch(Exception e) {
showActivity("com.huawei.systemmanager",
"com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
}
}
|
操作步驟:應用啟動管理 -> 關閉應用開關 -> 打開允許自啟動。
7.3小米
廠商判斷:
1
2
3
|
publicstaticbooleanisXiaomi() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("xiaomi");
}
|
跳轉小米安全中心的自啟動管理頁面:
1
2
3
4
|
privatevoidgoXiaomiSetting() {
showActivity("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity");
}
|
操作步驟:授權管理 -> 自啟動管理 -> 允許應用自啟動。
7.4OPPO
廠商判斷:
1
2
3
|
publicstaticbooleanisOPPO() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("oppo");
}
|
跳轉 OPPO 手機管家:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
privatevoidgoOPPOSetting() {
try{
showActivity("com.coloros.phonemanager");
}catch(Exception e1) {
try{
showActivity("com.oppo.safe");
}catch(Exception e2) {
try{
showActivity("com.coloros.oppoguardelf");
}catch(Exception e3) {
showActivity("com.coloros.safecenter");
}
}
}
}
|
操作步驟:權限隱私 -> 自啟動管理 -> 允許應用自啟動。
7.5VIVO
廠商判斷:
1
2
3
|
publicstaticbooleanisVIVO() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("vivo");
}
|
跳轉 VIVO 手機管家:
1
2
3
|
privatevoidgoVIVOSetting() {
showActivity("com.iqoo.secure");
}
|
操作步驟:權限管理 -> 自啟動 -> 允許應用自啟動。
7.6魅族
廠商判斷:
1
2
3
|
publicstaticbooleanisMeizu() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("meizu");
}
|
跳轉魅族手機管家:
1
2
3
|
privatevoidgoMeizuSetting() {
showActivity("com.meizu.safe");
}
|
操作步驟:權限管理 -> 后臺管理 -> 點擊應用 -> 允許后臺運行。
7.7三星
廠商判斷:
1
2
3
|
publicstaticbooleanisSamsung() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("samsung");
}
|
跳轉三星智能管理器:
1
2
3
4
5
6
7
|
privatevoidgoSamsungSetting() {
try{
showActivity("com.samsung.android.sm_cn");
}catch(Exception e) {
showActivity("com.samsung.android.sm");
}
}
|
操作步驟:自動運行應用程序 -> 打開應用開關 -> 電池管理 -> 未監視的應用程序 -> 添加應用。
7.8樂視
廠商判斷:
1
2
3
|
publicstaticbooleanisLeTV() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("letv");
}
|
跳轉樂視手機管家:
1
2
3
4
|
privatevoidgoLetvSetting() {
showActivity("com.letv.android.letvsafe",
"com.letv.android.letvsafe.AutobootManageActivity");
}
|
操作步驟:自啟動管理 -> 允許應用自啟動。
7.9錘子
廠商判斷:
1
2
3
|
publicstaticbooleanisSmartisan() {
returnBuild.BRAND !=null&& Build.BRAND.toLowerCase().equals("smartisan");
}
|
跳轉手機管理:
1
2
3
|
privatevoidgoSmartisanSetting() {
showActivity("com.smartisanos.security");
}
|
操作步驟:權限管理 -> 自啟動權限管理 -> 點擊應用 -> 允許被系統啟動。
總結
以上是生活随笔為你收集整理的android应用申请加入电池优化白名单的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。