兄弟们请收下,001!
第一步:判断当前应用是否已经拥有通知使用权,代码如下:
//判断是否拥有通知使用权
private boolean isNotificationEnable(Context context) {
if(null == context){
return false;
}
String packageName = context.getPackageName();
String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
if (null != flat) {
return flat.contains(packageName);
}
return false;
}
第二步:跳转系统通知使用权页面,代码如下:
//跳转系统通知使用权页面
private boolean gotoNotificationAccessSettingUI(Context context) {
if(null == context){
return false;
}
try {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
try {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationAccessSettingsActivity");
intent.setComponent(cn);
intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
context.startActivity(intent);
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
}
第三步:具体调用,代码如下 :
//先判断应用是否已经开启,如果没有开启就跳转到通知使用权页面
if(!isNotificationEnable(context)){
gotoNotificationAccessSettingUI(context);
}
本文详细介绍了在Android应用中如何检查和申请通知权限的步骤,包括判断应用是否已获得通知权限,以及如何引导用户前往系统设置页面开启权限。
7697

被折叠的 条评论
为什么被折叠?



