编辑:
我有一个应用程序写入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();
Run Code Online (Sandbox Code Playgroud)
和另一个必须从他们读取的应用程序
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", "") );
Run Code Online (Sandbox Code Playgroud)
但它返回空.
您认为可能是什么问题?
谢谢!
小智 7
您需要指定应用程序上下文.例如:
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");
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以阅读文件中保存的共享首选项"PREFERENCE_TAG_NAME"的内容
/data/data/YOUR_PACKAGE_NAME/shared_prefs/<PREFS_FILE>.xml
Run Code Online (Sandbox Code Playgroud)
您可以在不同的应用中执行此操作,但为此,"YOUR_PACKAGE_NAME"必须相同.
如果要更改任何应用程序中的值,您需要更改getSharedPreferences的模式:
Context.MODE_WORLD_READABLE
Run Code Online (Sandbox Code Playgroud)
至:
Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4514 次 |
| 最近记录: |