Search code examples
androidsharedpreferencesjava-package

SharedPreferences managing between different packages


I'm developing one app, with the package com.example1 and I'm doing another module to use as a library with the package com.example2.

On my module (com.example2) I'm using this static functions:

    public static void saveLastDownload(Activity mActivity, long numberLong) {
        SharedPreferences sharedPref = mActivity.getPreferences(Context.MODE_APPEND);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putLong(dataLastDownloadKey, numberLong);
        boolean commit = editor.commit();
    }

    public static long readLastDownload(Activity mActivity, long defaultValue) {
        SharedPreferences sharedPref = mActivity.getPreferences(Context.MODE_PRIVATE);
        return sharedPref.getLong(dataLastDownloadKey, defaultValue);
    }

I'm trying to saveDownload and readLastDownload from com.example1 but I haven't got any value on these shared preferences. How can I read this value in a modern way (without any security holes, although I'm not interested in making it private).

How could I do it?

Thank you very much in advance.


Solution

  • If you want to do this with preferences : MODE_WORLD_READABLE

    Creating world-writable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and Service.