android数据持久化存储(2)
SharedPreferences
將數據存儲到SharedPreferences中:
不同于文件的存儲方式,SharedPreferences是使用鍵值對的方式來存儲數據的。也就是說當保存一條數據的時候,需要給這條數據提供一個對應的健,這樣再讀取數據的時候就可以通過這個健把相應的值去出來,而SharedPreferences還支持多種不同的數據類型存儲,如果存儲的數據類型是整形,那么讀取出來的數據也是整形,存儲數據是一個字符串,那么讀取出來的數據仍然是字符串。這樣就可以明顯的感受到使用SharedPreferences來進行數據持久話要比使用文件方便很多。
具體的使用方法是:
首先要獲取到SharedPreferences對象,android中主要提供了三種方法用于得到SharedPreferences對象:
1context類中的getSharedPreferences()方法
? ? ?此方法接受兩個參數,第一個參數用于指定SharedPreferences文件的名稱,如果文件不存在則會創建i一個,第二個參數用于指定操作模式,主要有兩種模式可以選擇,MODE_PRIVATE和MODE_MULTI_PROCESS,MODE_PRIVATE仍然是默認的操作模式,和直接傳入0效果相同,表示只有當前的應用程序才可以對這個SharedPreferences文件進行讀寫。MODE_MULTI_PROCESS則一般是用于會有多個進程中對同一個SharedPreferences文件進行讀寫的情況。
2 Activity類中的getPreferences()方法
? 這個方法只能接受一個操作模式參數,因為使用這個方法時會自動將當前活動的類名作為SharedPreferences的文件名。
3?SharedPreferences類中的getDefaultSharedPreferences()方法
這是一個靜態方法,他接受一個context參數,并自動使用當前應用程序的包名作為前綴來命令SharedPreferences文件。
得到SharedPreferences對象之后,就可以開始向SharedPreferences文件中存儲數據了,主要按照下面三個補助進行實現。
1 調用SharedPreferences對象的edit()方法來獲取一個Sharedpreferences.Editor對象。
2 向SharedPreferences.Editoe對象中添加數據,比如添加一個字符串就使用putString()方法。
3 調用commit()方法將添加的數據提交,從而完成數據存儲操作。
?
從SharedPreferences中讀取數據:
從SharePreferences中讀取數據相當簡單,SharedPreferences提供了一系列的get方法用來存儲的數據進行讀取,每個get方法都對應了SharedPreferences.
?
具體實例:實現記住密碼功能
布局文件代碼:
<?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:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.apple.servicetest.MainActivity"android:orientation="vertical"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/user_name"android:hint="input your name"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/user_passwd"android:hint="password"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/remember_pass"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="remember passworld"/></LinearLayout><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/login"android:text="save data"/></LinearLayout>功能代碼?
package com.example.apple.servicetest;import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Message; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter;public class MainActivity extends AppCompatActivity {private SharedPreferences pref;private SharedPreferences.Editor editor;private EditText username;private EditText password;private Button login;private CheckBox rememberpass;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pref= PreferenceManager.getDefaultSharedPreferences(this);username=(EditText)findViewById(R.id.user_name);password=(EditText)findViewById(R.id.user_passwd);rememberpass=(CheckBox)findViewById(R.id.remember_pass);login=(Button)findViewById(R.id.login);boolean isremember=pref.getBoolean("remember_password",false);if (isremember){String account=pref.getString("account","");String passwd=pref.getString("password","");username.setText(account);password.setText(passwd);rememberpass.setChecked(true);}login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String account=username.getText().toString();String passwd=password.getText().toString();editor=pref.edit();if (rememberpass.isChecked()){editor.putBoolean("remember_password",true);editor.putString("account",account);editor.putString("password",passwd);}else {editor.clear();}editor.commit();Intent intent=new Intent(MainActivity.this,second.class);startActivity(intent);finish();}});}public String load(){FileInputStream in=null;BufferedReader reader=null;StringBuilder content=new StringBuilder();try {in=openFileInput("data");reader=new BufferedReader(new InputStreamReader(in));String line="";while ((line=reader.readLine())!=null){content.append(line);}}catch (IOException e){e.printStackTrace();}finally {if (reader!=null){try{reader.close();}catch (IOException e){e.printStackTrace();}}return content.toString();}}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();}}}}效果圖為:
轉載于:https://www.cnblogs.com/aizhiyuan/p/5307621.html
總結
以上是生活随笔為你收集整理的android数据持久化存储(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python做前端开发_Python开发
- 下一篇: 网络安全辅助工具:免费MD5解密网站