登录页面不保存用户的详细信息,必须一次又一次登录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登录页面不保存用户的详细信息,必须一次又一次登录相关的知识,希望对你有一定的参考价值。
这是完整的代码。该应用程序工作正常,但我唯一面临问题的是---> 1.当用户第一次注册时,应用程序正常注册,但当我关闭应用程序并打开应用程序时,它仍显示注册窗口显示欢迎窗口。 2.当我登录时,我必须一次又一次地输入详细信息,因为它应该只登录一次..它不应该一次又一次地问。
这些都是问题所在。请有人帮我解决这个问题。
MainActivity代码
public class MainActivity extends AppCompatActivity {
SQLiteOpenHelper openHelper;
SQLiteDatabase db;
Button _btnreg, _btnlogin;
EditText _txtfname, _txtlname, _txtpass, _txtemail, _txtphone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openHelper = new DatabaseHelper(this);
_txtfname = (EditText)findViewById(R.id.txtfname);
_txtlname = (EditText)findViewById(R.id.txtlname);
_txtpass = (EditText)findViewById(R.id.txtpass);
_txtemail = (EditText)findViewById(R.id.txtemail);
_txtphone = (EditText)findViewById(R.id.txtphone);
_btnlogin=(Button)findViewById(R.id.btnlogin);
_btnreg=(Button)findViewById(R.id.btnreg);
Log.d("MainActivity","40");
_btnreg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity","50");
db=openHelper.getWritableDatabase();
String fname=_txtfname.getText().toString();
String lname=_txtlname.getText().toString();
String pass=_txtpass.getText().toString();
String email=_txtemail.getText().toString();
String phone=_txtphone.getText().toString();
insertdata(fname, lname,pass,email,phone);
Toast.makeText(getApplicationContext(), "register successfully",Toast.LENGTH_LONG).show();
Log.d("MainActivity","60");
}
});
_btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity","70");
Intent intent = new Intent(MainActivity.this, login.class);
Log.d("MainActivity","80");
startActivity(intent);
Log.d("MainActivity","+90");
}
});
}
public void insertdata(String fname, String lname, String pass, String email, String phone) {
Log.d("MainActivity","100");
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_2, fname);
contentValues.put(DatabaseHelper.COL_3, lname);
contentValues.put(DatabaseHelper.COL_4, pass);
contentValues.put(DatabaseHelper.COL_5, email);
contentValues.put(DatabaseHelper.COL_6, phone);
long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
Log.d("MainActivity","200");
}
}
databaseHelper代码
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="register.db";
public static final String TABLE_NAME="registeration";
public static final String COL_1="ID";
public static final String COL_2="FirstName";
public static final String COL_3="LastName";
public static final String COL_4="Password";
public static final String COL_5="Email";
public static final String COL_6="Phone";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("DatabaseHelper","10");
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,FirstName TEXT,LastName TEXT,Password TEXT,Email TEXT,Phone TEXT)");
Log.d("DatabaseHelper","20");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("DatabaseHelper","30");
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
Log.d("DatabaseHelper","40");
onCreate(db);
}
}
这是登录活动代码
public class login extends AppCompatActivity {
SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Button __btnLogin;
EditText __txtEmail,__txtPass;
Cursor cursor;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d("login","11");
openHelper = new DatabaseHelper(this);
db=openHelper.getReadableDatabase();
__btnLogin = (Button)findViewById(R.id.btnLogins);
__txtEmail = (EditText)findViewById(R.id.txtEmails);
__txtPass = (EditText)findViewById(R.id.txtPasss);
Log.d("login","22");
__btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = __txtEmail.getText().toString();
String pass = __txtPass.getText().toString();
if (pass == "" || email == "") {
Toast.makeText(getApplicationContext(),"No Entry", Toast.LENGTH_LONG).show();
}
Log.d("login","33");
cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_5 + " =? AND " + DatabaseHelper.COL_4 + " =? ", new String[]{email,pass});
Log.d("login","44");
if(cursor!=null) {
Log.d("login","55");
if (cursor.getCount()>0) {
Log.d("login","66");
//cursor.moveToNext();
Log.d("login","77");
startActivity(new Intent(login.this, Welcome.class));
Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
}
else {
Log.d("login","88");
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
Log.d("login","99");
}
}
}
});
}
}
谢谢
答案
把boolean
放在Pref
。在login
或register
成功之后,
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLogin", true);
editor.commit();
在Boolean
成功后改变logout
旗帜
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLogin", false);
editor.commit();
在condition
之前在MainActivity(LAUNCHER Activity)
中添加此setContentView
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (pref.getBoolean("isLogin", false))
{
//Open you Welcome Activity
finish();
}
另一答案
通过使用共享首选项
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("username_preferences", email);
editor.putString("password_preferences",password);
editor.commit();
另一答案
当用户第一次登录时,将用户数据存储到SharedPreferences中,如密码和用户名。
然后在onCreate()方法的登录活动之后读取SharedPreferences数据并将if条件放在下面
if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(userName)){
Intent intent=new Intent((this,WelcomeActivity.class));
startActivity(intent);
}
else{
}
以上是关于登录页面不保存用户的详细信息,必须一次又一次登录的主要内容,如果未能解决你的问题,请参考以下文章
托管在 Azure 上的点网 mvc 应用程序突然一次又一次地重定向到登录页面,直到重新启动