回答

0

它将从工作:采用Android 4.3(JELLY_BEAN_MR2)。

  1. 添加到您的清单新的权限android.permission.BIND_NOTIFICATION_LISTENER_SERVICE

  2. 创建NotificationListenerService类并添加到清单。
    从谷歌开发者:

必须由NotificationListenerService要求,以确保 只有系统可以绑定到它。

的AndroidManifest.xml:

<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> 
  • 覆盖的onNotificationPosted()
  • NotificationListenerService类别:

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
    public class NotificationListenerService extends NotificationListenerService { 
    
    @Override 
    public void onNotificationPosted(final StatusBarNotification sbn) { 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
    // Here you need to play with the sbn object and get the data that you want from it. 
    
         //sbn.getPackageName() 
         Notification notification = sbn.getNotification(); 
         Bundle extras = null; 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
          extras = notification.extras; 
         } 
    
         if (extras != null) { 
          extras.get("android.title"); 
         } 
        } 
    } 
    
        @Override 
        public void onNotificationRemoved(StatusBarNotification sbn) { 
        } 
    
        @Override 
        @TargetApi(Build.VERSION_CODES.N) 
        public void onListenerDisconnected() { 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
          // Notification listener disconnected - requesting rebind 
          requestRebind(new ComponentName(this, NotificationListenerService2.class)); 
         } 
        } 
    } 
    

    开发参考:https://developer.android.com/reference/android/service/notification/NotificationListenerService.html实施

    简单示例应用:https://github.com/kpbird/NotificationListenerService-Example/