记录开发中通知权限使用
public class NotificationAccess {
private static final String TAG = "NotificationAccess" ;
public static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners" ;
private static final HashSet< ComponentName> mEnabledListeners = new HashSet < > ( ) ;
@RequiresPermission ( allOf = { Manifest. permission. WRITE_SETTINGS, Manifest. permission. WRITE_SECURE_SETTINGS} )
public static void enableNotificationAccess ( Context context, String packageName, String serviceName) {
if ( isAccessibilityEnabled ( context) ) {
LogUtil. d ( TAG, "enableNotificationAccess: the accessibility has been enabled" ) ;
return ;
}
if ( Build. VERSION. SDK_INT > Build. VERSION_CODES. O) {
NotificationManager manager = ( NotificationManager) context. getSystemService ( Context. NOTIFICATION_SERVICE) ;
try {
Method m_setNotificationListenerAccessGranted = manager. getClass ( )
. getDeclaredMethod ( "setNotificationListenerAccessGranted" , ComponentName. class , boolean . class ) ;
m_setNotificationListenerAccessGranted. setAccessible ( true ) ;
m_setNotificationListenerAccessGranted. invoke ( manager, new ComponentName ( packageName, serviceName) , true ) ;
} catch ( NoSuchMethodException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
} else {
loadEnabledListener ( context) ;
mEnabledListeners. add ( new ComponentName ( packageName, serviceName) ) ;
StringBuilder stringBuilder = null;
for ( final ComponentName componentName : mEnabledListeners) {
if ( stringBuilder == null) {
stringBuilder = new StringBuilder ( ) ;
} else {
stringBuilder. append ( ":" ) ;
}
stringBuilder. append ( componentName. flattenToShortString ( ) ) ;
}
Settings. Secure. putString ( context. getContentResolver ( ) ,
ENABLED_NOTIFICATION_LISTENERS, stringBuilder != null ? stringBuilder. toString ( ) : "" ) ;
}
}
private static boolean isAccessibilityEnabled ( Context context) {
String notificationList = Settings. Secure. getString ( context. getContentResolver ( ) , ENABLED_NOTIFICATION_LISTENERS) ;
if ( ! TextUtils. isEmpty ( notificationList) ) {
final String[ ] names = notificationList. split ( ":" ) ;
for ( final String name : names) {
ComponentName componentName = ComponentName. unflattenFromString ( name) ;
if ( componentName != null) {
if ( TextUtils. equals ( componentName. getPackageName ( ) , context. getPackageName ( ) ) ) {
return true ;
}
}
}
}
return false ;
}
private static void loadEnabledListener ( Context context) {
mEnabledListeners. clear ( ) ;
String notificationList = Settings. Secure. getString ( context. getContentResolver ( ) , ENABLED_NOTIFICATION_LISTENERS) ;
if ( ! TextUtils. isEmpty ( notificationList) ) {
final String[ ] names = notificationList. split ( ":" ) ;
for ( final String name : names) {
ComponentName componentName = ComponentName. unflattenFromString ( name) ;
if ( componentName != null) {
mEnabledListeners. add ( componentName) ;
}
}
}
}
public static void toOpenNotificationListenAccessManually ( Context context) {
try {
Intent intent;
if ( Build. VERSION. SDK_INT >= Build. VERSION_CODES. LOLLIPOP_MR1) {
intent = new Intent ( Settings. ACTION_NOTIFICATION_LISTENER_SETTINGS) ;
} else {
intent = new Intent ( "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS" ) ;
}
context. startActivity ( intent) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}