共享首选项更新值未反映在服务中 [英] Shared Preferences updated value not reflected in Service

查看:64
本文介绍了共享首选项更新值未反映在服务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在应用程序启动时调用服务.

My application is calling a service at app launch.

在应用程序中,我正在更新给定键值对的值

In the app, I am updating the value of a given key-value pair

SharedPreferences.Editor editor = getSharedPreferences(getString(R.string.shared_preferences_name), MODE_PRIVATE).edit();
    editor.putString("key",value);
    editor.apply();

我正在尝试从服务中获取它

and I'm trying to fetch it from the service

SharedPreferences preferences = getSharedPreferences(getString(R.string.shared_preferences_name), MODE_PRIVATE);
String key = preferences.getString("key",null);

但服务总是显示旧的键值.即,该值仅在应用启动或服务重启后反映.

but the service always displays the old key value. i.e, the value is reflected only after an app launch or service restart.

如何在不重启服务或应用的情况下获取服务中更新的值?

How to fetch the updated value in the service without restarting the service or the app?

如前所述这里 共享首选项不适用于多个进程.有其他选择吗?

As mentioned here Shared Preferences don't work across multiple processes. Any alternatives?

推荐答案

如前所述 这里

注意:该类不支持跨多个进程使用.

所以它不会立即反映变化.

So it wouldn't reflect changes instantaneously.

这篇关于共享首选项更新值未反映在服务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆