我正在尝试检查用户是否已使用共享首选项登录到我的Android应用程序。
我有一个闪存屏幕,我尝试从共享首选项中读取userID,如果userID不存在,我将启动登录活动,否则我将重定向到主页活动。
Flash屏幕:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final int vendorId = prefs.getInt("vendorId", 0);
if (0 == vendorId) {
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(WelcomeFlashScreen.this, LoginActivity.class);
WelcomeFlashScreen.this.startActivity(mainIntent);
WelcomeFlashScreen.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
else
{
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(WelcomeFlashScreen.this, HomeActivity.class);
mainIntent.putExtra(Constants.VENDOR_ID,vendorId);
WelcomeFlashScreen.this.startActivity(mainIntent);
WelcomeFlashScreen.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
我在登录活动中如果登录成功,我有以下代码将数据填充到共享首选项。
if ("success".equals(status)&&"".equals(errorCode)) {
String vendorId = rootJsonObject.get("data").getAsJsonObject().get("id").getAsString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.edit().putString(Constants.VENDOR_ID, vendorId).commit();
loginSucces();
}
我通过代码进行了调试,发现从首选项读取/写入时没有异常,但即使将数据放入loginActivity后,我也无法从闪存屏幕活动中读取数据。
提前致谢。
答案 0 :(得分:1)
您正在使用putString(...)编写String,但尝试使用getInt(...)将其作为Int读取