1

I have 4-5 different apps and I want to keep their settings parameters common. I want to do it through sharedpref.

Please suggest how to share sharedpref among different apps without making it public for all app.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
ADattatrey
  • 252
  • 2
  • 10
  • try this http://stackoverflow.com/questions/11025234/how-can-i-share-a-sharedpreferences-file-across-two-different-android-apps Or http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application – Mohammad Tauqir Dec 14 '15 at 09:04

3 Answers3

2

using following function:

getSharedPreference(String name, int mode)

retrieves and holds content of file path with particular name.

MODE_PRIVATE: File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

MODE_WORLD_READABLE: File creation mode: allow all other applications to have read access to the created file.

MODE_WORLD_WRITEABLE : File creation mode: allow all other applications to have write access to the created file.

To access World Readable/Writeable from different app:

Context myContext = createPackageContext("com.example.app", 
Context.MODE_WORLD_READABLE); // where com.example.app is the  app containing the preferences
  SharedPreferences testPrefs = myContext.getSharedPreferences 
("test_prefs", Context.MODE_WORLD_READABLE); //test_prefs is the name in getSharePreference's argument
Community
  • 1
  • 1
Karan
  • 2,120
  • 15
  • 27
  • __"MODE_WORLD_WRITEABLE"__ and __"MODE_WORLD_READABLE"__ make it public and then any app can use this. I want to ___restrict it to only my apps___. – ADattatrey Dec 14 '15 at 12:27
  • Zubair Akbar answer is the correct one then, btw other apps should know your package name and file name too to access. Not saying its secure, but unlikely that anyone will hack in. Also, same key will make your apps run in same process too. – Karan Dec 15 '15 at 05:30
0

You could create a service that will be used by those applications, it will be managing your preferences. For service you can specify which applications can use it.

Blady214
  • 729
  • 6
  • 19
  • First I also thought the same solution.But in which app I should keep that service. There is possibility user don't install all app. If I make service in app A and user only install B,C,D,E app then this formula won't work – ADattatrey Dec 14 '15 at 09:25
  • I am not sure but you could try to keep the service in all your applications. Set the same name for the service process in manifestes and it should works. In application check if service is running and if not run it. – Blady214 Dec 14 '15 at 13:37
0

Use same key to read and write shared preference in all your apps you will get the same result as you get in app1 and in app2 ...

Zubair Akber
  • 2,760
  • 13
  • 30
  • For this I have to make sharedpref ___"MODE_WORLD_WRITEABLE"___ and then any app can use this. I want to __restrict it to only my apps__. – ADattatrey Dec 14 '15 at 09:16
  • don't worry no one knows which key you have used for your shared preference so no one can access it – Zubair Akber Dec 14 '15 at 11:41
  • I am sharing method to get all the key value from WORLD_READABLE/WRITEABLE sharedpref. Map keys = prefs.getAll(); for(Map.Entry entry : keys.entrySet()){ Log.d("map values",entry.getKey() + ": "+entry.getValue().toString()); } – ADattatrey Dec 14 '15 at 12:19
  • why are you sharing this, you know your key and you want to access it in all your apps than simply use it there – Zubair Akber Dec 14 '15 at 12:31