Android如何判断通知栏权限是否开启:

     起先我是想通过app手动检测权限是否开启,进行动态设置及:

     

//检测权限是否开启ContextCompat.checkSelfPermission()//设置权限ActivityCompat.requestPermissions();

但是我查阅的好些资料,都没有查到其权限名是什么,貌似没有。无奈只有放弃这种方式

检测通知栏权限是否开启

   @RequiresApi(api = Build.VERSION_CODES.KITKAT)    private boolean isNotificationEnabled(Context context) {        String CHECK_OP_NO_THROW = "checkOpNoThrow";        String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);        ApplicationInfo appInfo = context.getApplicationInfo();        String pkg = context.getApplicationContext().getPackageName();        int uid = appInfo.uid;        Class appOpsClass = null;     /* Context.APP_OPS_MANAGER */        try {            appOpsClass = Class.forName(AppOpsManager.class.getName());            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,                    String.class);            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);            int value = (Integer) opPostNotificationValue.get(Integer.class);            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);        } catch (Exception e) {            e.printStackTrace();        }         return false;    }

当通知栏权限未开启时跳转到自身的设置界面

    private void toSetting() {        Intent localIntent = new Intent();        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        if (Build.VERSION.SDK_INT >= 9) {            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");            localIntent.setData(Uri.fromParts("package", getPackageName(), null));        } else if (Build.VERSION.SDK_INT <= 8) {            localIntent.setAction(Intent.ACTION_VIEW);            localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());        }        startActivity(localIntent);    }



   
   


更多相关文章

  1. Android(安卓)NotificationManager 和Notification的使用总结
  2. Android(安卓)Wifi模块学习
  3. Android(安卓)通知栏显示与设置
  4. Android(安卓)10 适配攻略
  5. Android(安卓)后台服务简要概述
  6. 【Android】19.1 SharedPreferences类
  7. Android(安卓)通知使用权(NotificationListenerService)的使用
  8. Android权限问题:Permission is only granted to system apps
  9. 理解 Android(安卓)上的安全性

随机推荐

  1. C语言中数据int的取值范围为多少?
  2. sqrt在c语言中怎么用?
  3. C语言中如何生成1~100的随机数(附代码)
  4. vc++6.0怎么调试?
  5. c语言中pow函数的用法是什么?
  6. c语言用户标识符命名规则是什么?
  7. c语言中if(x)是什么意思?
  8. C语言中for用法是什么?
  9. 在c语言中二维数组元素在内存中的存放顺
  10. C程序总是以main函数作为程序执行的起始