支持API19以上

方法一:public boolean isNotificationEnable(Context context){

        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(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("checkOpNoThrow", 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 (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;

    }

 

方法二:op为11

 public int checkOp(Context context, int op) {
        final int version = Build.VERSION.SDK_INT;
        if (version >= 19) {
            Object object = context.getSystemService("appops");
            Class c = object.getClass();
            try {
                Class[] cArg = new Class[3];
                cArg[0] = int.class;
                cArg[1] = int.class;
                cArg[2] = String.class;
                Method lMethod = c.getDeclaredMethod("checkOp", cArg);
                return (Integer) lMethod.invoke(object, op,
                        Binder.getCallingUid(), context.getPackageName());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

更多相关文章

  1. Android 文件管理方法
  2. android中自定义Toast方法详解(一)
  3. Android在网络中与JavaWeb的项目进行交互的方法
  4. Android加载Gif图片的一般方法:Movie实现
  5. Android、iPhone和Java三个平台一致的加密方法
  6. Android中Bitmap类getPixel方法获取的像素值为负
  7. Android的View体系(三):View坐标以及方法说明

随机推荐

  1. Android使用gradle生成maven库,上传githu
  2. Android之开发常用颜色
  3. 像写Flutter一样开发Android原生应用
  4. Android 中文API (67) —— BluetoothClass
  5. 使用ImageView 加上 Edittext做出自定义
  6. android和PC(Python)通过USB(adb模式)基于Soc
  7. Android 中文API (68) —— BluetoothClass
  8. Android(安卓)驱动之旅 第五章: 在Android
  9. [Android]-图片JNI(C++\Java)高斯模糊的实
  10. Android(安卓)滑动绘制流程探究 系统是如