Android app 跳转通知设置界面,Android app是否授权开启通知

最近做一个用户开启app通知的引导的功能,需要知道用户是否开启通知,如果没开启则跳转到通知界面
//判断推送通知是否开启
public static boolean isNotifyEnabled()
 {
    NotificationManagerCompat manager =         NotificationManagerCompat.from(Cocos2dxHelper.getActivity().getApplicati        onContext());
    boolean isOpened = manager.areNotificationsEnabled();
    return isOpened;
 }

//跳转到开启推送通知
public static void openNotifyEnabled() {
    Intent localIntent = new Intent();
    try {
        //直接跳转到应用通知设置的代码:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        {
               //8.0及以上
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", instance.activity.getPackageName(), null));
        } 
        else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
        {
                //5.0以上到8.0以下
            localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            localIntent.putExtra("app_package", instance.activity.getPackageName());
                 localIntent.putExtra("app_uid", instance.activity.getApplicationInfo().uid);
        } 
        else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)         {//4.4
            localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            localIntent.addCategory(Intent.CATEGORY_DEFAULT);
            localIntent.setData(Uri.parse("package:" + instance.activity.getPackageName()));
        }
         else 
        {
            //4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
            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", instance.activity.getPackageName(), null));
                } 
                else if (Build.VERSION.SDK_INT <= 8)
                 {
                        localIntent.setAction(Intent.ACTION_VIEW);
                        localIntent.setClassName("com.android.settings",                         "com.android.setting.InstalledAppDetails");
                        localIntent.putExtra("com.android.settings.ApplicationPkgName", instance.activity.getPackageName());
                 }
        }
        instance.activity.startActivity(localIntent);
    }
catch (Exception e)
{
        //其他异常情况 进入APP设置界面
        e.printStackTrace();
        localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        localIntent.putExtra("package", instance.activity.getPackageName());
        instance.activity.startActivity(localIntent);
    }
}

你可能感兴趣的:(android,android-studio)