version:2.0

fix:
update:通知变化时更新
This commit is contained in:
2022-06-08 09:29:55 +08:00
parent 190810bdc0
commit f954fc036d
13 changed files with 630 additions and 218 deletions

View File

@@ -6,7 +6,9 @@ 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
@@ -15,14 +17,14 @@ public class NotificationService extends NotificationListenerService {
private static final String TAG = NotificationService.class.getSimpleName();
private static HashMap<String, List<StatusBarNotification>> notificationMap = new HashMap<String, List<StatusBarNotification>>();
private static NotificationListener mListener;
private static Set<NotificationListener> mListener = new HashSet<NotificationListener>();
private void updateNotification() {
notificationMap.clear();
StatusBarNotification[] statusBarNotifications = getActiveNotifications();
for (StatusBarNotification sbn : statusBarNotifications) {
String pkg = sbn.getPackageName();
Log.e(TAG, "onListenerConnected: " + pkg);
Log.e(TAG, "onListenerUpdate: " + pkg);
if (notificationMap.get(pkg) == null) {
notificationMap.put(pkg, new ArrayList<>());
notificationMap.get(pkg).add(sbn);
@@ -30,7 +32,7 @@ public class NotificationService extends NotificationListenerService {
notificationMap.get(pkg).add(sbn);
}
}
Log.e(TAG, "onListenerConnected: " + notificationMap);
Log.e(TAG, "onListenerUpdate: " + notificationMap);
}
public static int getNotificationLength(String pkg) {
@@ -42,15 +44,11 @@ public class NotificationService extends NotificationListenerService {
}
public static void setListener(NotificationListener listener) {
mListener = listener;
mListener.add(listener);
}
public interface NotificationListener {
void onListenerConnected();
void onNotificationPosted(StatusBarNotification sbn);
void onNotificationRemoved(StatusBarNotification sbn);
void onListenerUpdate();
}
@Override
@@ -61,10 +59,11 @@ public class NotificationService extends NotificationListenerService {
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.e(TAG, "onListenerConnected: " + getActiveNotifications().length);
Log.e(TAG, "onListenerUpdate: " + getActiveNotifications().length);
updateNotification();
if (mListener != null)
mListener.onListenerConnected();
for (NotificationListener listener : mListener) {
listener.onListenerUpdate();
}
}
@Override
@@ -72,8 +71,9 @@ public class NotificationService extends NotificationListenerService {
super.onNotificationPosted(sbn);
Log.e(TAG, "onNotificationPosted: " + getActiveNotifications().length);
updateNotification();
if (mListener != null)
mListener.onNotificationPosted(sbn);
for (NotificationListener listener : mListener) {
listener.onListenerUpdate();
}
}
@Override
@@ -81,8 +81,9 @@ public class NotificationService extends NotificationListenerService {
super.onNotificationRemoved(sbn);
Log.e(TAG, "onNotificationRemoved: " + getActiveNotifications().length);
updateNotification();
if (mListener != null)
mListener.onNotificationRemoved(sbn);
for (NotificationListener listener : mListener) {
listener.onListenerUpdate();
}
}