Question

I am using shared preferences in my Android application to store some data. I want this data to be available to other apps, but only ones that I have made.

(I am aware the data will be available to anyone who has rooted their phone no matter what I do)

After extensive searching I have found 2 possible ways of doing this.

  1. Set shared prefs mode to private. Use a shareduserid and sign all apps with the same key. Then use the original apps context to access the private shared preferences. Sort of shown here however very old and not sure if still viable as I cannot get it to work.

  2. Use a content provider to access the shared preferences.- However the problem with this is that I cant find a single tutorial on how to do this using shared prefs. Everyone seems to use sqlite database. Im struggling to adapt this to use shared prefs as the query function seems to require use of cursors, which shared prefs do not use.

Was it helpful?

Solution

And so I have worked it out! if anyone else ever finds this useful..here we go:

I continued working on Option 1 (See question) and found the 2 conditions to be satisfied to allow apps to share resources.

  1. SharedUserID

    In the Manifest I put the following lines under the tag

            android:sharedUserLabel="@string/id"
            android:sharedUserId="com.example"
    

    And in the string id I put "samsid" - Can be anything so long as its the same for both apps.

  2. Apps signed with same key

    Eclipse uses the same debugger key when debugging your app so no need to worry about this until you put app on the play store.

To access the other apps resources via context I used the following:

    try {
 otherAppsContext = createPackageContext("com.example.samstest", 0);

        } catch (NameNotFoundException e) {} 

Where "com.example.samstest is the app that setup the shared prefs. Then used "otherAppsContext" when making a shared preferences object in order to retrieve data the usual way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top