Android 通知使用權(quán)(NotificationListenerService)的使用
Android 通知使用權(quán)(NotificationListenerService)的使用
簡(jiǎn)介
當(dāng)下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它們能管理過(guò)濾系統(tǒng)中的一些無(wú)用消息,使得消息欄更清爽干凈。其實(shí)此功能的實(shí)現(xiàn)便是使用了Android中提供的通知使用權(quán)權(quán)限。Android4.3后加入了通知使用權(quán)NotificationListenerService,就是說(shuō)當(dāng)你開(kāi)發(fā)的APP擁有此權(quán)限后便可以監(jiān)聽(tīng)當(dāng)前系統(tǒng)的通知的變化,在Android4.4后還擴(kuò)展了可以獲取通知詳情信息。下面我們來(lái)看看NotificationListenerService的具體使用。
使用
新建一服務(wù)類,使它繼承NotificationListenerService,并實(shí)現(xiàn)兩個(gè)重要的方法:
@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許可和意圖過(guò)濾器
android.service.notification.NotificationListenerService,還有我們?cè)谙到y(tǒng)設(shè)置中通知使用權(quán)列表中看到的label標(biāo)簽:
<serviceandroid:name=".NotificationListener"
android:label="通知使用權(quán)測(cè)試程序"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<actionandroid:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
OK,就這么簡(jiǎn)單就可以完成APP監(jiān)聽(tīng)系統(tǒng)通知欄的功能了。接下來(lái),我們來(lái)看看NotificationListenerService類還提供了一些重要的方法:
StatusBarNotification[] sbns = getActiveNotifications(); // 返回當(dāng)前系統(tǒng)所有通知的數(shù)組 cancelAllNotifications(); // 刪除系統(tǒng)中所有可被清除的通知 cancelNotification(String pkg, String tag, int id); // 刪除具體某一個(gè)通知
還有上面我們提到的兩個(gè)重要的重寫(xiě)方法:
onNotificationRemoved(StatusBarNotification sbn); // 通知被移除時(shí)回調(diào) onNotificationPosted(StatusBarNotification sbn); // 增加一條通知時(shí)回調(diào)
這兩個(gè)重要的回調(diào)方法它們的參數(shù)StatusBarNotification對(duì)象是當(dāng)前觸發(fā)變化通知的詳細(xì)信息。來(lái)看下StatusBarNotification的使用:
sbn.getId(); // 返回通知對(duì)應(yīng)的id sbn.getNotification(); // 返回通知對(duì)象 sbn.getPackageName(); // 返回通知對(duì)應(yīng)的包名 sbn.getPostTime(); // 返回通知發(fā)起的時(shí)間 sbn.getTag(); // 返回通知的Tag,如果沒(méi)有設(shè)置返回null sbn.isClearable(); // 返回該通知是否可被清楚,是否為FLAG_ONGOING_EVENT、FLAG_NO_CLEAR sbn.isOngoing(); // 返回該通知是否在正在運(yùn)行的,是否為FLAG_ONGOING_EVENT
其中,getNotification()返回的通知對(duì)象,還可以詳細(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)頁(yè)面
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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android入門(mén)之Service的使用詳解
- Android?NotificationListenerService通知監(jiān)聽(tīng)服務(wù)使用
- Android Google AutoService框架使用詳解
- Android使用Service實(shí)現(xiàn)IPC通信的2種方式
- 說(shuō)說(shuō)在Android如何使用服務(wù)(Service)的方法
- Android使用Service實(shí)現(xiàn)簡(jiǎn)單音樂(lè)播放實(shí)例
- 淺談Android中Service的注冊(cè)方式及使用
- Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能示例
- Android Service功能使用示例代碼
相關(guān)文章
Android開(kāi)發(fā)之Service用法實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之Service用法,實(shí)例分析了Android中Service的功能及使用技巧,需要的朋友可以參考下2015-05-05
安卓應(yīng)用開(kāi)發(fā)通過(guò)java調(diào)用c++ jni的圖文使用方法
這篇文章主要介紹了2013-11-11
Android垂直滾動(dòng)控件ScrollView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android垂直滾動(dòng)控件ScrollView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android數(shù)據(jù)庫(kù)Room的實(shí)際使用過(guò)程總結(jié)
這篇文章主要給大家介紹了關(guān)于Android數(shù)據(jù)庫(kù)Room的實(shí)際使用過(guò)程,詳細(xì)介紹了如何創(chuàng)建實(shí)體類、數(shù)據(jù)訪問(wèn)對(duì)象(DAO)和數(shù)據(jù)庫(kù)抽象類,需要的朋友可以參考下2025-01-01

