SharedPreferences Between Applications

September 27, 2012 Zaidi Android Development Tutorials

On my last post, I wrote about SharedPreferences Between Activities and how one activity can retrieve persistence data saved by another activity on the same application. But what if an application needs to access some sort of data stored in SharedPreferences from a totally different application? That’s what I will cover today.

Also, I will touch on something I left out in my last post, that’s mode parameters that you should pass when creating SharedPreferences objects. It will be something short and straight forward for everyone who wants to implement it.

Starting with SharedPreferences between applications, one application can access preferences from another application simply by referencing the first application package name plus a proper mode parameter. Lets say PackageTwo needs to access a preferences from PackageOne. Assuming that the first application preference was defined as a constant with a PREF_NAME name, this is how to access it.

 

Now, the second application can store or retrive values from PackageOne using the secondPreference object.

 

So far I have passed MODE_PRIVATE on all the SharedPreferences objects I have created. If MODE_PRIVATE or 0 is passed as parameter, the read and write access will only be allowed to the application that created the preference.

There are two more parameters that can be passed which are MODE_WORLD_READABLE or 1 and MODE_WORLD_WRITABLE or 2. Both modes give shared preferences access between applications. Depending on how your application is going to share data, be certain that a proper mode is passed.

mode_privatemode_world_readablemode_world_writableshared preferences between applicationssharedpreferences mode parameter


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Powered by WordPress and HQ Premium Themes.