android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息
NotificationListenerService是通過(guò)系統(tǒng)調(diào)起的服務(wù),在應(yīng)用發(fā)起通知時(shí),系統(tǒng)會(huì)將通知的應(yīng)用,動(dòng)作和信息回調(diào)給NotificationListenerService。但使用之前需要引導(dǎo)用戶進(jìn)行授權(quán)。使用NotificationListenerService一般需要下面三個(gè)步驟。
注冊(cè)服務(wù)
首先需要在AndroidManifest.xml對(duì)service進(jìn)行注冊(cè)。
<service
android:name=".NotificationCollectorService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
繼承實(shí)現(xiàn)NotificationListenerService
自己實(shí)現(xiàn)一個(gè)繼承NotificationListenerService的service,在onNotificationPosted中完成自己需要的操作。
public class NotificationCollectorService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("xiaolong", "open" + "-----" + sbn.getPackageName());
Log.i("xiaolong", "open" + "------" + sbn.getNotification().tickerText);
Log.i("xiaolong", "open" + "-----" + sbn.getNotification().extras.get("android.title"));
Log.i("xiaolong", "open" + "-----" + sbn.getNotification().extras.get("android.text"));
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("xiaolong", "remove" + "-----" + sbn.getPackageName());
}
}
引導(dǎo)用戶進(jìn)行授權(quán)
由于此服務(wù)需要用戶手動(dòng)進(jìn)行授權(quán),所以使用前需要對(duì)用戶進(jìn)行引導(dǎo)設(shè)置。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String string = Settings.Secure.getString(getContentResolver(),
"enabled_notification_listeners");
if (!string.contains(NotificationCollectorService.class.getName())) {
startActivity(new Intent(
"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}
}
}
用戶授權(quán)后就可以對(duì)通知欄的所有信息進(jìn)行監(jiān)聽(tīng)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中通過(guò)Notification&NotificationManager實(shí)現(xiàn)消息通知
- Android編程實(shí)現(xiàn)google消息通知功能示例
- Android之開(kāi)發(fā)消息通知欄
- Android消息通知欄的實(shí)現(xiàn)方法介紹
- Android自定義Notification添加點(diǎn)擊事件
- Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關(guān)文章
Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App
這篇文章主要介紹了Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App的實(shí)例,GridLayout比表格布局TabelLayout更容易用來(lái)制作計(jì)算器這樣的多按鈕排列的界面,需要的朋友可以參考下2016-04-04
Android自定義滑動(dòng)刪除效果的實(shí)現(xiàn)代碼
這篇文章將從現(xiàn)有 Android 滑動(dòng)刪除的痛點(diǎn),到搭建好一個(gè)基本的框架,到最終提供一份完整的 Demo為止,爭(zhēng)取為讀者提供最大的可定制化,需要的朋友可以參考下2018-03-03
36個(gè)Android開(kāi)發(fā)常用經(jīng)典代碼大全
本篇文章主要介紹了36個(gè)Android開(kāi)發(fā)常用經(jīng)典代碼片段,都是實(shí)用的代碼段,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android實(shí)現(xiàn)儀表盤(pán)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)儀表盤(pán)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android開(kāi)發(fā)全局音量調(diào)整的實(shí)現(xiàn)方式詳解
這篇文章主要為大家介紹了Android開(kāi)發(fā)全局音量調(diào)整的實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android 多線程實(shí)現(xiàn)重復(fù)啟動(dòng)與停止的服務(wù)
這篇文章主要介紹了Android 多線程實(shí)現(xiàn)重復(fù)啟動(dòng)與停止的服務(wù)的相關(guān)資料,多線程環(huán)境下為了避免死鎖,一般提倡開(kāi)放調(diào)用,開(kāi)放調(diào)用可以避免死鎖,它的代價(jià)是失去原子性,這里說(shuō)明重復(fù)啟動(dòng)與停止的服務(wù),需要的朋友可以參考下2017-08-08
Android編程實(shí)現(xiàn)二維碼的生成與解析
這篇文章主要介紹了Android編程實(shí)現(xiàn)二維碼的生成與解析方法,結(jié)合實(shí)例分析了Android二維碼的生成與讀取二維碼的相關(guān)技巧,并提供了二維碼jar包供讀者下載,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)實(shí)時(shí)滑動(dòng)ViewPager的2種方式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)實(shí)時(shí)滑動(dòng)ViewPager的2種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

