Android 通知使用權(quán)(NotificationListenerService)的使用
Android 通知使用權(quán)(NotificationListenerService)的使用
簡介
當(dāng)下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它們能管理過濾系統(tǒng)中的一些無用消息,使得消息欄更清爽干凈。其實此功能的實現(xiàn)便是使用了Android中提供的通知使用權(quán)權(quán)限。Android4.3后加入了通知使用權(quán)NotificationListenerService,就是說當(dāng)你開發(fā)的APP擁有此權(quán)限后便可以監(jiān)聽當(dāng)前系統(tǒng)的通知的變化,在Android4.4后還擴(kuò)展了可以獲取通知詳情信息。下面我們來看看NotificationListenerService的具體使用。
使用
新建一服務(wù)類,使它繼承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中聲明此服務(wù)類,并必須聲明BIND_NOTIFICATION_LISTENER_SERVICE許可和意圖過濾器
android.service.notification.NotificationListenerService,還有我們在系統(tǒng)設(shè)置中通知使用權(quán)列表中看到的label標(biāo)簽:
<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(); // 返回當(dāng)前系統(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對象是當(dāng)前觸發(fā)變化通知的詳細(xì)信息。來看下StatusBarNotification的使用:
sbn.getId(); // 返回通知對應(yīng)的id sbn.getNotification(); // 返回通知對象 sbn.getPackageName(); // 返回通知對應(yīng)的包名 sbn.getPostTime(); // 返回通知發(fā)起的時間 sbn.getTag(); // 返回通知的Tag,如果沒有設(shè)置返回null sbn.isClearable(); // 返回該通知是否可被清楚,是否為FLAG_ONGOING_EVENT、FLAG_NO_CLEAR sbn.isOngoing(); // 返回該通知是否在正在運行的,是否為FLAG_ONGOING_EVENT
其中,getNotification()返回的通知對象,還可以詳細(xì)看到通知的其它相關(guān)信息,如:
Notification notification = sbn.getNotification();
notification.contentView; // 通知的RemoteViews
notification.contentIntent; // 通知的PendingIntent
notification.actions; // 通知的行為數(shù)組
// Android4.4后還擴(kuò)展了可以獲取通知詳情信息
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)設(shè)置里的通知使用權(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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android入門之Service的使用詳解
- Android?NotificationListenerService通知監(jiān)聽服務(wù)使用
- Android Google AutoService框架使用詳解
- Android使用Service實現(xiàn)IPC通信的2種方式
- 說說在Android如何使用服務(wù)(Service)的方法
- Android使用Service實現(xiàn)簡單音樂播放實例
- 淺談Android中Service的注冊方式及使用
- Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
- Android Service功能使用示例代碼
相關(guān)文章
Android Camera2 實現(xiàn)預(yù)覽功能
最近在做一些關(guān)于人臉識別的項目,需要用到 Android 相機(jī)的預(yù)覽功能。今天小編通過本文給大家分享Android Camera2 實現(xiàn)預(yù)覽功能,感興趣的朋友跟隨小編一起看看吧2018-11-11
簡單學(xué)習(xí)Android Socket的使用方法
這篇文章主要幫助大家簡單學(xué)習(xí)Android Socket的使用方法,感興趣的小伙伴們可以參考一下2016-03-03
Android XmlPullParser 方式解析 Xml 文檔
這篇文章主要介紹了Android XmlPullParser 方式解析 Xml 文檔的相關(guān)資料,需要的朋友可以參考下2017-06-06

