我的Android应用程序有一个登录屏幕,用户登录到他的仪表板。但我不希望用户每次关闭应用程序并启动它时都会登录(除非他们从仪表板注销)。所以,我创建了一个类来检查用户是否登录
CheckLoggedIn.java
public class CheckLoggedIn extends Activity {
private boolean isLoggedIn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(!isLoggedIn){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(this, AgentHome.class);
startActivity(intent);
}
}
public boolean getStatus(){
return this.isLoggedIn;
}
public void setStatus(boolean status){
isLoggedIn = status;
}
}
当用户登录时,我会在注销时将布尔isLoggedIn
更改为true
和false
。但是当我通过成功登录并关闭它并再次启动它来检查它时,它仍然转到LoginActivity。为什么呢?
编辑:现在,我明白这是因为我在CheckLoggedIn.java的开头设置了布尔isLoggedIn
到false
。我怎样才能实现我想要做的事情?
编辑2:
Preference.java
public class Preference {
Context context;
SharedPreferences sharedPref;
public Preference(Context context){
this.context = context;
sharedPref = context.getSharedPreferences("LoginState", 0);
}
public boolean getIsLoggedIn(){
return sharedPref.getBoolean("State", false);
}
public void setIsLoggedIn(boolean state){
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("State", state);
editor.commit();
}
}
MainActivity.java
public class MainActivity extends Activity {
Preference preference = new Preference(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(preference.getIsLoggedIn()){
Log.d("State", "Already logged in");
}
......
}
}
logcat的
02-17 16:30:53.063: E/AndroidRuntime(21450): FATAL EXCEPTION: main
02-17 16:30:53.063: E/AndroidRuntime(21450): Process: collector.lbfinance, PID: 21450
02-17 16:30:53.063: E/AndroidRuntime(21450): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{collector.lbfinance/collector.lbfinance.MainActivity}: java.lang.NullPointerException
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.os.Looper.loop(Looper.java:136)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.reflect.Method.invoke(Method.java:515)
02-17 16:30:53.063: E/AndroidRuntime(21450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-17 16:30:53.063: E/AndroidRuntime(21450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-17 16:30:53.063: E/AndroidRuntime(21450): at dalvik.system.NativeStart.main(Native Method)
02-17 16:30:53.063: E/AndroidRuntime(21450): Caused by: java.lang.NullPointerException
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
02-17 16:30:53.063: E/AndroidRuntime(21450): at collector.lbfinance.library.Preference.<init>(Preference.java:13)
02-17 16:30:53.063: E/AndroidRuntime(21450): at collector.lbfinance.MainActivity.<init>(MainActivity.java:52)
02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.Class.newInstanceImpl(Native Method)
02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.Class.newInstance(Class.java:1208)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-17 16:30:53.063: E/AndroidRuntime(21450): ... 11 more
答案 0 :(得分:2)
为此,您必须使用shared preferences。您的布尔变量仅在您的活动未被销毁之前有效。要保留数据,您必须使用共享首选项或sqlite。
只有当我们有Relational数据库时,才会建议使用sqlite,对于应用程序seetings的轻量级持久存储等,共享首选项最好。
public class PreferenceForApp { Context context; SharedPreferences prefs; public PreferenceForApp(Context context) { this.context = context; prefs = context.getSharedPreferences("myAppPrefs", 0); } public Boolean getIsDeviceValidated() { return prefs.getBoolean("Validated", false); } public void setIsDeviceValidated(Boolean value) { SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("Validated", value); editor.commit(); } }
编辑
在Activity中调用此函数并以下列方式将上下文传递给它:
PreferenceForApp myPrefs= new PreferenceForApp (this);
Boolean val=myPrefs.getIsDeviceValidated();
答案 1 :(得分:0)
您可以使用SharedPreference存储凭据值。
在SharedPreference中存储值
SharedPreferences preferences = getSharedPreferences("userData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("userName", "value");
editor.putString("password", "value");
editor.commit();
从SharedPreference中检索值
SharedPreferences preferences = getSharedPreferences("loginData", 0);
String userName = preferences.getString("userName", "");
String password = preferences.getString("password", "");