检索另一个应用程序的sharedPreferences

时间:2012-08-14 19:22:03

标签: android

我有一个应用程序(com.example.MyApplication),可以像这样保存当前位置

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("City", city);
                editor.commit();

现在我想创建另一个应用程序(com.example.SecondApplication)并从其他应用程序中检索城市字符串,我该怎么做?

1 个答案:

答案 0 :(得分:1)

最后我找到了一个方法:D 我正在保存我的偏好

SharedPreferences settings = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("City", city);
                editor.commit();

我正在检索它们

Context otherAppsContext = null;
            try {
                otherAppsContext = createPackageContext("com.example.FirstApp", 0);
            } catch (NameNotFoundException e) {
            }



            SharedPreferences settings = otherAppsContext.getSharedPreferences(PREFERENCES, Context.CONTEXT_INCLUDE_CODE);
            text.setText(settings.getString("City", "nope"));

而且重要的是,我已经在两个应用程序中加入了清单

android:sharedUserId="example.shared"

我把它放在android:versionName