android - 从另一个类访问共享内存
问题描述
我正在向 Firebase 侦听器类中的共享内存写入一些数据。我可以看到数据已写入,并且我能够在同一类中读取相同的数据。但是我想从我的 Firebasemessaging 服务类中读取写入共享内存的数据。
当我尝试从消息服务类中读取共享内存数据时,它没有任何以 OnChildAdded 方法写入的数据。任何帮助表示赞赏。
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;
}
}
解决方案
推荐阅读
- python - 如何使用 SQL 查询将图像保存到 odoo ir_attachment 表
- indexing - 使用 GSI 的 DynamoDB 查询的结果数据集未返回正确的结果
- python - 使用熊猫的列日期到宿舍
- excel - 让一系列 Excel 列表框控件无缝协作
- laravel-5 - Laravel 集合语法
- sql - How to obtain Multiple subtotals using SQL and VBS from Access Table?
- spring - Spring自动将OffsetDateTime序列化为时间戳
- reactjs - 如何在 ReactJS 中设置对象的状态?
- vb.net - 使用 Linq 返回不同的对象
- angular - 如何使用“d3.js”库在折线图中放置圆(点)?