android引導(dǎo)用戶開啟自啟動權(quán)限的方法
前言:
最近在做項(xiàng)目的過程中遇到了以下一個需求,雖然看起來不難實(shí)現(xiàn),但是在實(shí)現(xiàn)的過程中遇到了各種坑,記錄一下,今后方便查看?。?!
需求:
用戶第一次安裝APP,點(diǎn)擊授權(quán)按鈕,跳轉(zhuǎn)至授權(quán)的頁面(不同手機(jī)跳轉(zhuǎn)到不同的授權(quán)頁面),用戶授權(quán)成功之后,點(diǎn)擊返回按鈕,直接進(jìn)入主頁面
問題:
1.如何適配不同機(jī)型
2.不同機(jī)型的授權(quán)頁面顯示不同彈窗(比如三星顯示懸浮窗,小米顯示彈窗)
3.小米彈窗始終無法顯示
4.在授權(quán)頁面點(diǎn)擊返回按鈕,怎么直接跳轉(zhuǎn)到主頁面
問題1:適配不同機(jī)型
這個是借鑒的一篇博文(忘記地方了,后邊找到了再添加~~)
public class MobileInfoUtils{
private SettingDialogPermision dialog_per;
//獲取手機(jī)類型
private static String getMobileType() {
return Build.MANUFACTURER;
}
//跳轉(zhuǎn)至授權(quán)頁面
public void jumpStartInterface(Context context) {
Intent intent = new Intent();
try {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("HLQ_Struggle", "******************當(dāng)前手機(jī)型號為:" + 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,不允許被其他程序調(diào)用
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");//跳轉(zhuǎn)到手機(jī)管家
componentName = ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity");//跳轉(zhuǎn)到后臺管理頁面
} else if (getMobileType().equals("OPPO")) { // OPPO R8205測試通過
componentName = ComponentName.unflattenFromString("com.oppo.safe/.permission.startup.StartupAppListActivity");
} else if (getMobileType().equals("ulong")) { // 360手機(jī) 未測試
componentName = new ComponentName("com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity");
} else {
// 將用戶引導(dǎo)到系統(tǒng)設(shè)置頁面
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) {//拋出異常就直接打開設(shè)置頁面
Log.e("HLQ_Struggle", e.getLocalizedMessage());
intent = new Intent(Settings.ACTION_SETTINGS);
context.startActivity(intent);
}
}
//小米手機(jī)顯示彈窗
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:不同機(jī)型的授權(quán)頁面顯示不同彈窗
在上面的問題中已經(jīng)解決。
思路如下:
①首先判斷當(dāng)前的機(jī)型
②判斷完機(jī)型之后,通過intent跳轉(zhuǎn)至不同的授權(quán)頁面
③在startActivity()之后顯示懸浮窗或者是彈窗
④小米手機(jī)在顯示彈窗的時(shí)候?qū)懮舷旅孢@一句話:
getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST)
因?yàn)檫@里類型沒有用“吐司”,所以在授權(quán)頁面一直不顯示彈窗
問題3:小米彈窗始終無法顯示
在問題2的第4步解決
問題4:在授權(quán)頁面點(diǎn)擊返回按鈕,怎么直接跳轉(zhuǎn)到主頁面
邏輯梳理:
Activity A——–點(diǎn)擊請求授權(quán)—–>跳轉(zhuǎn)至系統(tǒng)授權(quán)頁——–點(diǎn)擊back鍵——–>要求跳轉(zhuǎn)到主頁面(也就是MainActivity,注意不是Activity A)
在實(shí)現(xiàn)的過程中,就一直鉆牛角尖,這個授權(quán)頁面的Activity我也拿不到,怎么監(jiān)聽返回按鈕呢???(黑人問號臉)
所以啊,這時(shí)候就體現(xiàn)出Activity生命周期的重要性了。
在授權(quán)頁面,點(diǎn)擊返回鍵后,會再次跳轉(zhuǎn)到Activity A頁面,這時(shí)候只需要在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)了基礎(chǔ)!基礎(chǔ)!基礎(chǔ)!是多么重要!
以上這篇android引導(dǎo)用戶開啟自啟動權(quán)限的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
android開發(fā)通過Scroller實(shí)現(xiàn)過渡滑動效果操作示例
這篇文章主要介紹了android開發(fā)通過Scroller實(shí)現(xiàn)過渡滑動效果,結(jié)合實(shí)例形式分析了Android Scroller類實(shí)現(xiàn)過渡滑動效果的基本原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-01-01
Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符
Kotlin 是一種在 Java 虛擬機(jī)上運(yùn)行的靜態(tài)類型編程語言,被稱之為 Android 世界的Swift,由 JetBrains 設(shè)計(jì)開發(fā)并開源,下面這篇文章主要給大家介紹了關(guān)于Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符的相關(guān)資料,需要的朋友可以參考下。2017-12-12

