欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

NotificationListenerService是通過(guò)系統(tǒng)調(diào)起的服務(wù),在應(yīng)用發(fā)起通知時(shí),系統(tǒng)會(huì)將通知的應(yīng)用,動(dòng)作和信息回調(diào)給NotificationListenerService。但使用之前需要引導(dǎo)用戶(hù)進(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)用戶(hù)進(jìn)行授權(quán)

由于此服務(wù)需要用戶(hù)手動(dòng)進(jìn)行授權(quán),所以使用前需要對(duì)用戶(hù)進(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"));
    }
  }
}

用戶(hù)授權(quán)后就可以對(duì)通知欄的所有信息進(jìn)行監(jiān)聽(tīng)了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論