我想让用户在登录后不需要再次登录。我的代码不会跳过登录页面。如何跳过登录页面?我有一个起始页面。我按开始按钮,进入登录页面。但是用户已经登录,跳过登录页面并转到下一页。 我该怎么办?
JSONObject json=null;
int status=2;
String userID=null;
String teamID=null;
String teamName=null;
String admin=null;
String backNo=null;
String userName=null;
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("email",edit_txt_EmailAddress.getText().toString()));//// define the parameter
postParameters.add(new BasicNameValuePair("password",edit_txt_Password.getText().toString()));
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();
//postParameters.add(new BasicNameValuePair("userID","396797666"));
String response = null;
try {
response=CustomHttpClient.executeHttpPost("http://10.0.2.2/kwikwi/login.php", postParameters);
json=new JSONObject(response);
userID=json.getString("userID");
}
catch (Exception e) {
e.printStackTrace();
}
Log.i("USERID", userID+"");
JSONObject json_data = null;
String teamStatus ="";
String result = response.toString();
JSONObject jsonobj = null;
try{
jsonobj = new JSONObject (result);
teamStatus=jsonobj.getString("teamStatus");
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
Log.i("TeamStatus", result+"");
Log.i("RESULT", teamStatus+"");
//Log.i("halo",teamStatus+"");
String[] teamSaving=new String[6];
String[] noteamSaving=new String[2];
if (blNagSetting == true){
if(teamStatus.equals("1")){
try {
Log.i("Team Kyaw Win tal","Log in ma twar buu.");
userName=jsonobj.getString("userName");
userID=jsonobj.getString("userID");
teamID=jsonobj.getString("teamID");
teamName=jsonobj.getString("teamName");
admin=jsonobj.getString("admin");
backNo=jsonobj.getString("backNo");
teamSaving[0]=userName;
teamSaving[1]=userID;
teamSaving[2]=teamID;
teamSaving[3]=teamName;
teamSaving[4]=admin;
teamSaving[5]=backNo;
Log.i("USerName", userName+"");
Log.i("UserID",userID+"");
Log.i("TeamId",teamID+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent teamshi=new Intent(Login.this,Team.class);
teamshi.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(teamshi);
finish();
}
else if(teamStatus.equals("0"))
{
try {
Log.i("NoTeam Kyaw Win tal","Log in ma twar buu.");
userName=jsonobj.getString("userName");
userID=jsonobj.getString("userID");
noteamSaving[0]=userName;
noteamSaving[1]=userID;
Log.i("USerNameNN", userName+"");
Log.i("UserIDNN",userID+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent noteamshi=new Intent(Login.this,NoTeam.class);
noteamshi.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(noteamshi);
finish();
}
}else
{
if(teamStatus.equals("1")){
try {
Log.i("Team Win tal","Log in hmar");
userName=jsonobj.getString("userName");
userID=jsonobj.getString("userID");
teamID=jsonobj.getString("teamID");
teamName=jsonobj.getString("teamName");
admin=jsonobj.getString("admin");
backNo=jsonobj.getString("backNo");
teamSaving[0]=userName;
teamSaving[1]=userID;
teamSaving[2]=teamID;
teamSaving[3]=teamName;
teamSaving[4]=admin;
teamSaving[5]=backNo;
Log.i("USerName", userName+"");
Log.i("UserID",userID+"");
Log.i("TeamId",teamID+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editor = prefsNagSetting.edit();
editor.putBoolean(NAG_SETTING, true);
editor.commit();
Intent teamshi=new Intent(Login.this,Team.class);
teamshi.putExtra("TeamSaving", teamSaving);
startActivity(teamshi);
}
else if(teamStatus.equals("0"))
{
try {
Log.i("noTeam Win tal","Log in hmar");
userName=jsonobj.getString("userName");
userID=jsonobj.getString("userID");
noteamSaving[0]=userName;
noteamSaving[1]=userID;
Log.i("USerNameNN", userName+"");
Log.i("UserIDNN",userID+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editor = prefsNagSetting.edit();
editor.putBoolean(NAG_SETTING, true);
editor.commit();
Intent noteamshi=new Intent(Login.this,NoTeam.class);
noteamshi.putExtra("NoTeamSaving", noteamSaving);
startActivity(noteamshi);
}
else{
showAlertDialog(Login.this,"Invalid Email and Password", "Enter Valid Email and Password", false);
}
}
}
这是我的代码。
public void onClick(View arg0) {
// TODO Auto-generated method stub
isInternetPresent = conDetector.isConnectingToInternet();
Boolean save=false;
if (isInternetPresent){
isLoggedIn();
saveLoggedIn(save);
if (isLoggedIn()){
Intent team= new Intent(MainActivity.this,Team.class);
startActivity(team);
}
else{
Intent goLogin=new Intent(MainActivity.this,Login.class);
startActivity(goLogin);
}
}
else{
showAlertDialog(MainActivity.this, "No Internet Connection",
"You don't have internet connection.", false);
}
}
});
}
private boolean isLoggedIn() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
//The false represents the default value, if the variable is not stored
boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
return isLoggedIn;
}
private void saveLoggedIn(boolean value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean("isLoggedIn", value);
editor.commit();
}
这是我的编辑代码。
答案 0 :(得分:3)
在您的星标活动中,您应该验证用户是否已登录。
如何知道?
您需要一个布尔变量才能知道。持久变量。因此,当您创建开始活动时,您将放置:
if (variable){
goToLoggedActivity();
}else{
goToLogInActivity();
}
当用户登录时,您需要将此变量设置为 true
当用户退出时,您需要将此变量设置为 false
如何让它持久?
看看这些功能。
private boolean isLoggedIn() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
//The false represents the default value, if the variable is not stored
boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
return isLoggedIn;
}
private void saveLoggedIn(boolean value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean("isLoggedIn", value);
editor.commit();
}
要存储您只需要在上面调用此函数的变量。
另一个好方法,你可以看到here
答案 1 :(得分:0)
您可以在LAUNCHER ACTIVITY
中创建条件语句。使它像一个闪屏或什么的
也就是说,您可以将用户名和密码与isRememeber=true/false
一起存储在共享首选项中。当用户启动应用时,启动屏幕活动可以检查用户是否设置了isRememeber
活动是真还是假,并且可以启动MainActivity
或LoginActivity
。
这里几乎就像这个例子
Intent login=new Intent(Splashscreen.this,LoginActivity.class);
Intent Main=new Intent(Splashscreen.this,MainActivity.class);
......................
//Checking the shared preference here and storing the isRemebered value to a variable
if(isRemembered)
startActivity(Main);
else
startActivity(login);
希望这对您有所帮助,所有您需要做的就是进行一项必须在LAUNCHER活动中启动的其他活动,以获取有关启动画面的更多信息here
答案 2 :(得分:0)
用户登录后保存用户登录的SharedPreference。
当应用启动时,如果用户具有该共享首选项,则意味着您可以跳过该活动并打开登录后的活动。
要考虑的事情 - 如果有人以某种方式伪造SharedPreference会怎么样?确保它包含以后针对您的服务器验证的内容,以确保它是由真实登录生成的并且不是伪造的(甚至可以实现OAuth协议)。