Android通知权限设置(8.0上下兼容)

记录开发中通知权限使用

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();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值