一、前言
今天利用sqlite数据库实现了记住用户登录状态的功能,当用户登录以后在数据库中插入用户数据,当用户点击退出登录之后,将数据删除,在启动APP的时候对本地数据库进行查询,如果有数据,则直接进入主页面,否则进入登录页面。遇到的困难是在记住用户已登录之后,用户id参数的传导问题,通过了解query语法解决。
明天继续对细节以及一些页面进行美化改进。
二、成果
三、代码
DataBaseHelper.java
package com.androidlearing.tdtreehole.database; import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; import android.util.Log; import androidx.annotation.Nullable; /** * @ProjectName: TDTreeHole * @Package: com.androidlearing.tdtreehole.databasehelper * @ClassName: DataBaseHelper * @Description: java类作用描述 * @Author: 武神酱丶 * @CreateDate: 2020/4/23 16:44 * @UpdateUser: 更新者 * @UpdateDate: 2020/4/23 16:44 * @UpdateRemark: 更新说明 * @Version: 1.0 */ public class DataBaseHelper extends sqliteOpenHelper { private static final String DATA_BASE_NAME = "user"; private static final int VERSION = 1; private static final String TAG = "DataBaseHelper"; public DataBaseHelper(@Nullable Context context) { super(context, DATA_BASE_NAME, null, VERSION); } //创建数据库的回调 @Override public void onCreate(sqliteDatabase db) { Log.d(TAG,"创建数据库..."); String sql = "create table "+DATA_BASE_NAME+"(id integer primary key,username varchar(255),password varchar(255))"; db.execsql(sql); } //需要修改表结构时使用该方法 @Override public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { //升级数据库的回调 } }View Code
Dao.java
package com.androidlearing.tdtreehole.database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; import android.util.Log; /** * @ProjectName: TDTreeHole * @Package: com.androidlearing.tdtreehole.database * @ClassName: Dao * @Description: java类作用描述 * @Author: 武神酱丶 * @CreateDate: 2020/4/23 16:49 * @UpdateUser: 更新者 * @UpdateDate: 2020/4/23 16:49 * @UpdateRemark: 更新说明 * @Version: 1.0 */ public class Dao { private static final String TAG ="Dao" ; private final DataBaseHelper mHelper; private static final String DATA_BASE_NAME = "user"; //创建数据库 public Dao(Context context){ mHelper = new DataBaseHelper(context); } //插入数据 public void insert(int id,String username,String password){ sqliteDatabase db = mHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("id",id); values.put("username",username); values.put("password",password); db.insert(DATA_BASE_NAME,null,values); db.close(); } //查询数据 public int select(){ sqliteDatabase db = mHelper.getWritableDatabase(); int userid = 0; Cursor cursor = db.query(DATA_BASE_NAME,null,null,null,null,null,null); while (cursor.movetoNext()){ userid = cursor.getInt(cursor.getColumnIndex("id")); } cursor.close(); db.close(); return userid; } //删除数据 public void delete(){ sqliteDatabase db = mHelper.getWritableDatabase(); db.delete(DATA_BASE_NAME,null,null); db.close(); } }View Code
LunchActivity.java
package com.androidlearing.tdtreehole.activity; import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.androidlearing.tdtreehole.database.Dao; import com.androidlearing.tdtreehole.database.DataBaseHelper; import static java.lang.Thread.sleep; public class LunchActivity extends AppCompatActivity { private DataBaseHelper mDatabaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); //app启动页的加载 new Thread( new Runnable( ) { @Override public void run() { //耗时任务,比如加载网络数据 runOnUiThread(new Runnable() { @Override public void run() { // 这里可以睡几秒钟,如果要放广告的话 try { sleep(2000); } catch (InterruptedException e) { e.printstacktrace(); } } }); } } ).start(); //初始化用户的登录状态 initState(); } private void initState() { Dao dao = new Dao(this); //查询数据,如果数据库中有数据,跳转至MainActivity if (dao.select()!=0){ Intent intent = new Intent(LunchActivity.this, MainActivity.class); intent.putExtra("idKey",dao.select()); startActivity(intent); LunchActivity.this.finish(); } //如果没有查询到数据,跳转至LoginActivity else { Intent intent = new Intent(LunchActivity.this, LoginActivity.class); startActivity(intent); LunchActivity.this.finish(); } } }View Code
四、今日团队链接