我是安卓新手。我正在创建一个登录和注册页面,并将用户详细信息保存在 SQLite 中。到目前为止,我已经设法创建了登录和注册页面并保存了用户详细信息。但是每次用户启动应用程序时都会弹出登录页面,如果他已经登录,我想将用户定向到主页。我如何检查用户是否已登录,如果是,则将他定向到主页,以便他不必再次登录。
主要 Activity :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*Intent intent = new Intent(this, HomePageActivity.class);
startActivity(intent);
finish();*/
DatabaseUserDetails dbuserdeatils = new DatabaseUserDetails(getApplicationContext(),null,null,1);
startActivity(new Intent(this, dbuserdeatils.getSinlgeEntry() ? HomePageActivity.class : LoginActivity.class ));
finish();
}
}
主页 Activity :
public class HomePageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id){
case R.id.updateStatus:
//Take the user to update status activity
Intent intent = new Intent(this,UpdateStatusActivity.class);
startActivity(intent);
break;
case R.id.logoutUser:
//Logout the user
// SharedPreferences preferences = getSharedPreferences("Reg", Context.MODE_PRIVATE);
// SharedPreferences.Editor editor = preferences.edit();
// editor.commit();
finish();
// session.isUserLoggedIn()== false;
Intent in = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(in);
break;
}
return super.onOptionsItemSelected(item);
}
}
登录 Activity :
public class LoginActivity extends AppCompatActivity {
EditText mUsername;
EditText mPassword;
Button mLogin;
Button mSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Login");
mUsername=(EditText)findViewById(R.id.userName);
mPassword=(EditText)findViewById(R.id.password);
mLogin=(Button)findViewById(R.id.loginButton);
mSignUp=(Button)findViewById(R.id.signUpButton);
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String struserName = mUsername.getText().toString();
String strpassword = mPassword.getText().toString();
Log.d("onclick",strpassword);
DatabaseUserDetails dbuserdeatils = new DatabaseUserDetails(getApplicationContext(),null,null,1);
String storedPassword = dbuserdeatils.getSinlgeEntry(struserName);
Log.d("onclick",storedPassword);
if(strpassword.equals(storedPassword)) {
Toast.makeText(LoginActivity.this,"Welcome home "+struserName,Toast.LENGTH_LONG).show();
//Go to homepage
Intent intent = new Intent(LoginActivity.this, HomePageActivity.class);
startActivity(intent);
}else{
Toast.makeText(LoginActivity.this,"Username and password do not match",Toast.LENGTH_LONG).show();
}
}
});
mSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Go to Sign up page
Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(intent);
}
});
}
数据库用户详细信息:
public class DatabaseUserDetails extends SQLiteOpenHelper{
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME = "userDetailsDB.db";
private static final String TABLE = "userdeatils";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PASSWORD = "password";
public static final String COLUMN_EMAIL = "emailid";
public SQLiteDatabase db;
public DatabaseUserDetails(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_CLASS = "CREATE TABLE " + TABLE + "(" +
COLUMN_USERNAME + " " + " TEXT," +
COLUMN_PASSWORD + " " + " TEXT," +
COLUMN_EMAIL + " TEXT)";
Log.d("onCreate",CREATE_TABLE_CLASS);
db.execSQL(CREATE_TABLE_CLASS);
}
public void insertEntry(String username,String password,String emailid )
{
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, username);
values.put(COLUMN_PASSWORD,password);
values.put(COLUMN_EMAIL, emailid);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE,null,values);
Log.d("insertEntry",username);
Log.d("insertEntry",password);
Log.d("insertEntry",emailid);
db.close();
}
public String getSinlgeEntry(String userName)
{ SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor=db.query(TABLE, new String[] {COLUMN_USERNAME, COLUMN_PASSWORD}, COLUMN_USERNAME + " = ? " , new String[]{userName}, null, null, null);
/* Cursor cursor=db.query(TABLE, null, COLUMN_USERNAME + " =?", new String[]{userName}, null, null, null);*/
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD));
Log.d("getSingleEntry",password);
cursor.close();
return password;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
}
}
最佳答案
您需要使用 bool 值进行检查,
public class SessionManager {
// LogCat tag
private static String TAG = SessionManager.class.getSimpleName();
// Shared Preferences
SharedPreferences pref;
Editor editor;
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "AndroidHiveLogin";
private static final String KEY_IS_LOGGEDIN = "isLoggedIn";
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setLogin(boolean isLoggedIn) {
editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn);
// commit changes
editor.commit();
Log.d(TAG, "User login session modified!");
}
public boolean isLoggedIn(){
return pref.getBoolean(KEY_IS_LOGGEDIN, false);
}
}
有关更多信息,请参阅 this
关于android - 在 Android SQLite 中检查用户是否登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177418/