34

I have a settings application from which i have to retrieve other applications preferences, but i don't have the details of keys in them, how can i retrieve all the available keys and values in that preference?

Thanks, Swathi

ferostar
  • 7,074
  • 7
  • 38
  • 61
Swathi EP
  • 3,864
  • 6
  • 26
  • 25

6 Answers6

54

Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.

SharedPreferences prefs = getSharedPreferences("demopref",
                    Context.MODE_WORLD_READABLE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("demostring", strShareValue);
            editor.commit();

And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:

    try {
                con = createPackageContext("com.sharedpref1", 0);
                SharedPreferences pref = con.getSharedPreferences(
                        "demopref", Context.MODE_PRIVATE);
                String data = pref.getString("demostring", "No Value");
                displaySharedValue.setText(data);

            } catch (NameNotFoundException e) {
                Log.e("Not data shared", e.toString());
            }

More information please visit this URL: http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

danhnn.uit
  • 1,784
  • 1
  • 14
  • 7
  • 17
    this one is a good code: in my case, at first i didnt use the entry of the package name from the first app. but now it works. Instead of "Context.MODE_WORLD_READABLE" in App1, i tried it with " Context.MODE_PRIVATE" in combination with "android:sharedUserId" and "android:sharedUserLabel" in both AndroidManifest.xml Apps. The reason that i used this combination was that i only want access privileges only for my own developed apps. With "MODE_WORLD_READABLE" there would be also an access for other apps, which i dont want. Maybe this information is also usefull for some. – KingAlex1985 Jun 17 '14 at 09:57
  • 2
    This is a great answer, and @KingAlex1985 addition allows my paid app to read the shared preferences of it's free sibling, without exposing them to the world. Works with 'PreferenceManager.getDefaultSharedPreferences(con)' too. I added 'android:sharedUserId' and 'android:sharedUserLabel' to both apps. – Mr Chops Feb 01 '15 at 00:37
  • @danhnn.uit Why have you put `0` in `createPackageContext`? – Malwinder Singh Jun 08 '15 at 04:50
  • Additionally, in security point of view you can use an algorithm for encrypting in Application 1 then save it in Shared Preferences and then use decryption in Application 2 to access the data. This will ensure you that if in any case any other Application has access to shared preference data, then also that Application will not be able to see your sensitive data. – Divyanshu Kumar May 08 '19 at 04:56
26

Assuming the preference are WORLD_READABLE, this might work:

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
// where com.example is the owning  app containing the preferences
Context myContext = createPackageContext("com.example", Context.MODE_WORLD_WRITEABLE); 
SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE); 
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()) {
  // do something like String value = items.get(s).toString());
}
Ajay Gupta
  • 1,944
  • 2
  • 21
  • 36
jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • 2
    You do it in the other app (the app that owns the preferences). Which from you question it doesn't seem you have access to since if you did, you'd know the available keys. But you do it in a similar manner to above with mode_world_readable. it is really a very bad way of doing things, and if you want to share data you should follow commonsware suggestion – jkhouw1 May 17 '11 at 13:12
  • the application needs to have mode WORLD_READABLE - maybe thats what you meant and mis-typed it? WOLRD_WRITABLE means anyone can write to it. – jkhouw1 May 18 '11 at 09:27
  • I tried it but I cannot read the value from the another application – Mohammed Subhi Sheikh Quroush Mar 09 '13 at 20:26
  • how would you update the value in the preference that is loaded from other app...? – maverickosama92 Nov 01 '13 at 10:35
  • I allways get a NameNotFoundException for whatever I try to substitute for "com.example" – Androidcoder Apr 13 '14 at 18:20
  • 3
    MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE modes have been deprecated since API level 17. Since API level 24 these will throw SecurityException: https://developer.android.com/training/data-storage/shared-preferences#java – Eneko de la Torre Apr 21 '20 at 07:00
11

Additionally you have to add same android:sharedUserId in the both app's manifest file.

Khobaib
  • 1,577
  • 3
  • 21
  • 29
11

Unfortunately the docs now don't even explain MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE, instead saying:

This constant was depreciated in API level 17. Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, ....etc

Since the depreciation, implementing file sharing between apps with sharedpreferences may be too risky, although it was simple. I'm not too concerned with security holes from the MODE_WORLD_READABLE mode in game apps where I just want to be able to transfer characters from one app to another. It's too bad they depreciated both sharing modes.

Androidcoder
  • 4,389
  • 5
  • 35
  • 50
5

It can work if we want read perference value from other app/pkg/process. but there is something wrong in jkhouw1's answer:

Context myContext = createPackageContext("com.example", 
            Context.MODE_WORLD_WRITEABLE);

It should be :

Context myContext = createPackageContext("com.example", 
            Context.CONTEXT_IGNORE_SECURITY);

though , CONTEXT_IGNORE_SECURITY and MODE_WORLD_WRITEABLE with the same value of "int 2" At all ,thanks for this question and answers.

sakura_liu
  • 239
  • 2
  • 4
  • 7
-1

It's simple to retrieve store shared preferences data of one application to another application.

Step 1: add the same android:sharedUserId="android.uid.shared" in both app's manifest files.

Step 2: Store Value application1

 SharedPreferences preferences = context.getSharedPreferences("token_id",    Context.MODE_WORLD_READABLE);
        Editor editor = preferences.edit();
        editor.putString("shared_token", encryptedValue);
        Log.e("aaa *** shared_token : ", encryptedValue.toString());
        editor.commit();

Step 3: Get Value From application2

Context con = null;
        try {
            con = createPackageContext("application2 package name", Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        try {
            if (con != null) {
                SharedPreferences pref = con.getSharedPreferences(
                        "token_id", Context.MODE_WORLD_READABLE);

                String data = pref.getString("shared_token", "");
                Log.d("msg", "Other App Data: " + data);
            } else {
                Log.d("msg", "Other App Data: Context null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
Jaydeep Dobariya
  • 465
  • 6
  • 12