37

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

6 Answers 6

55

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
@danhnn.uit Why have you put 0 in createPackageContext?
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.
MODE_WORLD_READABLE deprecated since Android 17
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()); } 

6 Comments

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
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.
I tried it but I cannot read the value from the another application
how would you update the value in the preference that is loaded from other app...?
MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE modes have been deprecated since API level 17. Since API level 24 these will throw SecurityException: developer.android.com/training/data-storage/…
|
12

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

1 Comment

How we can use in both app's manifest files. Can you please give example for the same?
12

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.

1 Comment

I've come to the conclusion that using a ContentProvider may be the only way now (since these deprecations in android 17) to share data between two apps from the same author.
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.

1 Comment

Deprecated since android 17
-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(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.