DoubleP90
DoubleP90

Reputation: 1249

Retrieve sharedPreferences of another application

I have an application( com.example.MyApplication ) which saves the current location like this

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

Now i would like to make another application (com.example.SecondApplication) and retrieve the city string from the other application, how can i do that?

Upvotes: 1

Views: 146

Answers (1)

DoubleP90
DoubleP90

Reputation: 1249

Finally i found a way :D I am saving my preferences like this

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

I am retrieving them like this

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"));

And important, in the manifest i've put in both applications

android:sharedUserId="example.shared"

I've put it just under android:versionName

Upvotes: 1

Related Questions