【发布时间】:2016-03-04 15:26:18
【问题描述】:
我有两个应用程序,App1 和 App2。我想在 App2 中使用共享首选项和访问权限将数据保存在 App1 中,反之亦然。 我可以在 App1 中保存数据并在 App2 中访问,但不能相反。
这就是我现在正在做的事情:
在清单中:
android:sharedUserId="any string"
android:sharedUserLabel="@string/any_string"
在 App1 中:
SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
在 App2 中:
try {
con = createPackageContext("com.sharedpref1", 0);
SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
String your_data =
pref.getString("demostring", "No Value");
}
catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
各位有什么线索吗?
【问题讨论】:
-
最好使用
ContentProvider。这是最好的 -
我只需要分享几个字符串。为此,最好使用 ContentProvider 吗?