Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). Two 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

like image 806
Sameer Segal Avatar asked Nov 25 '25 18:11

Sameer Segal


1 Answers

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!

like image 94
Sameer Segal Avatar answered Nov 28 '25 07:11

Sameer Segal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!