Android通知信息不保留在Java类ArrayList中

问题描述 投票:0回答:1

我正在创建一个可在指定时间段内“捕获”所有通知,然后一次显示所有通知的应用。但是,我的NotificationListenerService Java类遇到问题。

目前,我能够在标题通过时“捕获”它们并阻止它们显示。我还能够将通知信息保留在ArrayLists中(如您在onNotificationPosted方法中看到的那样)。但是,当我尝试使用ArrayList getter之一将信息拉入另一个类时,ArrayList完全为空。关于这是为什么以及为什么不能在另一个Java类中获取此信息的任何想法?

NotificationListenerService类

public class NotificationListenerServiceUsage extends NotificationListenerService {
    private static final String TAG = "NotificationListenerSer";
    ArrayList<Integer> idMap = new ArrayList<>();
    ArrayList<Notification> notificationMap = new ArrayList<>();

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return super.onBind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn){
        Integer notificationInt = sbn.getId();
        Notification notificationContent = sbn.getNotification();
        idMap.add(notificationInt);
        notificationMap.add(notificationContent);

        cancelAllNotifications();
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn){

    }

    public ArrayList<Integer> getIdMap() {
        return idMap;
    }

    public ArrayList<Notification> getNotificationMap() {
        return notificationMap;
    }
}

实施类别

public class Batch_Notifications extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

     public void getHeldNotifications(View view){
        NotificationListenerServiceUsage noteListenerService = new NotificationListenerServiceUsage();
        ArrayList<Integer> idMap = noteListenerService.getIdMap();
        ArrayList<Notification> notificationMap = noteListenerService.getNotificationMap();
        Log.d(TAG, "getHeldNotifications: " + idMap + notificationMap);
    }
}


java android service notifications android-notifications
1个回答
0
投票
进行此类任务的最佳方法是将数据保留在持久存储即数据库中。当您尝试发送批处理通知时,将从数据库中获取信息。您可以将Sqlite数据库与Android-Room结合使用,以便于植入。

看看https://developer.android.com/training/data-storage

© www.soinside.com 2019 - 2024. All rights reserved.