您的位置:首页 > 移动开发 > Android开发

Android实现自动登录和记住密码

2015-12-05 01:42 691 查看
记住密码和自动登录是很多手机软件都有的一个功能。可以用 SharedPreferences类来轻松实现。 SharedPreferences是安卓平台的一个轻量级类。采用 SharedPreferences进行数据存储是很方便的。



public class LoginCheckActivity extends Activity implements OnClickListener{
	//登录按钮
	private Button loginButton;
	
	//账号文本框
	private EditText accountEditText;
	//密码文本框
	private EditText passwordEditText;
	
	//账号
	private String account;
	//密码
	private String password;
	
	private String accountValue;
	private String passwordValue;
		
	//进度条
	private ProgressDialog pd = null;
	
	//记住账号的CheckBox
	private CheckBox savedAccountCheckBox;
	//自动登录的CheckBox
	private CheckBox autoLoginCheckBox;
	
	//SharePreferences对象,用于记住账号
	private SharedPreferences sp;
	
	//记住账号的标志常数
	private final String MAK = "innoview";
	
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置标题不显示
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		//账号的文本框
		accountEditText = (EditText)this.findViewById(R.id.et_account);
		//密码的文本框
		passwordEditText = (EditText)this.findViewById(R.id.et_password);
		//保存账号的CheckBox
		savedAccountCheckBox = (CheckBox)findViewById(R.id.cb_savedAccount);
		//自动登录的CheckBox
		autoLoginCheckBox = (CheckBox)findViewById(R.id.cb_autoLogin);
		//获取保存在SharePreferences里面的账号信息,实现自动登录
		sp = getSharedPreferences("accountInfo",Context.MODE_WORLD_READABLE);
		
		if(sp.getBoolean("ISCHECK", false)){
			
			savedAccountCheckBox.setChecked(true);
			
			try{
				 accountValue = sp.getString("ACCOUNTVALUE","");
				 System.out.println("<<<<<<<<<<<<"+"账号"+accountValue);
			}catch (Exception e) {
				// TODO: handle exception
			}
			
			accountEditText.setText(account);
			
			try{
				 passwordValue = sp.getString("PASSWORDVALUE","");
				 System.out.println("<<<<<<<<<<<<"+"密码"+passwordValue);
            
			}catch (Exception e) {
				// TODO: handle exception
			}
			
			passwordEditText.setText(password);
			
			if(sp.getBoolean("AUTO_ISCHECK", false)){
				 autoLoginCheckBox.setChecked(true);
				 initLogin();
		    }
			
		}
		
		savedAccountCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
			if (savedAccountCheckBox.isChecked()) {
			                     
				System.out.println("记住账号框未选中状态");
				sp.edit().putBoolean("ISCHECK", true).commit();

			}else {

				System.out.println("记住账号框未选中");
				sp.edit().putBoolean("ISCHECK", false).commit();

			}

			}
		});
		
		autoLoginCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
	        	if (autoLoginCheckBox.isChecked()) {
	        		System.out.println("自动登录功能以启用");
	        		sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

	        	} else {
	        		System.out.println("自动登录已关闭");
	        		sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
	        	}
	        }
		});
		
		
		loginButton = (Button)findViewById(R.id.bt_login);
		loginButton.setOnClickListener(this);
		
	}
	
	/**
	 * 重载的按钮点击事件
	 */
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.bt_login:
			initLogin();
			break;
		default:
			break;
		}
	}   
	
	public void initLogin(){
		//显示进度条
		pd = new ProgressDialog(this);
		pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pd.setMessage("数据加载...");
		pd.show();
		
		account = accountEditText.getText().toString();
		password = passwordEditText.getText().toString();
		
		if(savedAccountCheckBox.isChecked())
		{
		 
		 Editor editor = sp.edit();

		    try {
	            editor.putString("ACCOUNTVALUE", account);
		        System.out.println("<<<<<<<<"+"账号"+account);
		    } catch (Exception e) {
		        Toast.makeText(LoginCheckActivity.this,"账号保存异常",Toast.LENGTH_SHORT).show();
		        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		    }
		    
		    try {
	                 editor.putString("PASSWORDVALUE", password);
		        System.out.println("<<<<<<<<"+"密码"+password));
		    } catch (Exception e) {
		        Toast.makeText(LoginCheckActivity.this,"密码保存异常",Toast.LENGTH_SHORT).show();
		        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		    }
		    
		   editor.commit();
		}

		
		loginCheck(account, password);
	}
	
	
	/**
	 * 登录验证
	 * @param account 账号(学号)
	 * @param password 密码
	 */
	public void loginCheck(String account, String password){
		
		if(account.equals("admin")&&password.equals("123")){
			Intent intent = new Intent(LoginCheckActivity.this,MainActivity.class);
			startActivity(intent);
			LoginCheckActivity.this.finish();
		}

	}                                                                                                                         }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: