1、安卓实现密码保存和自动登录 布局文件:logo.xml 布局文件:welcom.xml java代码:LoginActivity.javapackage com.wang.activity;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;im
2、port android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widge
3、t.ImageButton;import android.widget.Toast;public class LoginActivity extends Activity private EditText userName, passWord;private CheckBox rem_pw, auto_login;private Button btn_login;private ImageButton btnQuit; private String userNameValue,passwordValue;private SharedPreferences sp; private final S
4、tring MAK = innoview; public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);/去掉TitleBarthis.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.login); /获取shareprefrence里面的数据sp = this.getSharedPreferences(userInfo,Context.MODE_WORLD_READABLE);userName =
5、 (EditText) findViewById(R.id.et_zh);passWord = (EditText) findViewById(R.id.et_mima); rem_pw = (CheckBox) findViewById(R.id.cb_mima);auto_login = (CheckBox) findViewById(R.id.cb_auto); btn_login = (Button) findViewById(R.id.btn_login); btnQuit = (ImageButton)findViewById(R.id.img_btn); /判断记住密码框状态 i
6、f(sp.getBoolean(ISCHECK, false) /记住密码框状态标记为选中 rem_pw.setChecked(true); try userNameValue=sp.getString(USER_NAME, ); System.out.println(+加密后的用户名+userNameValue); String username=AESEncryptor.decrypt(MAK,userNameValue); System.out.println(+解密后的用户名+username); userName.setText(username); catch (Exception
7、 e) Toast.makeText(LoginActivity.this,用户名解密异常,Toast.LENGTH_SHORT).show(); e.printStackTrace(); /To change body of catch statement use File | Settings | File Templates. String password= null; try passwordValue=sp.getString(PASSWORD, ); System.out.println(+加密后的密码+passwordValue); password = AESEncrypto
8、r.decrypt(MAK, passwordValue); System.out.println(+解密后的密码+password); catch (Exception e) Toast.makeText(LoginActivity.this,密码解密异常,Toast.LENGTH_SHORT).show(); e.printStackTrace(); /To change body of catch statement use File | Settings | File Templates. passWord.setText(password); /判断自动登录框状态 if(sp.get
9、Boolean(AUTO_ISCHECK, false) /自动登录框状态标记为选中 auto_login.setChecked(true); Intent intent = new Intent(LoginActivity.this,LogoActivity.class); LoginActivity.this.startActivity(intent); / 用户名:login 秘密:123456btn_login.setOnClickListener(new OnClickListener() public void onClick(View v) userNameValue = use
10、rName.getText().toString(); passwordValue = passWord.getText().toString(); if(userNameValue.equals(login)&passwordValue.equals(123456)Toast.makeText(LoginActivity.this,登陆成功, Toast.LENGTH_SHORT).show();/如果记住密码框未选中状态if(rem_pw.isChecked()Editor editor = sp.edit();/ AESEncryptor.encrypt(userNameValue);
11、try editor.putString(USER_NAME, AESEncryptor.encrypt(MAK,userNameValue); System.out.println(+加密后的用户名+AESEncryptor.encrypt(MAK,userNameValue); catch (Exception e) Toast.makeText(LoginActivity.this,用户名加密异常,Toast.LENGTH_SHORT).show(); e.printStackTrace(); /To change body of catch statement use File | Settings | File Templates. try editor.putString(PASSWORD,AESEncryptor.encrypt(MAK,passwordValue);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2