Android數(shù)據(jù)持久化之記住密碼功能實(shí)現(xiàn)(附源碼)

效果演示:

源碼:https://github.com/junzaivip/SharePreferencesDemo

手機(jī)安裝演示鏈接:http://pan.baidu.com/s/1dFvbDdB 密碼:y8p8

其實(shí)我們?cè)谏缃痪W(wǎng)絡(luò)上面所發(fā)出的任何信息, 都希望能夠保留下來(lái). 那么如何實(shí)現(xiàn)呢?

數(shù)據(jù)持久化

數(shù)據(jù)持久化, 就是將內(nèi)存中的瞬時(shí)數(shù)據(jù)保存在存儲(chǔ)設(shè)備中, 保證即便關(guān)機(jī)之后, 數(shù)據(jù)仍然存在.

保存在內(nèi)存中的數(shù)據(jù)是瞬時(shí)數(shù)據(jù), 保存在存儲(chǔ)設(shè)備中的數(shù)據(jù)就是處于持久狀態(tài)的.

持久化技術(shù)則是提供了一種機(jī)制可以讓數(shù)據(jù)在瞬時(shí)狀態(tài)和持久狀態(tài)之間進(jìn)行轉(zhuǎn)換, Android系統(tǒng)中主要提供了3種方式用于簡(jiǎn)單地實(shí)現(xiàn)數(shù)據(jù)持久化功能, 即文件存儲(chǔ), SharePreference存儲(chǔ), 以及數(shù)據(jù)庫(kù)存儲(chǔ). 當(dāng)然你也可以將數(shù)據(jù)保存在手機(jī)的SD卡中.

文件存儲(chǔ)

文件存儲(chǔ)是android中最基本的一種數(shù)據(jù)存儲(chǔ)方式, 它不對(duì)存儲(chǔ)的內(nèi)容進(jìn)行任何的格式化處理, 所有的數(shù)據(jù)都是原封不動(dòng)地保存到文件當(dāng)中, 因?yàn)樗容^適合存儲(chǔ)一些簡(jiǎn)單的文本數(shù)據(jù)或二進(jìn)制數(shù)據(jù). 如果你希望使用文件存儲(chǔ)的方式來(lái)保存一些較為復(fù)雜的的文本數(shù)據(jù), 就需要定義一套自己的格式規(guī)范, 這樣可以方便之后將數(shù)據(jù)從文件中重新取出來(lái).

將數(shù)據(jù)存儲(chǔ)在文件中

Context類中提供了一個(gè)openFileOutput()方法, 可以用于將數(shù)據(jù)存儲(chǔ)在指定的文件中. 這個(gè)方法接收兩個(gè)參數(shù),

第一個(gè)參數(shù)是文件名, 在文件創(chuàng)建的時(shí)候使用的就是這個(gè)名稱, 注意這里指定的文件名不可以包含路徑的. 因?yàn)樗械奈募际悄J(rèn)存儲(chǔ)到/data/data/<package name>/files/目錄下.

第二個(gè)參數(shù)是文件的操作模式, 主要有兩種模式可以選, MODE_PRIVATE和MODE_APPEND. 其中MODE_PRIVATE是默認(rèn)的操作模式, 表示當(dāng)指定同樣文件名的時(shí)候, 所寫入的內(nèi)容將會(huì)覆蓋原文件中的內(nèi)容. 而MODE_APPEND則表示如果該文件已存在, 就往文件里面追加內(nèi)容, 不存在就創(chuàng)建新文件.

openFileOutput()方法返回的是一個(gè)FileOutputStream對(duì)象, 得到了這個(gè)對(duì)象之后就可以使用java流的方式將數(shù)據(jù)寫入到文件中了.

 public void save(){
        String data = "Data to save";
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
           try {
               if(writer!= null){
                   writer.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
        }
    }

說(shuō)明: 通過openFileOutput()方法能夠得到一個(gè)FileOutputStream對(duì)象, 然后再借助它構(gòu)建出一個(gè)OutputStreamWriter對(duì)象, 接著再使用OutputStreamWriter構(gòu)建出一個(gè)BufferedWriter對(duì)象, 這樣就可以通過BufferedWriter來(lái)講文本內(nèi)容寫入到文件中了.

下面我們來(lái)一個(gè)完整的例子來(lái)理解一下,當(dāng)我們?cè)谕顺龀绦蛑? 將我們?cè)谖谋究蛑休斎氲膬?nèi)容儲(chǔ)存在文件中.

新建項(xiàng)目FilePersistenceDemo項(xiàng)目, 且修改activity_main.xml中的代碼.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

說(shuō)明: 界面只有一個(gè)EditText文本框.

MainActivity.java文件:

public class MainActivity extends AppCompatActivity {
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取editText實(shí)例
        editText = (EditText)findViewById(R.id.edit);
    }

    // 重寫onDestroy(), 可以保證活動(dòng)銷毀之前一定會(huì)調(diào)用這個(gè)方法.
    @Override
    protected void onDestroy() {
        super.onDestroy();
        String inputText = editText.getText().toString();
        save(inputText);
    }

    public void save (String inputText){
        FileOutputStream out = null;
        BufferedWriter writer = null;

        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                  if(writer!= null) {
                      writer.close();
                  }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

那么我們運(yùn)行程序, 我們輸入的內(nèi)容就會(huì)保存在文件中. 如果您的手機(jī)已經(jīng)Root了, 可以直接在 應(yīng)用程序的包名/files目錄就可以發(fā)現(xiàn).

從文件中讀取數(shù)據(jù)

核心代碼:

public String load (){
        FileInputStream in = null;
        BufferedReader reader =  null;
        StringBuilder content = new StringBuilder();
        try {
            //獲取FileInputStream對(duì)象
            in = openFileInput("data");
            //借助FileInputStream對(duì)象, 構(gòu)建出一個(gè)BufferedReader對(duì)象
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            //通過BufferedReader對(duì)象進(jìn)行一行行的讀取, 把文件中的所有內(nèi)容全部讀取出來(lái)
            // 并存放在StringBuilder對(duì)象中
            while ((line = reader.readLine())!= null){
                content.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //最后將讀取到的內(nèi)容返回
        return  content.toString();
    }

修改我們MainActivity中的代碼:

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取editText實(shí)例
        editText = (EditText)findViewById(R.id.edit);
        String inputText = load();
//TextUtils.isEmpty()可以一次性判斷兩種非空判斷 傳入null或者空, 都返回true
        if(!TextUtils.isEmpty((inputText))){
            editText.setText(inputText);
            //setSelection()表示將光標(biāo)移動(dòng)在文本框的末尾位置, 以便繼續(xù)輸入
            editText.setSelection(inputText.length());
            //彈出Toast, 給出一個(gè)提示, 表示讀取數(shù)據(jù)成功
            Toast.makeText(this, "讀取數(shù)據(jù)成功!", Toast.LENGTH_SHORT).show();
        }
    }

    // 重寫onDestroy(), 可以保證活動(dòng)銷毀之前一定會(huì)調(diào)用這個(gè)方法.
    @Override
    protected void onDestroy() {
        super.onDestroy();
        String inputText = editText.getText().toString();
        save(inputText);
    }

    public void save (String inputText){
        FileOutputStream out = null;
        BufferedWriter writer = null;

        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(inputText);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                  if(writer!= null) {
                      writer.close();
                  }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    public String load (){
        FileInputStream in = null;
        BufferedReader reader =  null;
        StringBuilder content = new StringBuilder();
        try {
            //獲取FileInputStream對(duì)象
            in = openFileInput("data");
            //借助FileInputStream對(duì)象, 構(gòu)建出一個(gè)BufferedReader對(duì)象
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            //通過BufferedReader對(duì)象進(jìn)行一行行的讀取, 把文件中的所有內(nèi)容全部讀取出來(lái)
            // 并存放在StringBuilder對(duì)象中
            while ((line = reader.readLine())!= null){
                content.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //最后將讀取到的內(nèi)容返回
        return  content.toString();
    }
}
效果演示

源碼: https://github.com/junzaivip/FilePersistenceDemo

SharedPreferences存儲(chǔ)

不同于文件的存儲(chǔ)方式, haredPreferences是使用鍵值對(duì)的方式來(lái)存儲(chǔ)數(shù)據(jù)的. 而且支持多種不同的數(shù)據(jù)類型存儲(chǔ), 如果存儲(chǔ)的是整型, 那么讀取出來(lái)也是整型. 如果存儲(chǔ)的是字符串, 那么讀取出來(lái)也是字符串.

將數(shù)據(jù)存儲(chǔ)到SharedPreferences中

如果希望使用SharePreferences來(lái)存儲(chǔ)數(shù)據(jù), 首先需要獲取到SharePreferences對(duì)象.Android中提供了三種方法來(lái)獲取SharePreferences對(duì)象.

Context類中g(shù)etSharePreferences()方法

此方法接收兩個(gè)參數(shù), 第一個(gè)參數(shù)用于指定SharedPreferences文件的名稱, 如果指定的文件不存在則會(huì)創(chuàng)建一個(gè).SharePreferences文件都是存放在/data/data/<package name>/shared_prefs/
第二個(gè)參數(shù)用于指定操作模式, 目前只可以使用MODE_PRIVATE這一種模式, 和直接傳入0效果是相同的. 表示只有當(dāng)前的應(yīng)用程序才可以對(duì)這個(gè)SharePreferences文件進(jìn)行讀寫.

Activity類中g(shù)etSharePreferences()方法

這個(gè)方法和Context中的getSharePreferences()方法很相似, 它只接收一個(gè)操作模式參數(shù), 因?yàn)槭褂眠@個(gè)方法時(shí), 會(huì)自動(dòng)將當(dāng)前活動(dòng)的類名作為SharePreferences的文件名.

PreferenceManager類中的getDefaultSharePreferences()方法

這是一個(gè)靜態(tài)方法, 它接收一個(gè)Context參數(shù), 并自動(dòng)使用當(dāng)前應(yīng)用程序的包名作為前綴來(lái)命名SharePreferences文件. 得到SharePreferences對(duì)象之后, 就開始向SharedPreferences文件中存儲(chǔ)數(shù)據(jù)了.

  1. 調(diào)用SharePreferences對(duì)象的edit()方法來(lái)獲取一個(gè)SharePreferences.Editor對(duì)象

  2. 想SharedPreferences.Editor對(duì)象中添加數(shù)據(jù), 比如添加一個(gè)布爾型數(shù)據(jù)就可以使用putBoolean()方法, 添加一個(gè)字符串使用putString()方法等

  3. 調(diào)用apply()方法將添加的數(shù)據(jù)提交, 從而完成數(shù)據(jù)存儲(chǔ)操作.

現(xiàn)在開始新建一個(gè)SharePreferencesDemo項(xiàng)目, 然后修改activity_main.xml中的代碼.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/save_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save Data"/>
</LinearLayout>

MainActivity中的代碼:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button saveData = (Button)findViewById(R.id.save_data);
        saveData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                editor.putString("name","junzai");
                editor.putInt("age",18);
                editor.putBoolean("married", false);
                editor.apply();
            }
        });
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/save_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save Data"/>
    <Button
        android:id="@+id/restore_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="restore data"/>
</LinearLayout>
 Button restoreData = (Button) findViewById(R.id.restore_data);
        restoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences pref = getSharedPreferences("jun_zai",MODE_PRIVATE);
                String name = pref.getString("name","");
                int age = pref.getInt("age",0);
                boolean married = pref.getBoolean("married", false);
                Log.d("MainActivity","name is " + name);
                Log.d("MainActivity","age is " + age);
                Log.d("MainActivity","married is " + married);
            }
        });

運(yùn)行效果:

成功將數(shù)據(jù)取出.

下面實(shí)現(xiàn)記住密碼的功能:

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:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="User Name"
            android:textSize="18sp"
            android:layout_gravity="center_vertical"/>
        <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:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="Password"
            android:textSize="18sp"
            android:layout_gravity="center_vertical"/>
        <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="wrap_content"
        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="Remember Password"/>
   </LinearLayout>
    
    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Login"/>
</LinearLayout>

LoginActivity中的代碼:

public class LoginActivity  extends BaseActivity implements View.OnClickListener{
    private EditText userName;
    private EditText passWord;
    private Button login;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox remberPass;

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

        //獲取SharedPreferences對(duì)象
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        userName = (EditText)findViewById(R.id.account);
        passWord = (EditText)findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);

        //調(diào)用getBoolean()方法去獲取remember_password這個(gè)鍵對(duì)應(yīng)的值, 如果不存在默認(rèn)的值, 就是用的是false
        boolean isRemember = pref.getBoolean("remember_password", false);
        remberPass = (CheckBox)findViewById(R.id.remember_pass);

        if(isRemember){
            //賬號(hào)和密碼都設(shè)置到文本框
            String username = pref.getString("username","");
            String password = pref.getString("password","");

            userName.setText(username);
            passWord.setText(password);
            remberPass.setChecked(true);

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                String username = userName.getText().toString();
                String password = passWord.getText().toString();
                if(username.equals("admin") && password.equals("123")){
                    // 將數(shù)據(jù)存儲(chǔ)在SharedPreferences當(dāng)中
                    editor = pref.edit();
                    if(remberPass.isChecked()){ // 檢驗(yàn)復(fù)選框是否被選中
                        // 如果被選中, remember_password的值改為True
                        editor.putBoolean("remember_password", true);
                        editor.putString("username",username);
                        editor.putString("password", password);
                    } else{
                        editor.clear();
                    }
                    editor.apply();

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

                } else{
                    Toast.makeText(this, "賬號(hào)或者密碼錯(cuò)誤", Toast.LENGTH_SHORT).show();
                }

        }

    }
}

效果演示:

參閱: 郭霖: 第一行代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容