一聚教程网:一个值得你收藏的教程网站

热门教程

android引导用户开启自启动权限的方法

时间:2022-06-25 22:51:31 编辑:袖梨 来源:一聚教程网

前言:

最近在做项目的过程中遇到了以下一个需求,虽然看起来不难实现,但是在实现的过程中遇到了各种坑,记录一下,今后方便查看!!!

需求:

用户第一次安装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 {
    // 将用户引导到系统设置页面
    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——–点击请求授权—–>跳转至系统授权页——–点击back键——–>要求跳转到主页面(也就是MainActivity,注意不是Activity A)

在实现的过程中,就一直钻牛角尖,这个授权页面的Activity我也拿不到,怎么监听返回按钮呢???(黑人问号脸)

所以啊,这时候就体现出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();
 }

这次再次体现了基础!基础!基础!是多么重要!

热门栏目