【发布时间】:2011-07-06 12:36:00
【问题描述】:
我已经读过可以从我的应用程序外部检索共享首选项,我的意思是如果我在 X 应用程序中保存一个首选项,我可以从 Y 应用程序中检索它,问题是,这对我不起作用,问题是怎么做?这是我的代码,我不知道我哪里出错了:
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();
}
}
然后我尝试从其他应用程序中检索此首选项,如下所示:
final String APP = "Test";
final String USER_ID = "User Id";
SharedPreferences sp = getSharedPreferences(APP, MODE_PRIVATE);
String s = sp.getString(USER_ID, null);
但我只得到一个空对象,我做错了什么?我不是我的错误。
提前致谢。
【问题讨论】:
-
你应该看看这里发布的答案:stackoverflow.com/questions/4787785/…
-
变量
MODE_PRIVATE似乎没有在你脑海中敲响逻辑警钟? -
有趣的解决方案,但我需要在某处保存一个 id,即使在卸载创建此 id 的应用程序后,该 id 仍将保存。有什么想法吗?
标签: android