iOS UserDefaults Sharing Preferences Across Multiple Applications Android Equivalent

188 Views Asked by At

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.

1

There are 1 best solutions below

0
rolling_codes On BEST ANSWER

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;
    }