android app通知栏权限状态判断及跳转状态栏设置页面

原文链接: https://www.cnblogs.com/shoneworn/p/10276980.html

原文链接

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationManagerCompat;

/**

  • Created by chenxiangxiang on 2019/1/16.
    */

public class Utils {

public static boolean isPermissionOpen(Context context) {  //判断是否有打开通知栏权限
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return NotificationManagerCompat.from(context).getImportance() != NotificationManager.IMPORTANCE_NONE;
    }
    return NotificationManagerCompat.from(context).areNotificationsEnabled();
}

public static void openPermissionSetting(Context context) { //定位到当前应用通知栏位置
    try {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //直接跳转到应用通知设置的代码:
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            localIntent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            localIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
            context.startActivity(localIntent);
            return;
        }
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            localIntent.putExtra("app_package", context.getPackageName());
            localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
            context.startActivity(localIntent);
            return;
        }
        if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            localIntent.addCategory(Intent.CATEGORY_DEFAULT);
            localIntent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivity(localIntent);
            return;
        }

        //4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,

        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
            context.startActivity(localIntent);
            return;
        }

        localIntent.setAction(Intent.ACTION_VIEW);
        localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
        localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());


    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(" cxx   pushPermission 有问题");
    }
}

}

你可能感兴趣的:(Android)