package com.uiuios.aios.service; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.util.Log; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; /** * @author TT */ public class NotificationService extends NotificationListenerService { private static final String TAG = NotificationService.class.getSimpleName(); private static HashMap> notificationMap = new HashMap>(); private static Set mListener = new HashSet(); private void updateNotification() { notificationMap.clear(); StatusBarNotification[] statusBarNotifications = getActiveNotifications(); for (StatusBarNotification sbn : statusBarNotifications) { String pkg = sbn.getPackageName(); Log.e(TAG, "onListenerUpdate: " + pkg); if (notificationMap.get(pkg) == null) { notificationMap.put(pkg, new ArrayList<>()); notificationMap.get(pkg).add(sbn); } else { notificationMap.get(pkg).add(sbn); } } Log.e(TAG, "onListenerUpdate: " + notificationMap); } public static int getNotificationLength(String pkg) { if (notificationMap.get(pkg) == null) { return 0; } else { return notificationMap.get(pkg).size(); } } public static void setListener(NotificationListener listener) { mListener.add(listener); } public interface NotificationListener { void onListenerUpdate(); } @Override public void onCreate() { super.onCreate(); } @Override public void onListenerConnected() { super.onListenerConnected(); Log.e(TAG, "onListenerUpdate: " + getActiveNotifications().length); updateNotification(); for (NotificationListener listener : mListener) { listener.onListenerUpdate(); } } @Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); Log.e(TAG, "onNotificationPosted: " + getActiveNotifications().length); updateNotification(); for (NotificationListener listener : mListener) { listener.onListenerUpdate(); } } @Override public void onNotificationRemoved(StatusBarNotification sbn) { super.onNotificationRemoved(sbn); Log.e(TAG, "onNotificationRemoved: " + getActiveNotifications().length); updateNotification(); for (NotificationListener listener : mListener) { listener.onListenerUpdate(); } } }