最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)用戶進(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

孟连| 沙田区| 蓬溪县| 县级市| 石景山区| 蓝田县| 温州市| 平度市| 乐安县| 皮山县| 开原市| 襄汾县| 长海县| 泗阳县| 明水县| 桐梓县| 江城| 丹阳市| 甘肃省| 尚志市| 富阳市| 根河市| 宁武县| 泽普县| 漳州市| 潮安县| 修水县| 海盐县| 安多县| 奇台县| 景德镇市| 深圳市| 临沧市| 比如县| 阳春市| 任丘市| 唐河县| 固原市| 大港区| 乌什县| 阳山县|