在相同的应用程序中同时preferenceActivity和服务管理共享preferences [英] Managing SharedPreferences in both PreferenceActivity and Service within same app

查看:230
本文介绍了在相同的应用程序中同时preferenceActivity和服务管理共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立与一个应用程序的 preferenceActivity 的和的服务的(在其自己的远程过程中因的运行Android:工艺=:远程的标志),并都需要以编程方式访问的共享preferences 的。在present一次我得到一个的共享preference 的与使用的 getShared preferences 的两个定义的方法如下code对象的服务的和的 preferenceActivity 的类:

I'm building an app with a PreferenceActivity and a Service (running in its own remote process due to android:process=":remote" flag) and both need to access programmatically the SharedPreferences. At present time I'm getting a SharedPreference object with the following code that uses the getSharedPreferences method defined in both Service and PreferenceActivity classes:

SharedPreferences sharedPrefs = getSharedPreferences("com.mypackage_preferences", MODE_PRIVATE, MODE_MULTI_PROCESS)

这是一样的下面?

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences()

1)在第二个我不能指定标志,还是有指定它们的方法吗?

1) In the second one I can not specify flags, or is there a way to specify them?

2)我是否真的需要,因为我访问/来自修改共享preferences指定MODE_MULTI_PROCESS 服务的和的 preferenceActivity 的?我认为,大部分的时间你有一个的 preferenceActivity 的可修改的属性和其他的活动的读取/修改它们,所以MODE_MULTI_PROCESS需要几乎总是或者是只有当有多个,可能并发访问到所需的共享preferences 的(在我的情况下,后台服务,而你在的 preferenceActivity 的)

2) Do I really need to specify MODE_MULTI_PROCESS since I'm accessing/modifying the shared preferences from both Service and PreferenceActivity? I think that most of the time you have a PreferenceActivity that modifies the properties and another Activity that reads/modifies them, so is MODE_MULTI_PROCESS needed almost always or is it only needed when there are multiple and possibly concurrent accesses to the SharedPreferences (as in my case, with the background service that can be active while you're editing preferences in PreferenceActivity)

3)的的<一个文档href=\"http://developer.android.com/reference/android/content/Context.html#MODE_MULTI_PROCESS%20MODE_MULTI_PROCESS\"相对=nofollow> MODE_MULTI_PROCESS 说的这种行为有时需要在应用程序有多个进程的情况下,全部写入同一个共享preferences文件的,这是否意味着我的的服务的和我的 preferenceActivity 的可以指向不同的preference文件?你如何改变preference文件名中的 preferenceActivity 的?

3) The documentation of MODE_MULTI_PROCESS says This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file, does this mean that my Service and my PreferenceActivity can point different preference files? How do you change the preference file name in a PreferenceActivity?

4)共享preferences以这种方式命名,因为他们是你的应用程序的所有组件(活动,服务等)之间共享的,对不对?非共享preferences(即那些可以用的的get preferences()的在你的活动获得)是本地创建它们的组件,但被保存在同一个*融为一体。 mypackage_ preferences *文件?这可以回答我的疑惑点的 3)

4) Shared preferences are named in this way because they are shared among all the components (activities, services etc) of your application, right? Non shared preferences (i.e. the ones you can get with getPreferences() in you activity) are local to the component that creates them but are being saved in the same *com.mypackage_preferences* file? This could answer to my doubts in point 3)

推荐答案

您只需要 MODE_MULTI_PROCESS 如果你正在访问来自不同的进程preferences 。如果你只是有使用共享preFS那么不同的活动,你不需要 MODE_MULTI_PROCESS

You only need MODE_MULTI_PROCESS if you are accessing the preferences from different processes. If you just have different activities that are using the shared prefs then you don't need MODE_MULTI_PROCESS.

您提到,您有服务。只要服务在同一个进程中的活动仍然不需要 MODE_MULTI_PROCESS 运行。该服务将在默认情况下在同一个进程中运行,除非你指定安卓过程=...&LT;服务&GT; 标签中的清单。

You mention that you have a service. As long as the service is running in the same process as the activities you still don't need MODE_MULTI_PROCESS. The service will run by default in the same process unless you have specified android:process="..." in the <service> tag in the manifest.

这篇关于在相同的应用程序中同时preferenceActivity和服务管理共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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