整理下安卓跳转通知设置页面的代码,如下:

常量补充:

    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

1、android check通知是否开启代码

    public static boolean isNotificationEnabled(Context context) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            return isEnableV26(context);        } else {            return isEnableV19(context);        }    }    /**     * Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT     * 19及以上     *     * @param context     * @return     */    public static boolean isEnableV19(Context context) {        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);        ApplicationInfo appInfo = context.getApplicationInfo();        String pkg = context.getApplicationContext().getPackageName();        int uid = appInfo.uid;        Class appOpsClass;        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 (ClassNotFoundException e) {            MyLog.e(sTAG, e);        } catch (NoSuchMethodException e) {            MyLog.e(sTAG, e);        } catch (NoSuchFieldException e) {            MyLog.e(sTAG, e);        } catch (InvocationTargetException e) {            MyLog.e(sTAG, e);        } catch (IllegalAccessException e) {            MyLog.e(sTAG, e);        }        return false;    }    /**     * Build.VERSION.SDK_INT >= Build.VERSION_CODES.O     * 针对8.0及以上设备     *     * @param context     * @return     */    public static boolean isEnableV26(Context context) {        try {            NotificationManager notificationManager = (NotificationManager)                    context.getSystemService(Context.NOTIFICATION_SERVICE);            Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");            sServiceField.setAccessible(true);            Object sService = sServiceField.invoke(notificationManager);            ApplicationInfo appInfo = context.getApplicationInfo();            String pkg = context.getApplicationContext().getPackageName();            int uid = appInfo.uid;            Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage"                    , String.class, Integer.TYPE);            method.setAccessible(true);            return (boolean) method.invoke(sService, pkg, uid);        } catch (Exception e) {            MyLog.e(sTAG, e);        }        return false;    }

2、跳转到通知页面

 public static void gotoNotificationSetting(Activity activity) {        ApplicationInfo appInfo = activity.getApplicationInfo();        String pkg = activity.getApplicationContext().getPackageName();        int uid = appInfo.uid;        try {            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {                Intent intent = new Intent();                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);                //这种方案适用于 API 26, 即8.0(含8.0)以上可以用                intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);                intent.putExtra(Settings.EXTRA_CHANNEL_ID, uid);                //这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用                intent.putExtra("app_package", pkg);                intent.putExtra("app_uid", uid);                activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {                Intent intent = new Intent();                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);                intent.addCategory(Intent.CATEGORY_DEFAULT);                intent.setData(Uri.parse("package:" + GlobalData.app().getPackageName()));                activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);            } else {                Intent intent = new Intent(Settings.ACTION_SETTINGS);                activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);            }        } catch (Exception e) {            Intent intent = new Intent(Settings.ACTION_SETTINGS);            activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);            MyLog.e(sTAG, e);        }    }

3、但是目前存在两个已知问题。

   (1)优购手机(UOOGOU T10  Android 5.1.1) check通知权限,关闭的通知权限, 也会isNotificationEnabled函数也会返回true,check权限不准,目前在排查,有结论了再在博客添加。

   (2)小米手机部分系统跳转到通知设置页了,但是却屏蔽了开启按钮的显示。 很诡异。

            我测试的问题手机是 米五(MIUI 9 8.4.27 开发板 Android 8.0.0) 和 小米6x (MIUI 10.0 稳定版 Android 8.1.0)

            截图如下:

android 跳转到应用通知设置界面【Android 8.0 需要特殊处理】_第1张图片 正常的截图

 

android 跳转到应用通知设置界面【Android 8.0 需要特殊处理】_第2张图片 异常页面截图

如果大家说有更好地解决方案麻烦留言评论,谢谢。

更多相关文章

  1. 【转】Android --权限大全
  2. Android android 6.0权限校验及版本兼容问题
  3. Android P SystemUI下拉时,状态栏和通知栏显示位置不一致。
  4. Android 读取一个已经安装的包的权限
  5. 修改frameworks源码去掉android的下拉通知状态栏
  6. Android——《Android第一行代码》中使用通知 方法,Android8.0系
  7. 在Android java代码中如何改变文件的权限
  8. Android学习系列(2)--App自动更新之通知栏下载
  9. Android 的权限级别小记

随机推荐

  1. MySQL 转换函数与运算符
  2. MySQL数据库相关开发入门
  3. php&input复选框存储每个循环的值
  4. MySQL使用二进制日志来恢复数据
  5. windows下安装mysql5.6解压版,32位和64位
  6. Mysql的存储引擎之:MyISAM存储引擎
  7. Mysql基础之 基础知识解释
  8. 如何向mysql数据库添加多个映像?
  9. Sql查询性能限制行数之间的差异
  10. PHP分页算法详解