问题描述
我有通过preferences共享某些数据的多个应用程序。每个应用通过preferenceActitivity(从XML)设置它的preferences。两个问题:
I have multiple applications that share certain data through preferences. Each app sets its preferences through a PreferenceActitivity (from xml). Two questions:
我如何使用一个应用程序在另一个应用程序中创建/编辑preferences。如果我弄清楚如何使用preferenceActivity,将解决这个问题创造MODE_WORLD_WRITEABLE preferences。
How do I use/edit preferences created by one app in another app. If I figure out how to create MODE_WORLD_WRITEABLE preferences using PreferenceActivity that will solve the problem.
SharedPreferences prefs = getSharedPreferences(
<String referring to another package´s prefs>, MODE_WORLD_WRITEABLE);
HashMap<String, String> map = (HashMap<String, String>) prefs
.getAll();
String str = map.toString();
tv.setText(str);
以上code返回{}
Above code returns {}
- 二,我怎么使用添加preferencesFromIntent(I) - 我得到一个NullPointerException异常,即使目的不是空
感谢您提前帮助。
最佳,
萨米尔
Best,Sameer
推荐答案
要从另一个应用程序访问preferences的设置相同android:sharedUserId在清单中。这将允许您访问preferences&安培;文件MODE_PRIVATE(或安全)的方式。
To access preferences from another app in a secure way set the same android:sharedUserId in the Manifest. This will allow you to access preferences & files in MODE_PRIVATE (or secure) manner.
之后,我们意识到这不会单独工作,一个需要创建访问第二应用程序文件的第一个应用程序的一个包方面:
After much time spent, we realized this alone will not work and one needs to create a package context of the first app to access files in the second app:
try {
Context c = createPackageContext(com.app.first, MODE_PRIVATE);
SharedPreferences prefs = c.getSharedPreferences(
"com.app.first_preferences", MODE_PRIVATE);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
非常感谢你@CommonsWare和KARTHIK尚穆根的帮助!
A big thank you to @CommonsWare and Karthik Shanmugam for their help!
这篇关于Android的preferenceActivity创建不同应用程序中MODE_WORLD_WRITEABLE preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!