本篇文章給大家分享的是有關(guān)怎么在Android中使用SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、臨夏州網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、購(gòu)物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為臨夏州等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
XML文件
Java文件
package com.example.application; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; import com.sun.util.DBHelper; import java.util.HashMap; import cn.sharesdk.framework.Platform; import cn.sharesdk.framework.PlatformActionListener; import cn.sharesdk.framework.ShareSDK; import cn.sharesdk.sina.weibo.SinaWeibo; public class MainActivity extends AppCompatActivity { public static String LoginUid=null; public static String LoginName=null; private Button login_login; private Button login_find; private EditText login_uname; private EditText login_upass; private CheckBox login_auto; private CheckBox login_btn; private SharedPreferences sp; private ImageButton login_xinlan; private Platform weibo; private DBHelper dbHelper; private SQLiteDatabase sqLiteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //獲取控件 login_uname = (EditText) findViewById(R.id.login_uname); login_upass = (EditText) findViewById(R.id.login_upass); login_auto = (CheckBox) findViewById(R.id.login_auto); //記住密碼 login_btn = (CheckBox) findViewById(R.id.login_btn); //自動(dòng)登錄 login_login = (Button) findViewById(R.id.login_login); //登錄 login_find = (Button) findViewById(R.id.login_find); login_xinlan = (ImageButton) findViewById(R.id.login_xinlan); //調(diào)用數(shù)據(jù)庫(kù) dbHelper = new DBHelper(this,"dtb.db",null,1); sqLiteDatabase = dbHelper.getWritableDatabase(); //第三方登錄 weibo = ShareSDK.getPlatform(SinaWeibo.NAME); login_xinlan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //回調(diào)信息,可以在這里獲取基本的授權(quán)返回的信息,但是注意如果做提示和UI操作要傳到主線程handler里去執(zhí)行 weibo.setPlatformActionListener(new PlatformActionListener () { @Override public void onComplete(Platform platform, int i, HashMaphashMap) { String openid = platform.getDb().getUserId(); String nickname = platform.getDb().getUserName(); Cursor cursor=sqLiteDatabase.rawQuery("select * from dtb_users where uname=?",new String[]{openid}); LoginUid=cursor.getString(cursor.getColumnIndex("uid")); if(cursor.moveToNext()){ Log.i("test","已經(jīng)注冊(cè)過(guò)!"); }else{ sqLiteDatabase.execSQL("insert into dtb_users(uname,upass,name,levelnumber) values('"+openid+"','null','"+nickname+"','"+1+"')"); } //跳轉(zhuǎn) MainActivity.LoginName=openid; Intent intent=new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); } @Override public void onError(Platform arg0, int arg1, Throwable arg2) { // TODO Auto-generated method stub arg2.printStackTrace(); } @Override public void onCancel(Platform arg0, int arg1) { // TODO Auto-generated method stub } }); //authorize與showUser單獨(dú)調(diào)用一個(gè)即可 weibo.authorize();//單獨(dú)授權(quán),OnComplete返回的hashmap是空的 weibo.showUser(null);//授權(quán)并獲取用戶信息 //移除授權(quán) // weibo.removeAccount(true); } }); //自動(dòng)登錄判斷 sp = this.getSharedPreferences("userInfo",0); String name=sp.getString("USER_NAME", ""); String pass =sp.getString("PASSWORD", ""); boolean choseRemember =sp.getBoolean("remember", false); boolean choseAutoLogin =sp.getBoolean("autologin", false); //如果上次選了記住密碼,那進(jìn)入登錄頁(yè)面也自動(dòng)勾選記住密碼,并填上用戶名和密碼 if(choseRemember){ login_uname.setText(name); login_upass.setText(pass); login_auto.setChecked(true); } //如果上次登錄選了自動(dòng)登錄,那進(jìn)入登錄頁(yè)面也自動(dòng)勾選自動(dòng)登錄 if(choseAutoLogin){ login_btn.setChecked(true); Cursor cursor= sqLiteDatabase.rawQuery("select * from dtb_users where uname=? and upass=?",new String[]{name,pass}); if(cursor.moveToNext()){ new LoginThread().start(); LoginName=name; LoginUid=cursor.getString(cursor.getColumnIndex("uid")); } } // 登錄監(jiān)聽(tīng)事件 現(xiàn)在默認(rèn)為用戶名為:admin 密碼:123 login_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName=login_uname.getText().toString(); String userPass=login_upass.getText().toString(); SharedPreferences.Editor editor =sp.edit(); Cursor cursor= sqLiteDatabase.rawQuery("select * from dtb_users where uname=? and upass=?",new String[]{userName,userPass}); if(cursor.moveToNext()){//判斷是否查詢到此數(shù)據(jù) Toast.makeText(MainActivity.this,"登錄成功", Toast.LENGTH_SHORT).show(); LoginName=userName; LoginUid=cursor.getString(cursor.getColumnIndex("uid")); //是否記住密碼 //記住用戶名、密碼、 editor.putString("USER_NAME", userName); editor.putString("PASSWORD",userPass); if(login_auto.isChecked()){ editor.putBoolean("remember", true); }else{ editor.putBoolean("remember", false); } //是否自動(dòng)登錄 if(login_btn.isChecked()){ editor.putBoolean("autologin", true); }else{ editor.putBoolean("autologin", false); } editor.commit(); //跳轉(zhuǎn)界面 Intent intent = new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); Toast.makeText(MainActivity.this, "登錄成功!", Toast.LENGTH_SHORT).show(); // finish(); }else{ Toast.makeText(MainActivity.this,"用戶名或密碼錯(cuò)誤,請(qǐng)重新登錄", Toast.LENGTH_LONG).show(); } } }); } public void JumpRegister(View view){ Intent intent=new Intent(this,RegisterActivity.class); startActivity(intent); finish(); } //子線程 控制自動(dòng)睡眠2秒鐘后自動(dòng)登錄 class LoginThread extends Thread{ @Override public void run() { try { sleep(2000); Intent intent = new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); } catch (InterruptedException e) { e.printStackTrace(); } } } }
以上就是怎么在Android中使用SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。