android引導用戶開啟自啟動權限的方法
前言:
最近在做項目的過程中遇到了以下一個需求,雖然看起來不難實現(xiàn),但是在實現(xiàn)的過程中遇到了各種坑,記錄一下,今后方便查看!??!
需求:
用戶第一次安裝APP,點擊授權按鈕,跳轉至授權的頁面(不同手機跳轉到不同的授權頁面),用戶授權成功之后,點擊返回按鈕,直接進入主頁面
問題:
1.如何適配不同機型
2.不同機型的授權頁面顯示不同彈窗(比如三星顯示懸浮窗,小米顯示彈窗)
3.小米彈窗始終無法顯示
4.在授權頁面點擊返回按鈕,怎么直接跳轉到主頁面
問題1:適配不同機型
這個是借鑒的一篇博文(忘記地方了,后邊找到了再添加~~)
public class MobileInfoUtils{
private SettingDialogPermision dialog_per;
//獲取手機類型
private static String getMobileType() {
return Build.MANUFACTURER;
}
//跳轉至授權頁面
public void jumpStartInterface(Context context) {
Intent intent = new Intent();
try {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("HLQ_Struggle", "******************當前手機型號為:" + getMobileType());
ComponentName componentName = null;
if (getMobileType().equals("Xiaomi")) { // 紅米Note4測試通過
componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
} else if (getMobileType().equals("Letv")) { // 樂視2測試通過
intent.setAction("com.letv.android.permissionautoboot");
} else if (getMobileType().equals("samsung")) { // 三星Note5測試通過
//componentName = new ComponentName("com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
//componentName = ComponentName.unflattenFromString("com.samsung.android.sm/.ui.ram.RamActivity");// Permission Denial not exported from uid 1000,不允許被其他程序調用
componentName = ComponentName.unflattenFromString("com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity");
} else if (getMobileType().equals("HUAWEI")) { // 華為測試通過
//componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");//鎖屏清理
componentName = ComponentName.unflattenFromString("com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity");//跳自啟動管理
//SettingOverlayView.show(context);
} else if (getMobileType().equals("vivo")) { // VIVO測試通過
componentName = ComponentName.unflattenFromString("com.iqoo.secure/.safeguard.PurviewTabActivity");
} else if (getMobileType().equals("Meizu")) { //萬惡的魅族
//componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity");//跳轉到手機管家
componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity");//跳轉到后臺管理頁面
} else if (getMobileType().equals("OPPO")) { // OPPO R8205測試通過
componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
} else if (getMobileType().equals("ulong")) { // 360手機 未測試
componentName = new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity");
} else {
// 將用戶引導到系統(tǒng)設置頁面
if (Build.VERSION.SDK_INT >= 9) {
Log.e("HLQ_Struggle", "APPLICATION_DETAILS_SETTINGS");
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
}
}
intent.setComponent(componentName);
context.startActivity(intent);
if (getMobileType().equals("Xiaomi")) {
showtip();//顯示彈窗(**特別注意**)
}
if (getMobileType().equals("samsung")){
new SettingOverlayView().show(context);//顯示懸浮窗
}
} catch (Exception e) {//拋出異常就直接打開設置頁面
Log.e("HLQ_Struggle", e.getLocalizedMessage());
intent = new Intent(Settings.ACTION_SETTINGS);
context.startActivity(intent);
}
}
//小米手機顯示彈窗
private void showtip() {
try {
dialog_per=new SettingDialogPermision(context, R.style.CustomDialog4);
dialog_per.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);//注意這里改成吐司類型
dialog_per.show();
Log.e("HLQ_Struggle","顯示彈窗");
} catch (Exception e) {
e.printStackTrace();
Log.e("HLQ_Struggle", "沒有顯示彈窗"+e.getMessage());
}
}
}
問題2:不同機型的授權頁面顯示不同彈窗
在上面的問題中已經解決。
思路如下:
①首先判斷當前的機型
②判斷完機型之后,通過intent跳轉至不同的授權頁面
③在startActivity()之后顯示懸浮窗或者是彈窗
④小米手機在顯示彈窗的時候寫上下面這一句話:
getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST)
因為這里類型沒有用“吐司”,所以在授權頁面一直不顯示彈窗
問題3:小米彈窗始終無法顯示
在問題2的第4步解決
問題4:在授權頁面點擊返回按鈕,怎么直接跳轉到主頁面
邏輯梳理:
Activity A——–點擊請求授權—–>跳轉至系統(tǒng)授權頁——–點擊back鍵——–>要求跳轉到主頁面(也就是MainActivity,注意不是Activity A)
在實現(xiàn)的過程中,就一直鉆牛角尖,這個授權頁面的Activity我也拿不到,怎么監(jiān)聽返回按鈕呢???(黑人問號臉)
所以啊,這時候就體現(xiàn)出Activity生命周期的重要性了。
在授權頁面,點擊返回鍵后,會再次跳轉到Activity A頁面,這時候只需要在Activity A中寫上以下代碼就完美的解決了:
protected void onRestart() {
super.onRestart();
Intent intent = new Intent(SelfStartAcitity.this,MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
finish();
}
這次再次體現(xiàn)了基礎!基礎!基礎!是多么重要!
以上這篇android引導用戶開啟自啟動權限的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
android開發(fā)通過Scroller實現(xiàn)過渡滑動效果操作示例
這篇文章主要介紹了android開發(fā)通過Scroller實現(xiàn)過渡滑動效果,結合實例形式分析了Android Scroller類實現(xiàn)過渡滑動效果的基本原理與實現(xiàn)技巧,需要的朋友可以參考下2020-01-01

