標籤:android database sharedpreferences 登入 append
在我們平時使用的手機應用都可以實現只需要登陸一次帳號後,第二次進入應用直接跳轉到效果介面的效果,還有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")@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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;// 讀取資料用不到editSharedPreferences preferences = getSharedPreferences(fileName,Context.MODE_APPEND);//Context.MODE_APPEND可以對已存在的值進行修改map = preferences.getAll();return map;}}