开发者

can't retrieve shared preference from other apps Android

i've read that shared preferences can be retrieved from outside my application, i mean if i save a preference in X app i can retrieve it from Y app, the thing is, that's not working to me, and the question is how to do it? Here's is my code, i don't know where i went wrong:

final String APP = "Test"; 
        final String USER_ID = "User Id";
        String myId;

        SharedPreferences prefs = getSharedPreferences(APP, MODE_PRIVATE);

        if (prefs.getString(USER_ID, null) == null){
            if(phoneNumber != null){
                myId = phoneNumber;
                prefs.edit().putString(USER_ID, myId).commit();
            }
            if(deviceId != null){
                myId = deviceId;
                prefs.edit().putString(USER_ID, myId).commit();
            }else{
                myId = randomId.toString();
                prefs.edit().putString(USER_ID, myId).commit();
            }
        }

Then i try to retrieve this preference from a different app like this:

final开发者_Go百科 String APP = "Test"; 
        final String USER_ID = "User Id";

        SharedPreferences sp = getSharedPreferences(APP, MODE_PRIVATE);
        String s = sp.getString(USER_ID, null);

but i only get a null object, what did i do wrong? I don't my the mistake.

Thanks in advance.


I was actually just recently looking at this, but didn't need it in the end.

This guide looked pretty good.


You are using MODE_PRIVATE instead of MODE_WORLD_READABLE

public abstract SharedPreferences getSharedPreferences (String name, int mode)

[...] mode Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. The bit MODE_MULTI_PROCESS can also be used if multiple processes are mutating the same SharedPreferences file. MODE_MULTI_PROCESS is always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions.

(getSharedPreferences)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜