共享首选项更新值未反映在服务中

编程入门 行业动态 更新时间:2024-10-25 04:17:14
本文介绍了共享首选项更新值未反映在服务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

共享首选项更新值未反映在服务中

本文发布于:2023-11-10 06:32:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:首选

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!