共享preferences交叉应用 [英] SharedPreferences cross applications

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

问题描述

我有2个应用程序应用程序1( com.mine.app1 ),其中我已经声明了一个共享preference

 共享preferences controlinfo = getShared preferences(销code,MODE_WORLD_WRITEABLE | MODE_WORLD_READABLE);
共享preferences.Editor编辑= controlinfo.edit();editor.putString(销code,DDDD);
editor.commit();

我有一个应用程序2( com.mine.app2 )中,我试着读出共享preference 从应用程序1。

在这里,我有以下的code(下一个按钮的onclick):

 上下文CON = createPackageContext(com.mine.app1,0);
共享preferences INTERAL preF = getShared preferences(销code,MODE_PRIVATE);应用程序2(这个程序)的//共享preF
共享preferences外部preF = con.getShared preferences(销code,MODE_PRIVATE); //应用1的共享preF
//注意:MODE_PRIVATE无所谓字符串internalPin code = INTERAL pref.getString(销code,无);
字符串externalPin code =外部pref.getString(销code,无);

在这种情况下,我得到 internalPin code externalPin code 无。

然而,当我改变的顺序 getShared preference

 上下文CON = createPackageContext(com.mine.app1,0);
共享preferences外部preF = con.getShared preferences(销code,MODE_PRIVATE); //应用1的共享preF
共享preferences INTERAL preF = getShared preferences(销code,MODE_PRIVATE);应用程序2(这个程序)的//共享preF字符串internalPin code = INTERAL pref.getString(销code,无);
字符串externalPin code =外部pref.getString(销code,无);

我这种情况下,我得到两个 internalPin code externalPin code DDDD (这是在应用程序1中设定的值( com.mine.app1 ))

我希望 internalPin code 将返回none和 externalPin code DDDD 在这两个情况。

为什么不呢?


解决方案

  

我有一个应用程序2(com.mine.app2)中,我尝试从申请1中读出的共享preference。


这是不是一个好主意。用户可以摆脱应用1时,他们希望,在这一点上应用2再也不能得到这个数据。

相反,使用同步模型。已申请1发出广播意图时preferences改变,带有附加签名级别权​​限所以只有申请2(或者你写的任何其他人)可以接收它。然后,应用程序2能更新自己的本地数据存储(例如,自己的共享preferences )。应用2可以做同样的,允许用户修改其preference数据的副本,并发送出的广播,让其他应用程序(S)知道。这样一来,如果任何一个应用程序被删除,其他应用程序(S)不要失去自己的preference数据,但一切都保持同步。

I have 2 applications Application 1 (com.mine.app1) in which I have declared a SharedPreference.

SharedPreferences controlinfo = getSharedPreferences("pincode", MODE_WORLD_WRITEABLE | MODE_WORLD_READABLE);
SharedPreferences.Editor editor = controlinfo.edit();

editor.putString("pincode", "dddd");
editor.commit();

I have an application 2 (com.mine.app2) in which I try to read out the SharedPreference from Application 1.

Here I have the following code (under onclick of a button):

Context con = createPackageContext("com.mine.app1", 0);
SharedPreferences interalPref = getSharedPreferences("pincode", MODE_PRIVATE); //shared pref of App 2 (this app)
SharedPreferences externalPref = con.getSharedPreferences("pincode", MODE_PRIVATE); //shared pref of App 1
//note: MODE_PRIVATE doesn't matter

String internalPincode = interalPref.getString("pincode", "none");
String externalPincode = externalPref.getString("pincode", "none");

In this case I get for internalPincode and externalPincode "none".

However when I change the order of getSharedPreference:

Context con = createPackageContext("com.mine.app1", 0);
SharedPreferences externalPref = con.getSharedPreferences("pincode", MODE_PRIVATE); //shared pref of App 1
SharedPreferences interalPref = getSharedPreferences("pincode", MODE_PRIVATE); //shared pref of App 2 (this app)

String internalPincode = interalPref.getString("pincode", "none");
String externalPincode = externalPref.getString("pincode", "none");

I this case I get for both internalPincode and externalPincode "dddd" (which is the value set in Application 1 (com.mine.app1))

I expect that internalPincode will return "none" and externalPincode "dddd" in both case.

Why doesn't it?

解决方案

I have an application 2 (com.mine.app2) in which I try to read out the SharedPreference from Application 1.

This is not a great idea. The user can get rid of Application 1 whenever they wish, at which point Application 2 can no longer get at this data.

Instead, use a synchronization model. Have Application 1 send out a broadcast Intent when preferences change, with an attached signature-level permission so only Application 2 (or any others that you write) can receive it. Application 2 can then update its own local data store (e.g., its own SharedPreferences). Application 2 can do the same, allowing the user to modify its copy of the preference data and sending out the broadcasts to let the other application(s) know. That way, if any one application is removed, the other application(s) do not lose their preference data, yet everything can remain in sync.

这篇关于共享preferences交叉应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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