首页 > 代码库 > Android开发之自动登录功能的实现

Android开发之自动登录功能的实现

         在我们平时使用的手机应用都可以实现只需要登陆一次账号后,第二次进入应用直接跳转到效果界面的效果,还有QQ的登陆框是如何记忆我们的隐身登陆,保存账号选项的呢,这些都是通过使用SharedPreferences共享参数效果实现的,而无须使用数据库来存储。以下我们直接看详细代码分析。

package com.example.account.login;

import java.util.HashMap;
import java.util.Map;

import com.android.dao.MySQLiteOpenHelper;
import com.example.account.MainActivity;
import com.example.account.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private EditText e1, e2;
	private SQLiteOpenHelper helper;
	private boolean flag, flag2, flag3;
	private HashMap<String, Object> map;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		TextView textView = (TextView) this.findViewById(R.id.textView1);
		e1 = (EditText) this.findViewById(R.id.editText1);
		e2 = (EditText) this.findViewById(R.id.editText2);
		//从共享参数获取数据
		map = (HashMap<String, Object>) getMsg("login");
		if (map != null && !map.isEmpty()) {
			if ((Boolean) map.get("login2")) {
				//若值为true,用户无需输入密码,直接跳转进入操作界面
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				startActivity(intent);
			}
		}
		helper = new MySQLiteOpenHelper(this);
		textView.setText("登录界面");
		Button button = (Button) findViewById(R.id.button2);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!e1.getText().toString().isEmpty()
						&& !e2.getText().toString().isEmpty()) {
					//从数据库获取账号信息
					SQLiteDatabase database = helper.getReadableDatabase();
					Cursor cursor = database.query("user", new String[] {
							"username", "password" }, null, null, null, null,
							null);
					while (cursor.moveToNext()) {
						flag = e1
								.getText()
								.toString()
								.equals(cursor.getString(cursor
										.getColumnIndex("username")));
						flag2 = e2
								.getText()
								.toString()
								.equals(cursor.getString(cursor
										.getColumnIndex("password")));
						if (flag && flag2) {
							Intent intent = new Intent(LoginActivity.this,
									MainActivity.class);
							startActivity(intent);
							//登陆跳转动画
							overridePendingTransition(R.anim.zoomin,
									R.anim.zoomout);
							Toast.makeText(LoginActivity.this, "登录成功",
									Toast.LENGTH_SHORT).show();
							flag3 = true;
							//登陆成功后将flag设置为ture存入共享参数中
							HashMap<String, Object> map = new HashMap<String, Object>();
							map.put("login2", flag3);
							saveMsg("login", map);
						}
					}
					if (!flag3) {
						Toast.makeText(LoginActivity.this, "您输入的帐号或密码有误",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(LoginActivity.this, "请正确输入您的帐号密码",
							Toast.LENGTH_SHORT).show();
				}

			}

		});
		Button button2 = (Button) findViewById(R.id.button1);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(LoginActivity.this,
						RegisterActivity.class);
				startActivity(intent);

			}

		});

	}
   //将数据存储进入共享参数
	public boolean saveMsg(String fileName, Map<String, Object> map) {
		boolean flag = false;
		// 一般Mode都使用private,比较安全
		SharedPreferences preferences = getSharedPreferences(fileName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		// Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。
		// 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,
		// 因此,上面的代码可以被组织得更符合逻辑
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			String key = entry.getKey();
			Object object = entry.getValue();
			// 根据值得不同类型,添加
			if (object instanceof Boolean) {
				Boolean new_name = (Boolean) object;
				editor.putBoolean(key, new_name);
			} else if (object instanceof Integer) {
				Integer integer = (Integer) object;
				editor.putInt(key, integer);
			} else if (object instanceof Float) {
				Float f = (Float) object;
				editor.putFloat(key, f);
			} else if (object instanceof Long) {
				Long l = (Long) object;
				editor.putLong(key, l);
			} else if (object instanceof String) {
				String s = (String) object;
				editor.putString(key, s);
			}
		}
		flag = editor.commit();
		return flag;

	}

	// 读取数据
	public Map<String, ?> getMsg(String fileName) {
		Map<String, ?> map = null;
		// 读取数据用不到edit
		SharedPreferences preferences = getSharedPreferences(fileName,
				Context.MODE_APPEND);
		//Context.MODE_APPEND可以对已存在的值进行修改
		map = preferences.getAll();
		return map;
	}

}