记录开发中通知权限使用

public class NotificationAccess {    private static final String TAG = "NotificationAccess";    public static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";    private static final HashSet<ComponentName> mEnabledListeners = new HashSet<>();    @RequiresPermission(allOf = {Manifest.permission.WRITE_SETTINGS, Manifest.permission.WRITE_SECURE_SETTINGS})    public static void enableNotificationAccess(Context context, String packageName, String serviceName) {        if (isAccessibilityEnabled(context)) {            LogUtil.d(TAG, "enableNotificationAccess: the accessibility has been enabled");            return;        }        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);            try {                Method m_setNotificationListenerAccessGranted = manager.getClass()                        .getDeclaredMethod("setNotificationListenerAccessGranted", ComponentName.class, boolean.class);                m_setNotificationListenerAccessGranted.setAccessible(true);                m_setNotificationListenerAccessGranted.invoke(manager, new ComponentName(packageName, serviceName), true);            } catch (NoSuchMethodException e) {                e.printStackTrace();            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (InvocationTargetException e) {                e.printStackTrace();            }//            manager.setNotificationListenerAccessGranted(new ComponentName(packageName,serviceName),true);        } else {            loadEnabledListener(context);            mEnabledListeners.add(new ComponentName(packageName, serviceName));            StringBuilder stringBuilder = null;            for (final ComponentName componentName : mEnabledListeners) {                if (stringBuilder == null) {                    stringBuilder = new StringBuilder();                } else {                    stringBuilder.append(":");                }                stringBuilder.append(componentName.flattenToShortString());            }            Settings.Secure.putString(context.getContentResolver(),                    ENABLED_NOTIFICATION_LISTENERS, stringBuilder != null ? stringBuilder.toString() : "");        }    }    /**     * 判断当前应用是否开启NotificationListener监听权限     *     * @param context     * @return     */    private static boolean isAccessibilityEnabled(Context context) {        String notificationList = Settings.Secure.getString(context.getContentResolver(), ENABLED_NOTIFICATION_LISTENERS);        if (!TextUtils.isEmpty(notificationList)) {            final String[] names = notificationList.split(":");            for (final String name : names) {                ComponentName componentName = ComponentName.unflattenFromString(name);                if (componentName != null) {                    if (TextUtils.equals(componentName.getPackageName(), context.getPackageName())) {                        return true;                    }                }            }        }        return false;    }    /**     * 获取所有开启NotificationListener监听权限的组件     *     * @param context     */    private static void loadEnabledListener(Context context) {        mEnabledListeners.clear();        String notificationList = Settings.Secure.getString(context.getContentResolver(), ENABLED_NOTIFICATION_LISTENERS);        if (!TextUtils.isEmpty(notificationList)) {            final String[] names = notificationList.split(":");            for (final String name : names) {                ComponentName componentName = ComponentName.unflattenFromString(name);                if (componentName != null) {                    mEnabledListeners.add(componentName);                }            }        }    }    /**     * 去设置里手动打开NotificationListener监听权限     *     * @param context     */    public static void toOpenNotificationListenAccessManually(Context context) {        try {            Intent intent;            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {                intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);            } else {                intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");            }            context.startActivity(intent);        } catch (Exception e) {            e.printStackTrace();        }    }}

更多相关文章

  1. android 视频下载
  2. Android(安卓)TV开发中所有的遥控器按键监听
  3. Android(安卓)开启闪光灯做手电筒 源码
  4. Android开启/关闭/监听 飞行模式
  5. 关于Android短信拦截(三)
  6. android获取位置权限,手机状态权限,存储权限
  7. Android中系统设置参数改变监听(以时间同步为例)
  8. android 如何判断程序是否在前台运行
  9. Android(安卓)摄像头聚焦

随机推荐

  1. 「Android问卷调查类型页面及逻辑实现」R
  2. android之高德地图学习
  3. react-navigation使用及dva初探二
  4. Android 自定义控件
  5. Android:Gradle 解析
  6. Android -- Android Init进程的处理流程
  7. android process and thread
  8. Blog-07-《一周快速上手Kotlin For Andro
  9. QCom Camera Development
  10. 一个很棒的 Android(安卓)APP框架