亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android使用NotificationListenerService監(jiān)聽通知欄消息

 更新時間:2017年01月13日 14:54:09   作者:可樂加冰可樂  
本篇文章主要介紹了android使用NotificationListenerService監(jiān)聽通知欄消息,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

NotificationListenerService是通過系統(tǒng)調(diào)起的服務,在應用發(fā)起通知時,系統(tǒng)會將通知的應用,動作和信息回調(diào)給NotificationListenerService。但使用之前需要引導用戶進行授權(quán)。使用NotificationListenerService一般需要下面三個步驟。

注冊服務

首先需要在AndroidManifest.xml對service進行注冊。

<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>

繼承實現(xiàn)NotificationListenerService

自己實現(xiàn)一個繼承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());

  }
}

引導用戶進行授權(quán)

由于此服務需要用戶手動進行授權(quán),所以使用前需要對用戶進行引導設(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)后就可以對通知欄的所有信息進行監(jiān)聽了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論