Android 从另一个类访问共享内存

Android 从另一个类访问共享内存,android,sharedpreferences,Android,Sharedpreferences,我正在将一些数据写入Firebase侦听器类中的共享内存。 我可以看到数据是被写入的,并且我能够在同一个类中读取相同的数据。 但我想从Firebasemessaging服务类读取写入共享内存的数据 当我尝试从消息传递服务类读取共享内存数据时,它没有任何用onchildaded方法写入的数据。 感谢您的帮助 public class MyFirebaseMessagingService extends FirebaseMessagingService { public void onMessage

我正在将一些数据写入Firebase侦听器类中的共享内存。 我可以看到数据是被写入的,并且我能够在同一个类中读取相同的数据。 但我想从Firebasemessaging服务类读取写入共享内存的数据

当我尝试从消息传递服务类读取共享内存数据时,它没有任何用onchildaded方法写入的数据。 感谢您的帮助

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage remoteMessage) {

SharedPreferences settings1 = this.getSharedPreferences("UserInfo",Context.MODE_PRIVATE);
    List<String> sampleReturnList = SharePrefUtil.pullStringList(settings1, sampleName);

    for(String listItem : sampleReturnList){
        Log.d("Item Messaging", listItem);
    }
}
}
public class FirebaseEventListener implements ChildEventListener {

private DataListAdapter userList;
private Context mContext;
public static SharedPreferences settings ;
public static String sampleName = "sample";
private String NotificationMessage;

public FirebaseEventListener(DataListAdapter userList, Context context) {
    mContext = context;
    this.userList = userList;
}

public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) 
{
SharedPreferences settings = 
mContext.getSharedPreferences("UserInfo",Context.MODE_PRIVATE);

        List<String> sampleList = new ArrayList();
     Log.d("Item Status", (String) dataSnapshot.child("7").getValue());
        if (( dataSnapshot.child("7").getValue()).equals("In Progress") )
     {

         String NotificationMessage= (String) dataSnapshot.child("0").getValue()+"-->"+(String) dataSnapshot.child("1").getValue();
            sampleList.add(NotificationMessage);

        }
SharePrefUtil.pushStringList(settings, sampleList, sampleName);

}
}

public class SharePrefUtil {


    public static void pushStringList(SharedPreferences sharedPref, List<String> list, String uniqueListName) {

        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(uniqueListName + "_size", list.size());

        for (int i = 0; i < list.size(); i++) {
            editor.remove(uniqueListName + i);
            editor.putString(uniqueListName + i, list.get(i));
        }
        editor.apply();
    }

    public static List<String> pullStringList(SharedPreferences sharedPref,
                                              String uniqueListName) {

        List<String> result = new ArrayList<>();
        int size = sharedPref.getInt(uniqueListName + "_size", 0);

        for (int i = 0; i < size; i++) {
            result.add(sharedPref.getString(uniqueListName + i, null));
        }
        return result;
    }

}
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
收到消息时公共无效(RemoteMessage RemoteMessage){
SharedReferences设置1=this.getSharedReferences(“UserInfo”,Context.MODE\u PRIVATE);
List sampleReturnList=SharePrefUtil.pullStringList(设置1,sampleName);
用于(字符串列表项:sampleReturnList){
Log.d(“项目消息传递”,listItem);
}
}
}
公共类FirebaseEventListener实现ChildEventListener{
私有数据列表适配器用户列表;
私有上下文;
公共静态SharedReferences设置;
公共静态字符串sampleName=“sample”;
私有字符串通知消息;
公共FirebaseEventListener(DataListAdapter用户列表,上下文){
mContext=上下文;
this.userList=userList;
}
公共void onChildAdded(DataSnapshot DataSnapshot,字符串previousChildName)
{
SharedReferences设置=
mContext.getSharedReferences(“UserInfo”,Context.MODE\u PRIVATE);
List sampleList=new ArrayList();
Log.d(“项状态”,(字符串)dataSnapshot.child(“7”).getValue();
if((dataSnapshot.child(“7”).getValue()).equals(“进行中”))
{
字符串NotificationMessage=(字符串)dataSnapshot.child(“0”).getValue()+“-->”+(字符串)dataSnapshot.child(“1”).getValue();
sampleList.add(NotificationMessage);
}
pushStringList(设置、sampleList、sampleName);
}
}
公共类SharePrefUtil{
公共静态void pushStringList(SharedReferences SharedRef,List List,String uniqueListName){
SharedPreferences.Editor=sharedPref.edit();
editor.putInt(uniqueListName+“_size”,list.size());
对于(int i=0;i
1)显示您的SharePrefUtil类。2)我没有看到您在阅读prefsI。我已经添加了代码AntonI没有在您的代码中看到pushStringList。我已经添加了。我错过了添加初始消息。我的意思是你没有在代码中调用它