共享同一应用程序的两个过程之间preferences [英] Shared preferences between two processes of the same application

查看:106
本文介绍了共享同一应用程序的两个过程之间preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Android应用程序,包含的UI和运行独立的进程。 我想分享的主要过程,并且是在一个不同的进程中执行我的应用程序中定义的服务之间的简单信息。 我发现令人眼花缭乱的使用AIDL进行进程间通信用于此目的。

I'm writing an Android app that contains both UI and separate processes running. I want to share simple information between the main process and a service defined in my application that is executed in a different process. I find to messy to use AIDL for inter-process communication for this purpose.

现在的问题是:是否可以安全使用共享$ P $申请pferences这两个进程之间的通信?这就是:读取和写入相同的共享preferences

The question is: Is it safe to use Shared Preferences of the application for communicating between this two processes? This is: both read and write the same shared preferences.

我想知道,如果它的实际工作。在Android开发者参考有关共享preferences(http://developer.android.com/reference/android/content/Shared$p$pferences.html)他们的状态:注:目前这一类不支持使用在多个流程。这将在以后添加。的,但我不知道究竟这是什么意思。

I'm wondering if it actually works. In android developers reference about shared preferences (http://developer.android.com/reference/android/content/SharedPreferences.html) they state: Note: currently this class does not support use across multiple processes. This will be added later. but I don't know exactly what does this mean.

感谢您的帮助

推荐答案

在Android的< 2.3它的工作原理。一过程可以写的变化和其他过程可以读取的更改。在code读取/写入共享preferences文件(他们实际上是存储在文件中)检查是否有过之前的读/写文件所做的任何修改,他们相应地更新其缓存版本。

In Android < 2.3 it works. One process can write changes and the other process can read the changes. The code to read/write shared preferences files (they are actually stored in files) checks if there have been any changes made to the file before reading/writing and they update their cached version accordingly.

在安卓> 2.3它的工作原理,但你需要专门设置 MODE_MULTI_PROCESS getShared preferences()

In Android > 2.3 it works, but you need to specifically set MODE_MULTI_PROCESS when calling getSharedPreferences().

在Android 2.3的它被打破,它不工作: - (

In Android 2.3 it is broken and it doesn't work :-(

这篇关于共享同一应用程序的两个过程之间preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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