兄弟们请收下,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); }