zoukankan      html  css  js  c++  java
  • Android 通过系统使用NotificationListenerService 监听各种Notification的用法

    NotificationListenerService是通过系统调起的服务,当有应用发起通知的时候,系统会将通知的动作和信息回调给NotificationListenerService。

    在继承NotificationListenerService服务实现自己逻辑之前,须要在配置文件里加入例如以下代码,获取权限。

     <service android:name=".NotificationListener"
              android:label="@string/service_name"
              android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
         <intent-filter>
             <action android:name="android.service.notification.NotificationListenerService" />
         </intent-filter>
     </service>

    这样,在系统设置中就能找到开启该服务的开关,以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开发中,使用该方法捕获手机的通知,然后同步到手表中,就是使用该服务进行的中转

  • 相关阅读:
    L1-050. 倒数第N个字符串
    全排列问题(递归)
    L2-014. 列车调度
    连通 OR 不连通(NOJ 1044)
    数三角形(codevs 3693)
    Min(BZOJ 1441)
    STL中heap用法
    军训分批(codevs 2751)
    团伙(codevs 2597)
    Subsequence(hdu 3530)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3992580.html
Copyright © 2011-2022 走看看