Android PreferenceActivity to create a MODE_WORLD_WRITEABLE preference across applications
I have multiple applications that share certain data through preferences. Each app sets its preferences through a PreferenceActitivity (from xml). Tw开发者_如何转开发o questions:
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);
Above code returns {}
- Second, how do I use addPreferencesFromIntent(i) -- I am getting a NullPointerException even though the intent is not Null.
Thanks for the help in advance.
Best, Sameer
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();
}
A big thank you to @CommonsWare and Karthik Shanmugam for their help!
精彩评论