Android通知权限设置(8.0上下兼容)
16lz
2021-12-23
记录开发中通知权限使用
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(); } }}
更多相关文章
- android 视频下载
- Android(安卓)TV开发中所有的遥控器按键监听
- Android(安卓)开启闪光灯做手电筒 源码
- Android开启/关闭/监听 飞行模式
- 关于Android短信拦截(三)
- android获取位置权限,手机状态权限,存储权限
- Android中系统设置参数改变监听(以时间同步为例)
- android 如何判断程序是否在前台运行
- Android(安卓)摄像头聚焦