Android判断是否开启通知权限并跳转设置页面

废话不多 直接上代码:

  /**
     * 检查通知权限
     */
    private void checkNotification() {
        if (Build.VERSION.SDK_INT >= 19) {
            if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(MainActivity.this);
//                normalDialog.setIcon(R.drawable.icon_dialog);
                normalDialog.setTitle("提示");
                normalDialog.setMessage("您还没有开启通知管理权限,将无法接收到推送信息,是否开启?");
                normalDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent localIntent = new Intent();
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                if (Build.VERSION.SDK_INT >= 9) {
                                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    localIntent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
                                } else if (Build.VERSION.SDK_INT <= 8) {
                                    localIntent.setAction(Intent.ACTION_VIEW);

                                    localIntent.setClassName("com.android.settings",
                                            "com.android.settings.InstalledAppDetails");

                                    localIntent.putExtra("com.android.settings.ApplicationPkgName",
                                            MainActivity.this.getPackageName());
                                }
                                startActivity(localIntent);
                            }
                        });
                normalDialog.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                // 显示对话框
                normalDialog.show();
            }
        }
    }

参考链接>https://www.jianshu.com/p/1e27efb1dcac

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容