当前位置:   article > 正文

2023-04-27 Android APP 不同机型跳转应用权限设置页面,我这里在小米(android12)和三星(android8)上测试_android intent跳转到指定的权限开关界面

android intent跳转到指定的权限开关界面

一、代码

  1. package com.xxx.bluetooth_ble.permission;
  2. import android.content.ActivityNotFoundException;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageInfo;
  7. import android.content.pm.PackageManager;
  8. import android.content.pm.ResolveInfo;
  9. import android.net.Uri;
  10. import android.os.Build;
  11. import android.provider.Settings;
  12. import android.util.Log;
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.util.List;
  17. public class JumpPermissionManagement {
  18. private Context mContext;
  19. private String TAG = "JumpPermissionManagement";
  20. //自己的项目包名
  21. private String packageName = "your-packageName(自己去清单文件看一下)";
  22. public JumpPermissionManagement(Context context) {
  23. this.mContext = context;
  24. }
  25. public void jumpPermissionPage() {
  26. try {
  27. String name = Build.MANUFACTURER;
  28. switch (name) {
  29. case "HUAWEI":
  30. goHuaWeiManager();
  31. break;
  32. case "vivo":
  33. goVivoManager();
  34. break;
  35. case "OPPO":
  36. goOppoManager();
  37. break;
  38. case "Coolpad":
  39. goCoolpadManager();
  40. break;
  41. case "Meizu":
  42. goMeiZuManager();
  43. break;
  44. case "Xiaomi":
  45. goXiaoMiMainager();
  46. break;
  47. case "samsung":
  48. goSangXinManager();
  49. break;
  50. case "Sony":
  51. goSonyManager();
  52. break;
  53. case "LG":
  54. goLGManager();
  55. break;
  56. default:
  57. goIntentSetting();
  58. break;
  59. }
  60. } catch (Exception e) {
  61. goIntentSetting();
  62. }
  63. }
  64. private void goLGManager() {
  65. try {
  66. Intent intent = new Intent(packageName);
  67. ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity");
  68. intent.setComponent(comp);
  69. mContext.startActivity(intent);
  70. } catch (Exception e) {
  71. goIntentSetting();
  72. }
  73. }
  74. private void goSonyManager() {
  75. try {
  76. Intent intent = new Intent(packageName);
  77. ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
  78. intent.setComponent(comp);
  79. mContext.startActivity(intent);
  80. } catch (Exception e) {
  81. goIntentSetting();
  82. }
  83. }
  84. private void goHuaWeiManager() {
  85. try {
  86. Intent intent = new Intent(packageName);
  87. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  88. ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
  89. intent.setComponent(comp);
  90. mContext.startActivity(intent);
  91. } catch (Exception e) {
  92. goIntentSetting();
  93. }
  94. }
  95. private static String getMiuiVersion() {
  96. String propName = "ro.miui.ui.version.name";
  97. String line = null;
  98. BufferedReader input = null;
  99. try {
  100. Process p = Runtime.getRuntime().exec("getprop " + propName);
  101. input = new BufferedReader(
  102. new InputStreamReader(p.getInputStream()), 1024);
  103. line = input.readLine();
  104. } catch (Exception ex) {
  105. //Timber.e(ex);
  106. } finally {
  107. if (input != null) {
  108. try {
  109. input.close();
  110. } catch (IOException e) {
  111. //Timber.e(e);
  112. }
  113. }
  114. }
  115. return line;
  116. }
  117. private void goXiaoMiMainager() {
  118. String rom = getMiuiVersion();
  119. Log.d(TAG,"getMiuiVersion:"+rom);
  120. Intent intent = new Intent();
  121. if ("V6".equals(rom) || "V7".equals(rom)) {
  122. intent.setAction("miui.intent.action.APP_PERM_EDITOR");
  123. intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
  124. intent.putExtra("extra_pkgname", packageName);
  125. } else if ("V8".equals(rom) || "V9".equals(rom)) {
  126. intent.setAction("miui.intent.action.APP_PERM_EDITOR");
  127. intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
  128. intent.putExtra("extra_pkgname", packageName);
  129. } else if ("V130".equals(rom)){
  130. intent= new Intent("miui.intent.action.APP_PERM_EDITOR");
  131. intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
  132. intent.putExtra("extra_pkgname", mContext.getPackageName());
  133. }else
  134. goIntentSetting();
  135. mContext.startActivity(intent);
  136. }
  137. private void goMeiZuManager() {
  138. try {
  139. Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
  140. intent.addCategory(Intent.CATEGORY_DEFAULT);
  141. intent.putExtra("packageName", packageName);
  142. mContext.startActivity(intent);
  143. } catch (ActivityNotFoundException localActivityNotFoundException) {
  144. localActivityNotFoundException.printStackTrace();
  145. goIntentSetting();
  146. }
  147. }
  148. private void goSangXinManager() {
  149. //三星4.3可以直接跳转
  150. goIntentSetting();
  151. }
  152. private void goIntentSetting() {
  153. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  154. Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
  155. intent.setData(uri);
  156. try {
  157. mContext.startActivity(intent);
  158. } catch (Exception e) {
  159. //Timber.e(e);
  160. }
  161. }
  162. private void goOppoManager() {
  163. doStartApplicationWithPackageName("com.coloros.safecenter");
  164. }
  165. /**
  166. * doStartApplicationWithPackageName("com.yulong.android.security:remote")
  167. * 和Intent open = getPackageManager().getLaunchIntentForPackage("com.yulong.android.security:remote");
  168. * startActivity(open);
  169. * 本质上没有什么区别,通过Intent open...打开比调用doStartApplicationWithPackageName方法更快,也是android本身提供的方法
  170. */
  171. private void goCoolpadManager() {
  172. doStartApplicationWithPackageName("com.yulong.android.security:remote");
  173. /* Intent openQQ = getPackageManager().getLaunchIntentForPackage("com.yulong.android.security:remote");
  174. startActivity(openQQ);*/
  175. }
  176. private void goVivoManager() {
  177. doStartApplicationWithPackageName("com.bairenkeji.icaller");
  178. /* Intent openQQ = getPackageManager().getLaunchIntentForPackage("com.vivo.securedaemonservice");
  179. startActivity(openQQ);*/
  180. }
  181. /**
  182. * 此方法在手机各个机型设置中已经失效
  183. *
  184. * @return
  185. */
  186. private Intent getAppDetailSettingIntent() {
  187. Intent localIntent = new Intent();
  188. localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  189. if (Build.VERSION.SDK_INT >= 9) {
  190. localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  191. localIntent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
  192. } else if (Build.VERSION.SDK_INT <= 8) {
  193. localIntent.setAction(Intent.ACTION_VIEW);
  194. localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
  195. localIntent.putExtra("com.android.settings.ApplicationPkgName", mContext.getPackageName());
  196. }
  197. return localIntent;
  198. }
  199. private void doStartApplicationWithPackageName(String packagename) {
  200. // 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
  201. PackageInfo packageinfo = null;
  202. try {
  203. packageinfo = mContext.getPackageManager().getPackageInfo(packagename, 0);
  204. } catch (PackageManager.NameNotFoundException e) {
  205. //Timber.e(e);
  206. }
  207. if (packageinfo == null) {
  208. return;
  209. }
  210. try {
  211. // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
  212. Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
  213. resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  214. resolveIntent.setPackage(packageinfo.packageName);
  215. // 通过getPackageManager()的queryIntentActivities方法遍历
  216. List<ResolveInfo> resolveinfoList = mContext.getPackageManager().queryIntentActivities(resolveIntent, 0);
  217. ResolveInfo resolveinfo = resolveinfoList.iterator().next();
  218. if (resolveinfo != null) {
  219. // packageName参数2 = 参数 packname
  220. String packageName = resolveinfo.activityInfo.packageName;
  221. // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packageName参数2.mainActivityname]
  222. String className = resolveinfo.activityInfo.name;
  223. // LAUNCHER Intent
  224. Intent intent = new Intent(Intent.ACTION_MAIN);
  225. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  226. // 设置ComponentName参数1:packageName参数2:MainActivity路径
  227. ComponentName cn = new ComponentName(packageName, className);
  228. intent.setComponent(cn);
  229. mContext.startActivity(intent);
  230. }
  231. } catch (Exception e) {
  232. goIntentSetting();
  233. }
  234. }
  235. }

二、调用的地方

  1. JumpPermissionManagement m_JumpPermissionManagement = new JumpPermissionManagement(m_context);
  2. m_JumpPermissionManagement.jumpPermissionPage();

三、三星手机的界面

 四、小米平板

1、打开settings里面的该app的权限界面,执行

dumpsys activity | grep mFocusedApp

 2、跳转的地方代码

  1. private void goXiaoMiMainager() {
  2. String rom = getMiuiVersion();
  3. Log.d(TAG,"getMiuiVersion:"+rom);
  4. Intent intent = new Intent();
  5. if ("V6".equals(rom) || "V7".equals(rom)) {
  6. intent.setAction("miui.intent.action.APP_PERM_EDITOR");
  7. intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
  8. intent.putExtra("extra_pkgname", packageName);
  9. } else if ("V8".equals(rom) || "V9".equals(rom)) {
  10. intent.setAction("miui.intent.action.APP_PERM_EDITOR");
  11. intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
  12. intent.putExtra("extra_pkgname", packageName);
  13. } else if ("V130".equals(rom)){
  14. intent= new Intent("miui.intent.action.APP_PERM_EDITOR");
  15. intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
  16. intent.putExtra("extra_pkgname", mContext.getPackageName());
  17. }else
  18. goIntentSetting();
  19. mContext.startActivity(intent);
  20. }

3、效果

 

 

五、参考文章

Android调用miui给权限,Android跳转至MIUI权限设置页面_喝冰红茶的虫的博客-CSDN博客

Android兼容适配 - 不同机型跳转应用权限设置页面_android跳转应用权限设置_Modu_Liu的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/378255?site
推荐阅读
相关标签
  

闽ICP备14008679号