当前位置:   article > 正文

Android中打开手机通知权限设置

Android中打开手机通知权限设置

参考网址:关于安卓判断通知权限_水很清的博客-CSDN博客 

参考网址:https://blog.csdn.net/u012556114/article/details/120319874

 通过以下代码可直接跳转到项目的通知权限设置界面

  1. public class NotifyManagerUtils {
  2. private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
  3. private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
  4. //调用该方法获取是否开启通知栏权限
  5. public static boolean isNotifyEnabled(Context context) {
  6. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  7. return isEnableV26(context);
  8. } else {
  9. return isEnabledV19(context);
  10. }
  11. }
  12. /**
  13. * 8.0以下判断
  14. *
  15. * @param context api19 4.4及以上判断
  16. * @return
  17. */
  18. private static boolean isEnabledV19(Context context) {
  19. AppOpsManager mAppOps =
  20. (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
  21. ApplicationInfo appInfo = context.getApplicationInfo();
  22. String pkg = context.getApplicationContext().getPackageName();
  23. int uid = appInfo.uid;
  24. Class appOpsClass;
  25. try {
  26. appOpsClass = Class.forName(AppOpsManager.class.getName());
  27. Method checkOpNoThrowMethod =
  28. appOpsClass.getMethod(CHECK_OP_NO_THROW,
  29. Integer.TYPE, Integer.TYPE, String.class);
  30. Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
  31. int value = (Integer) opPostNotificationValue.get(Integer.class);
  32. return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==
  33. AppOpsManager.MODE_ALLOWED);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. return true;
  37. }
  38. }
  39. /**
  40. * 8.0及以上通知权限判断
  41. *
  42. * @param context
  43. * @return
  44. */
  45. private static boolean isEnableV26(Context context) {
  46. ApplicationInfo appInfo = context.getApplicationInfo();
  47. String pkg = context.getApplicationContext().getPackageName();
  48. int uid = appInfo.uid;
  49. try {
  50. NotificationManager notificationManager = (NotificationManager)
  51. context.getSystemService(Context.NOTIFICATION_SERVICE);
  52. @SuppressLint("DiscouragedPrivateApi")
  53. Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
  54. sServiceField.setAccessible(true);
  55. Object sService = sServiceField.invoke(notificationManager);
  56. Method method = null;
  57. if (sService != null) {
  58. method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage"
  59. , String.class, Integer.TYPE);
  60. method.setAccessible(true);
  61. }
  62. return (boolean) method.invoke(sService, pkg, uid);
  63. } catch (Exception e) {
  64. return true;
  65. }
  66. }
  67. /**
  68. * 打开通知权限
  69. *
  70. * @param context
  71. */
  72. public static void openNotificationSettingsForApp(Context context) {
  73. // Links to this app's notification settings.
  74. if (isNotifyEnabled(context))
  75. return;
  76. Intent intent = new Intent();
  77. intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
  78. intent.putExtra("app_package", context.getPackageName());
  79. intent.putExtra("app_uid", context.getApplicationInfo().uid);
  80. // for Android 8 and above
  81. intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
  82. context.startActivity(intent);
  83. }
  84. }

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

闽ICP备14008679号