For our iOS application, we have the ability for the user to enter in simple key-value preferences that should persist in 6 other applications within the same app group. This means the user will not need to re-enter those preferences 6 more times each time they open the other applications. We do this simply by calling UserDefaults(suiteName:)
which returns a dictionary that is collectively readable/writable in all 7 iOS applications, which is quite convenient and useful. Is there an Android java equivalent of this? We don't want all of our Android users being forced to enter in the same information 7 times. I have tried creating a file that would be world readable/writable but cannot seem to find an appropriate directory that can be accessed by all 7 applications.
Asked
Active
Viewed 155 times
0

rolling_codes
- 15,174
- 22
- 76
- 112
-
Take a look at: [Data sharing between two applications](https://stackoverflow.com/q/5745243/295004) – Morrison Chang Jan 06 '20 at 19:29
-
@MorrisonChang interesting. Still won't be the easiest implementation :/ – rolling_codes Jan 06 '20 at 19:35
-
If you are trying to avoid login, you might want to check: https://developer.android.com/training/id-auth/custom_auth and https://developers.google.com/identity/smartlock-passwords/android/overview – Morrison Chang Jan 06 '20 at 19:43
-
@MorrisonChang no, I am just trying to persist preferences like `showWelcomeMessage` or `backgroundColor` for multiple applications so it does not need to be done individually in each application – rolling_codes Jan 06 '20 at 19:54
-
You might be able to use: [FileProvider](https://developer.android.com/training/secure-file-sharing/index.html) but to my knowledge there is no built-in publisher restricted sharing option in Android. – Morrison Chang Jan 06 '20 at 20:14
-
@MorrisonChang Your thread in your first comment was useful. I had to iterate through every application and update but it does the job even though it's not a preferable hack – rolling_codes Jan 06 '20 at 21:48
1 Answers
0
I had to write a hack to achieve this functionality, which was not preferred, but the requirement was necessary. I followed some of the accepted answer in this thread as suggested by Morrison Chang, but ended up having to loop through and compare to see which preference file was updated most recently and then make the updates to the app running the code, accordingly.
public static SharedPreferences getSharedPreferences(Activity activity) {
SharedPreferences prefs = activity.getApplicationContext().getSharedPreferences(SHARED_PREFS_KEY,
Context.MODE_PRIVATE);
long lastUpdate = prefs.getLong("lastUpdate", 0);
Context packageContext;
for (String app : new String[] { "app1", "app2", "app3", "app4", "app5", "app6" }) {
try {
packageContext = activity.createPackageContext(SHARED_PREFS_KEY + "." + app, 0);
SharedPreferences sharedPrefs = packageContext.getSharedPreferences(SHARED_PREFS_KEY, Context.MODE_PRIVATE);
long sharedLastUpdate = sharedPrefs.getLong("lastUpdate", 0);
if (sharedPrefs != null && sharedLastUpdate > lastUpdate) {
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
for (Map.Entry<String, ?> entry : sharedPrefs.getAll().entrySet()) {
if (entry.getValue() instanceof Boolean)
editor.putBoolean(entry.getKey(), (Boolean) entry.getValue());
else if (entry.getValue() instanceof Float)
editor.putFloat(entry.getKey(), (Float) entry.getValue());
else if (entry.getValue() instanceof Integer)
editor.putInt(entry.getKey(), (Integer) entry.getValue());
else if (entry.getValue() instanceof Long)
editor.putLong(entry.getKey(), (Long) entry.getValue());
else if (entry.getValue() instanceof String)
editor.putString(entry.getKey(), (String) entry.getValue());
}
editor.commit();
break;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return prefs;
}

rolling_codes
- 15,174
- 22
- 76
- 112