Android获取通知状态栏
2019-05-16 本文已影响0人
dashingqi
背景
- 最近在搞消息的推送,用的是腾讯的信鸽推送。用户自己是不知道去开启通知栏的,只能通过程序去提示用户去手动开启状态栏。下面的代码是我本次集成到项目中去的核心代码。经过测试 在 8.0 9.0 也是可以。
核心代码
- 获取通知状态拦开启状态
// 用来判断通知权限是否开启了
public static boolean isPermissionOpen(Context context) {
// 这是8.0 以及以后版本做的处理,来获取是否开启
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return NotificationManagerCompat.from(context).getImportance() != NotificationManager.IMPORTANCE_NONE;
}
// 8.0 一下版本获取开启状态
return NotificationManagerCompat.from(context).areNotificationsEnabled();
}
- 不同版本区间做不同的页面跳转设置
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, getPackageName());
} else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
localIntent.putExtra("app_package", getPackageName());
localIntent.putExtra("app_uid", getApplicationInfo().uid);
} else 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:" + 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", 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", getPackageName());
}
}
startActivity(localIntent);