技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:主頁 > 教程 > 服務器類 >

Android 通知使用權(quán)(NotificationListenerService)如何使用

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2019-10-24 06:01┆點擊:

Android  通知使用權(quán)(NotificationListenerService)的使用

簡介

當下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它們能管理過濾系統(tǒng)中的一些無用消息,使得消息欄更清爽干凈。其實此功能的實現(xiàn)便是使用了Android中提供的通知使用權(quán)權(quán)限。Android4.3后加入了通知使用權(quán)NotificationListenerService,就是說當你開發(fā)的APP擁有此權(quán)限后便可以監(jiān)聽當前系統(tǒng)的通知的變化,在Android4.4后還擴展了可以獲取通知詳情信息。下面我們來看看NotificationListenerService的具體使用。

使用

新建一服務類,使它繼承NotificationListenerService,并實現(xiàn)兩個重要的方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public class NotificationListener extends NotificationListenerService { privatestatic final String TAG = "NotificationListener"; @Override public void onNotificationRemoved(StatusBarNotification sbn) { Log.i(TAG,"Notification removed"); } @Override public void onNotificationPosted(StatusBarNotification sbn) { Log.i(TAG, "Notification posted"); } }

AndroidManifest.xml中聲明此服務類,并必須聲明BIND_NOTIFICATION_LISTENER_SERVICE許可和意圖過濾器

android.service.notification.NotificationListenerService,還有我們在系統(tǒng)設置中通知使用權(quán)列表中看到的label標簽:

<serviceandroid:name=".NotificationListener" android:label="通知使用權(quán)測試程序" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <actionandroid:name="android.service.notification.NotificationListenerService"/> </intent-filter> </service>

OK,就這么簡單就可以完成APP監(jiān)聽系統(tǒng)通知欄的功能了。接下來,我們來看看NotificationListenerService類還提供了一些重要的方法:

StatusBarNotification[] sbns = getActiveNotifications(); // 返回當前系統(tǒng)所有通知的數(shù)組 cancelAllNotifications(); // 刪除系統(tǒng)中所有可被清除的通知 cancelNotification(String pkg, String tag, int id); // 刪除具體某一個通知

還有上面我們提到的兩個重要的重寫方法:

onNotificationRemoved(StatusBarNotification sbn); // 通知被移除時回調(diào) onNotificationPosted(StatusBarNotification sbn); // 增加一條通知時回調(diào)

這兩個重要的回調(diào)方法它們的參數(shù)StatusBarNotification對象是當前觸發(fā)變化通知的詳細信息。來看下StatusBarNotification的使用:

sbn.getId(); // 返回通知對應的id sbn.getNotification(); // 返回通知對象 sbn.getPackageName(); // 返回通知對應的包名 sbn.getPostTime(); // 返回通知發(fā)起的時間 sbn.getTag(); // 返回通知的Tag,如果沒有設置返回null sbn.isClearable(); // 返回該通知是否可被清楚,是否為FLAG_ONGOING_EVENT、FLAG_NO_CLEAR sbn.isOngoing(); // 返回該通知是否在正在運行的,是否為FLAG_ONGOING_EVENT

其中,getNotification()返回的通知對象,還可以詳細看到通知的其它相關(guān)信息,如:

Notification notification = sbn.getNotification(); notification.contentView; // 通知的RemoteViews notification.contentIntent; // 通知的PendingIntent notification.actions; // 通知的行為數(shù)組 // Android4.4后還擴展了可以獲取通知詳情信息 if (Build.VERSION.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) { Bundle extras = notification.extras; String notificationTitle = extras.getString(Notification.EXTRA_TITLE); int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON); Bitmap notificationLargeIcon = ((Bitmap)extras.getParcelable(Notification.EXTRA_LARGE_ICON)); CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT); }

跳轉(zhuǎn)系統(tǒng)設置里的通知使用權(quán)頁面

private boolean gotoNotificationAccessSetting(Contextcontext) { 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; } }

判斷是否擁有通知使用權(quán)

private boolean notificationListenerEnable() { boolean enable = false; String packageName = getPackageName(); String flat= Settings.Secure.getString(getContentResolver(),"enabled_notification_listeners"); if (flat != null) { enable= flat.contains(packageName); } return enable; }