无法从其他应用程序
EDITED读取SharedPreferences:无法从其他应用程序
我有一个应用程序,写入SharedPreferences这样的:
Context otherAppsContext = null;
try {
otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
}
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("PREFS_PRIVATE", Context.MODE_WORLD_READABLE);
Editor prefsPrivateEditor = sharedPreferences.edit();
prefsPrivateEditor.putString("layout02", jString);
prefsPrivateEditor.putString("layout02name", "Russian Layout");
prefsPrivateEditor.commit();
,具有由他们
Context otherAppsContext = null;
try {
otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
}
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("PREFS_PRIVATE", Context.MODE_WORLD_READABLE);
Log.e("name2" , "name2: "+sharedPreferences.getString("layout02name", ""));
读取另一个应用程序
但它返回空。
您认为可能是什么问题?
谢谢!
myPrefs = otherAppsContext.getSharedPreferences("NAME_OF_SHARED_PREFERENCES", Context.MODE_WORLD_READABLE);
String s = myPrefs.getString("layout02name", ""));
试试这个:
写SharedPreferences为:作为
Context otherAppsContext = null;
try {
otherAppsContext = createPackageContext("AfroKeyboard.com.rob", Context.CONTEXT_IGNORE_SECURITY);
}
catch (NameNotFoundException e) {
}
myPrefs = otherAppsContext.getSharedPreferences("NAME_OF_SHARED_PREFERENCES", Context.MODE_WORLD_READABLE);
Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("layout02", jString);
prefsEditor.putString("layout02name", "Russian Layout");
prefsEditor.commit();
读取SharedPreferences this.getSharedPreferences()
似乎没有阅读...我应该把什么,而不是PREFS_PRIVATE? – 2012-03-29 17:19:14
我已经编辑了上述问题中的代码。 – 2012-03-29 17:25:46
对不起,朋友,只是把SharedPreferences的名称,看到我的编辑ans – 2012-03-29 17:33:08
因为你是从另一个包访问SharedPreferences,你需要使用Context.createPackageContext().getSharedPreferences()
代替:
您需要指定应用程序上下文。例如:
Context otherAppsContext = null;
try
{
otherAppsContext = createPackageContext("<YOUR_PACKAGE_NAME>", Context.MODE_WORLD_READABLE);
}
catch (NameNotFoundException e)
{
}
SharedPreferences prefs = otherAppsContext.getSharedPreferences("PREFS_FILE", Context.MODE_WORLD_READABLE);
String result = prefs.getString("PREFERENCE_TAG_NAME", "DEFAULT_VALUE");
这样就可以读取共享偏好“PREFERENCE_TAG_NAME”被保存在文件
/data/data/YOUR_PACKAGE_NAME/shared_prefs/<PREFS_FILE>.xml
你可以在不同的应用程序,将工作做在的内容,但为此,“YOUR_PACKAGE_NAME”必须相同。
如果你想改变任何应用程序的价值,你将需要从改变getSharedPreferences 的模式:
Context.MODE_WORLD_READABLE
到:
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE"
感谢您的有用答案。我的问题是试图从我的独立BroadcastReceiver(称为“SMSReceiver”)访问应用程序A的SharedPreferences。 Eclipse报告“对于SMSReceiver类型,方法createPackageContext(String,int)未定义”。是否可以从广播接收器访问该方法? – PeteH 2013-02-21 00:21:25
迟,因为这是,这是我设法做到这一点的方式。
String packageName ="com.yourpackage.yourname"
Context otherAppsContext = null;
try
{
otherAppsContext = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "will crash", 3).show();
}
SharedPreferences prefsother = otherAppsContext.getSharedPreferences(keyPackageName, Context.MODE_WORLD_READABLE);
String key = prefsother.getString("key", "defValue");//use prefs normally here
'MODE_WORLD_READABLE'似乎已被弃用。如何仍然可以实现这一目标? – milkersarac 2017-02-09 07:14:01
特拉维斯,你有什么问题与我的答案? – 2012-03-29 16:47:23
似乎没有阅读...我应该把什么,而不是PREFS_PRIVATE? – 2012-03-29 17:19:27