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

你可能感兴趣的:(Android判断是否开启通知权限并跳转设置页面)