1. 程式人生 > >第一行程式碼之SharedPreferences儲存的程式碼

第一行程式碼之SharedPreferences儲存的程式碼

SharedPreferences儲存

要想使用 SharedPreferences 來儲存資料,首先需要獲取到 SharedPreferences 物件。Android 提供了三種方法得到 SharedPreferences 物件:

  • Context 類中的 getSharedPreferences()方法   此方法接收兩個引數,第一個引數指定 SharedPreferences 檔案的名稱,第二個引數指定操作模式,目前只有 MODE_PRIVATE 一種模式,和直接傳入 0 效果相同。其他幾種模式已被廢棄。

  • Activity 類中的 getPreferences()方法   此方法和上面的方法相似,但只接收一個操作模式引數,使用這個方法時會自動將當前活動的類名作為 SharedPreferences 的檔名。

  • PreferenceManager 類中的 getDefaultSharedPreferences()方法   這是一個靜態方法,它接收一個 Context 引數,並自動使用當前應用程式的包名作為字首來命名 SharedPreferences 檔案。

得到了 SharedPreferences 物件之後,分為三步實現向 SharedPreferences 檔案中儲存資料:   (1)呼叫 SharedPreferences 物件的 edit()方法來獲取一個 SharedPreferences.Editor 物件。   (2)向 SharedPreferences.Editor 物件中新增資料,如新增一個布林型資料使用 putBoolean 方法,新增一個字串使用 putString()方法,以此類推。   (3)

呼叫 apply()方法將新增的資料提交,完成資料儲存。

當然,SharedPreference在提交資料時也可用 Editor 的 commit 方法,兩者區別如下:

  • apply() 沒有返回值而 commit() 返回 boolean 表明修改是否提交成功
  • apply() 將修改提交到記憶體,然後再非同步提交到磁碟上;而 commit() 是同步提交到磁碟上。** 谷歌建議:若在UI執行緒中,使用 apply() 減少UI執行緒的阻塞(寫到磁碟上是耗時操作)引起的卡頓。**

新建一個專案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/save_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="儲存資料" />

</LinearLayout>

放一個按鈕,將一些資料儲存到SharedPreferences 檔案當中。然後修改 Activity 中的程式碼

public class SharePreferencesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_preferences);

        Button save_data = (Button) findViewById(R.id.save_data);
        save_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 1.指定檔名為 wonderful,並得到 SharedPreferences.Editor 物件
                SharedPreferences.Editor editor = getSharedPreferences("wonderful",MODE_PRIVATE).edit();
                // 2.新增資料
                editor.putString("name","開心wonderful");
                editor.putInt("age",20);
                editor.putBoolean("married",false);
                // 3.資料提交
                editor.apply();
            }
        });
    }
}

點選按鈕後,這時資料已儲存成功了,生成了一個 wonderful.xml 檔案

SharedPreferences 檔案是使用 XML 格式來對資料進行管理的

6.2.2 從 SharedPreferences 中讀取資料

SharedPreferences 物件中提供了一系列的 get 方法用於對儲存的資料進行讀取

每種 get 方法都對應了 SharedPreferences. Editor 中的一種 put 方法

這些 get 方法接收兩個引數,第一個引數是鍵,即傳入儲存資料時使用的鍵;第二個引數是預設值

即當傳入的鍵找不到對應的值時,返回預設值

修改上面專案中的佈局程式碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"  >

    <Button
        android:id="@+id/save_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="儲存資料" />

    <Button
        android:id="@+id/restore_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="讀取資料" />
    
    <TextView
        android:id="@+id/show_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

增加了一個還原資料的按鈕和 TextView,點選按鈕來從 SharedPreferences 檔案中讀取資料並在 TextView 中顯示讀取的資料

修改 Activity 中的程式碼:

public class SharePreferencesActivity extends AppCompatActivity {
    
    private Button save_data,restore_data;
    
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_preferences);

        save_data = (Button) findViewById(R.id.save_data);
        save_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 1.指定檔名為 wonderful,並得到 SharedPreferences.Editor 物件
                SharedPreferences.Editor editor = getSharedPreferences("wonderful",MODE_PRIVATE).edit();
                // 2.新增不同型別的資料
                editor.putString("name","開心wonderful");
                editor.putInt("age",20);
                editor.putBoolean("married",false);
                // 3.資料提交
                editor.apply();
            }
        });

        textView = (TextView) findViewById(R.id.show_data);
        restore_data = (Button) findViewById(R.id.restore_data);
        restore_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 獲得 SharedPreferences 物件
                SharedPreferences pref = getSharedPreferences("wonderful",MODE_PRIVATE);
                // 獲取相應的值
                String name = pref.getString("name","");
                int age = pref.getInt("age",0);
                boolean married = pref.getBoolean("married",false);
                // 將獲取到的值顯示
                textView.setText("name is " + name + ",age is "+ age + ",married is "+ married);
            }
        });
    }
}

重新執行一下程式,並點選介面上的 讀取資料 按鈕,然後檢視 TextView中顯示的資訊:

6.2.3 實現記住密碼功能

會用上一章廣播的強制下線的例子

修改專案前,先來簡單封裝下關於 SharedPreferences 的工具類,如下:

public class PrefUtils {

    private static final String PREF_NAME = "config";

    /**
     * 讀取布林資料
     * @param ctx 上下文
     * @param key 鍵
     * @param defaultValue 預設值
     * @return
     */
    public static boolean getBoolean(Context ctx, String key,
                                     boolean defaultValue) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * 添加布爾資料
     * @param ctx 上下文
     * @param key 鍵
     * @param value 新增的資料
     */
    public static void setBoolean(Context ctx, String key, boolean value) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        sp.edit().putBoolean(key, value).apply();
    }

    /**
     * 讀取字串
     * @param ctx
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getString(Context ctx, String key, String defaultValue) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        return sp.getString(key, defaultValue);
    }

    /**
     * 新增字串
     * @param ctx
     * @param key
     * @param value
     */
    public static void setString(Context ctx, String key, String value) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        sp.edit().putString(key, value).apply();
    }

    /**
     * 讀取int型別資料
     * @param ctx
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(Context ctx, String key, int defaultValue) {
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        return sp.getInt(key, defaultValue);
    }

    /**
     * 新增int型別資料
     * @param ctx
     * @param key
     * @param value
     */
    public static void setInt(Context ctx, String key, int value){
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        sp.edit().putInt(key, value).apply();
    }

    /**
     * 將資料全部清除掉
     * @param ctx
     */
    public static void clear(Context ctx){
        SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        sp.edit().clear().apply();
    }
}

編輯下登入介面,修改 activity_login.xml 中的程式碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--***************** 賬號 *********************-->
    <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:gravity="center"
            android:textSize="18sp"
            android:text="賬號:"/>

        <EditText
            android:id="@+id/et_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:gravity="center"
            android:textSize="18sp"
            android:text="密碼:"/>

        <EditText
            android:id="@+id/et_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/cb_remember_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_gravity="center_vertical"
            android:text="記住密碼"/>

    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:text="登入"/>

</LinearLayout>

添加了個 CheckBox 來勾選記住密碼,接著修改 LoginActivity 的程式碼:

public class LoginActivity extends BaseActivity {

    private EditText et_account, et_password;
    private CheckBox cb_remember_pass;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_remember_pass = (CheckBox) findViewById(R.id.cb_remember_pass);
        btn_login = (Button) findViewById(R.id.btn_login);
        
        Boolean isRemember = PrefUtils.getBoolean(this,"remember_pass",false);
        if (isRemember){
            // 將賬號和密碼都設定到文字框中
            String account = PrefUtils.getString(this,"account","");
            String password = PrefUtils.getString(this,"password","");
            et_account.setText(account);
            et_password.setText(password);
            cb_remember_pass.setChecked(true);
        }

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = et_account.getText().toString();
                String password = et_password.getText().toString();
                // 若賬號是 wonderful 且密碼是 123456,就認為登入成功
                if (account.equals("wonderful") && password.equals("123456")){
                    // 檢查複選框是否被勾選
                    if (cb_remember_pass.isChecked()){
                        // 儲存資料到SharePreference檔案中
                        PrefUtils.setBoolean(LoginActivity.this,"remember_pass",true);
                        PrefUtils.setString(LoginActivity.this,"account",account);
                        PrefUtils.setString(LoginActivity.this,"password",password);
                    }else {
                        // 清除SharePreference檔案中的資料
                        PrefUtils.clear(LoginActivity.this);
                    }
                    // 登入成功跳轉到主介面
                    IntentUtils.myIntent(LoginActivity.this,ForceOfflineActivity.class);
                    finish();
                }else {
                    ToastUtils.showShort("賬號或密碼無效!");
                }
            }
        });

    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_login;
    }
}

現在執行下程式,輸入賬號和密碼並選中記住密碼複選框後,點選登入,就會跳轉到 ForceOfflineActivity

接著在 ForceOfflineActivity 中發出一條強制下線廣播會讓程式重新回到登入介面

此時你會發現,賬號密碼都已經自動填充到介面上