Android 通過系統使用NotificationListenerService 監聽各種Notification的使用方法

來源:互聯網
上載者:User

Android 通過系統使用NotificationListenerService 監聽各種Notification的使用方法

NotificationListenerService是通過系統調起的服務,當有應用發起通知的時候,系統會將通知的動作和資訊回調給NotificationListenerService。

在繼承NotificationListenerService服務實現自己邏輯之前,需要在設定檔中添加如下代碼,擷取許可權。

                     

這樣,在系統設定中就能找到開啟該服務的開關,以miui為例,在設定--安全與隱私--通知讀取許可權

該服務中有以下兩個抽象方法,是需要開發人員在使用該服務的時候實現的。

public class NotificationCollectorService extends NotificationListenerService {    @Override    public void onNotificationPosted(StatusBarNotification sbn) {                Log.i("zpf", "open"+"-----"+sbn.toString());    }    @Override    public void onNotificationRemoved(StatusBarNotification sbn) {        Log.i("zpf", "shut"+"-----"+sbn.toString());    }}
也就是說當系統發現某應用產生通知或者使用者刪除某通知,都會回調該服務的這兩個函數,函數的參數StatusBarNotification包含著該通知的具體資訊。

如果是在Android Wear開發中,使用該方法捕獲手機的通知,然後同步到手錶中,就是使用該服務進行的中轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.