一、持久化技術簡介
1. 瞬時數據:
存儲在內存中,當內存被回收時,會丟失的數據。
2. 數據持久化:
將內存中的瞬時數據保存到存儲設備中,保證設備關機後,數據不會丟失。
3. Android中的三種實現數據持久化功能的技術:
A. 文件存儲 B. SharedPreference 存儲 C. 數據庫存儲
二、文件存儲
最基本的一種數據存儲方式,不對所存儲的數據進行任何的格式化處理,適合一些簡單的文本數據或者二進制數據。
核心技術:Context 類中提供的 openFileInput(…) 和 openFileOutput(…) 方法,配合上Java的各種流來進行讀寫操作。
1. 存儲數據到文件中
Context 類中提供的 openFileOutput(…) 方法:
openFileOutput(…)
參數一: 文件名,不可以包含路徑(文件默認存儲到/data/data//files/目錄下)
參數二: 文件的操作模式,模式一:MODE_PRIVATE(默認)同名時覆蓋原文件;模式二:MODE_APPEND 同名時追加內容。
返回值: FileOutputStream 對象
2. 從文件中讀取數據
Context 類中提供的 openFileInput(…) 方法:
openFileInput(…)
參數: 文件名
返回值: FileInputStream 對象
三、SharedPreferences 存儲
使用鍵值對存儲數據,支持多種不同的數據類型存儲。使用XML格式來對數據進行管理。
1. 將數據存儲到 SharedPreferences 中
步驟:
- 獲取SharedPreferences對象;
- 調用SharedPreferences對象的edit()方法獲得SharedPreferences.Editor對象;
- 向SharedPreferences.Editor對象添加數據,使用方法putXxx(K,V);(Xxx 某數據類型;K 鍵名;V 值);
- 調用apply()方法,將添加的數據提交。
如何獲取到SharedPreferences對象呢?Android主要提供了三種方法:
- Context類 getSharedPreferences(…)方法
參數一: 文件名(文件默認存到/data/data//shared_prefs/目錄下)
參數二: 操作模式,MODE_PRIVATE(默認)和直接傳入0的效果相同,表只有當前應用程序可以對此SharedPreferences文件進行讀寫。
返回值: SharedPreferences 對象; - Activity類 getPreferences(…)方法
參數: 操作模式;
返回值: SharedPreferences 對象;
該方法會自動將當前活動的類名作爲SharedPreferences的文件名。 - PreferenceManager類 getDefaultSharedPreferences(…)靜態方法
參數: Context對象;
返回值: SharedPreferences 對象;
該方法會自動使用當前應用程序的包名作爲前綴來命名SharedPreferences文件。
2. 從 SharedPreferences 中讀取數據
方法: 調用SharedPreferences提供的方法getXxx(…)方法
參數一: K鍵;
參數二: 默認值;(當傳入的鍵找不到對應的值,以這個默認值返回)
3. 實現記住密碼功能
這只是一個簡單的示例,並不能在實際項目中直接使用,因爲這種將密碼以明文的形式存儲在SharedPreferences文件中是非常不安全的,易被盜取,在正式的項目中還需要配合加密算法來對密碼進行保護。
A. 在此項目上進行修改廣播的最佳實踐——實現強制下線功能
B. 效果圖
1.)登錄界面
2.)輸入賬號密碼並勾選記住密碼
3.)強制重新登陸
C. 代碼
1.)activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.thinkpad.broadcastbestpractice.LoginActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="賬號:"/>
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="密碼:"/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:inputType="textPassword"/>
</LinearLayout>
<!--複選框的佈局-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/remember_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="記住密碼!"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="登錄"/>
</LinearLayout>
2.)LoginActivity.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity {
private SharedPreferences pref; //SharedPreferences 變量
private SharedPreferences.Editor editor; //SharedPreferences.Editor變量
private EditText accountEdit; //輸入賬號的EditText變量
private EditText passwordEdit; //輸入密碼的EditText變量
private Button login; //登錄按鈕變量
private CheckBox rememberPass; //複選框變量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref = PreferenceManager.getDefaultSharedPreferences(this);//獲取pref實例
//實例化控件
accountEdit = (EditText)findViewById(R.id.account);
passwordEdit = (EditText)findViewById(R.id.password);
rememberPass = (CheckBox)findViewById(R.id.remember_pass);
boolean isRemember = pref.getBoolean("remember_password",false);//獲取複選框先前狀態
if(isRemember){
//如果複選框先前被選中,那麼將賬號密碼從SharedPreferences文件中讀出,設置到文本框中
String account = pref.getString("account","");
String password = pref.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login = (Button)findViewById(R.id.login);
//爲登陸按鈕設置點擊事件
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account = accountEdit.getText().toString(); //獲取賬號輸入框中的內容
String password = passwordEdit.getText().toString(); //獲取密碼輸入框中的內容
//判斷賬號密碼是否正確;此處的賬號:Nicholas 密碼: hzf
if (account.equals("Nicholas")&&password.equals("hzf")){
editor = pref.edit(); //獲取SharedPreferences.Editor實例
if(rememberPass.isChecked()){
//查看複選框是否被選中,若選中則將文本框中的信息存入SharedPreferences文件中
editor.putBoolean("remember_password",true);//存入當前複選框狀態
editor.putString("account",account);
editor.putString("password",password);
}else{
editor.clear();//未被選中則將文件內容清空
}
editor.apply();//提交數據
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this,"賬號密碼輸入錯誤或者爲空!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
整理學習自郭霖大佬的《第一行代碼》
目前小白一名,持續學習Android中,如有錯誤請批評指正!