我只在一次登录时遇到麻烦...我的目标是第一个用户获得登录屏幕..如果他是新用户,他将注册然后登录...从那时用户启动应用程序时他应该直接重定向到跳过登录页面的主要活动。请朋友们帮助我解决这个问题..请给我发布任何教程或任何代码...请告诉我如何在清单文件中修改...
我在登录活动中使用这样的东西,但我没有完成任务。
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
editor.putString("register","true");
editor.commit();
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true"))
// redirect to next activity
else
// show registration page again
答案 0 :(得分:7)
以这种方式实施SharedPreferences
:
Boolean isFirstTime;
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(Splash.this);
SharedPreferences.Editor editor = app_preferences.edit();
isFirstTime = app_preferences.getBoolean("isFirstTime", true);
if (isFirstTime) {
//implement your first time logic
editor.putBoolean("isFirstTime", false);
editor.commit();
}else{
//app open directly
}
答案 1 :(得分:5)
在这里查看
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
Android应用程序中会话管理的一个很好的例子。
答案 2 :(得分:2)
使用SharedPreferences. contains告诉SharedPreferences中是否存在密钥。将您的代码更改为:
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
if(pref.contains("register"))
{
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true")){
redirect to next activity
}else{
//first time
editor.putString("register","true");
editor.commit();
/// show registration page again
}
}
else{ //first time
editor = pref.edit();
editor.putString("register","true");
editor.commit();
/// show registration page again
}
答案 3 :(得分:0)
您可以访问我的博客
http://upadhyayjiteshandroid.blogspot.in/2013/01/android-working-with-shared-preferences.html
希望你能得到答案并清楚地理解
Boolean flag;
SharedPreferences applicationpreferences = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = applicationpreferences .edit();
flag = applicationpreferences .getBoolean("flag", false);
if (flag) {
///second time activity
}else{
//first time
editor.putBoolean("flag", true);
editor.commit();
}
答案 4 :(得分:0)
查看Session Management in Android,其中显示了如果用户已登录应用程序,您将如何管理登录。并相应地切换用户。
希望这会对你有所帮助。
答案 5 :(得分:0)
1.存储在存储的首选项中使用此
SharedPreferences.Editor editor = getSharedPreferences("DeviceToken",MODE_PRIVATE).edit();
editor.putString("DeviceTokenkey","ABABABABABABABB12345");
editor.apply();
2.检索相同用途
SharedPreferences prefs = getSharedPreferences("DeviceToken",
MODE_PRIVATE);
String deviceToken = prefs.getString("DeviceTokenkey", null);