Android持久化技術
瞬時數據是指存儲在記憶體中的數據。持久化技術可以將記憶體中的數據和持久狀態(保存在存儲設備上)之間相互轉化。
Android提供了三種持久化方式
文件存儲
文件存儲不對存儲內容進行任何的格式化處理,所有數據都是原封不動保存到文件當中。
將數據存儲到文件中
Context類中提供了一個openFileOutput()方法,返回一個FileOutputStream對象,然後就可以用javaI/O流去寫文件中了。文件都默認放在/data/data/
/files/目錄下。
public void save(String data){
FileOutputStream out=null;
BufferedWriter writer=null;
try{
out=openFileOutput("data", Context.MODE_PRIVATE);//第一個參數是文件的名稱,第二個參數是模式。MODE_PRIVATE表示覆蓋,MODE_APPEND表示追加。
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();
}
}
}
從文件中讀取數據
Context類還提供了一個openFileInput(),返回的是一個FileInputStream。它接收一個參數,即要讀取的文件名。然後系統會去/data/data/
/files/目錄下尋找。
public String load(String name){
FileInputStream input=null;
BufferedReader reader=null;
StringBuilder content=new StringBuilder();
try {
input=openFileInput(name);
reader=new BufferedReader(new InputStreamReader(input));
String line="";
while((line=reader.readLine())!=null){
content.append(line);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(reader!=null)
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
return content.toString();
}
SharedPreferences存儲
SharedPreferences是使用鍵值對的方式來存儲數據的
將數據存儲到SharedPreferences中
步驟
1.獲取SharedPreferences對象
- Context類中的getSharedPreferences()方法
此方法接收兩個參數,第一個參數用於指定SharedPreferences文件的名稱,如果直接的名稱文件不存在,則會創建一個文件,SharedPreferences文件都是存放在/data/data/
/shared_prefs/目錄下。第二個參數用於指定操作模式,目前只有MODE_WORLD_READABLE這一種模式可選,它是默認的操作模式,和直接傳入0效果是相同的,表示只有當前的應用才可以對這個文件進行讀寫.
- Activity類中的getPreferences()方法
只接收一個操作模式參數,因為自動將當前活動的類名當作SharedPreferences的文件名
- PreferenceManager類中的getDefaultSharedPreferences()方法
這是一個靜態方法,它接收一個Context參數,並自動使用當前應用程式的包名作為前綴來命名SharedPreferences文件。
2.向SharedPreferences中存儲數據
(1)調用SharedPreferences對象的edit()方法來獲取一個SharedPreferences.Editor對象
(2)向SharedPreferences.Editor對象中添加數據,比如添加一個布爾型數據就使用putboolean()方法等
(3)調用apply()方法將添加的數據提交,從而完成數據存儲操作
從SharedPreferences中讀取數據
步驟
1.獲取SharedPreferences對象
2.從SharedPreferences中讀取數據
直接使用SharedPreferences對象的getxxx(鍵,默認值)方法獲取對應類型值
實例:用SharedPreferences實現記住密碼功能
1.新建LoginActivity活動
布局:
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Account:"
android:textSize="18dp"
android:layout_gravity="center"/>
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Password:"
android:textSize="18dp"
android:layout_gravity="center_vertical"
/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<CheckBox
android:id="@+id/remeber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remember password"/>
<Button
android:id="@+id/login"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
LoginActivity類:
ublic class LoginActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private EditText account;
private EditText password;
private CheckBox isRemeber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
account=(EditText)findViewById(R.id.account);
password=(EditText)findViewById(R.id.password);
isRemeber=(CheckBox)findViewById(R.id.remeber);
if(sharedPreferences.getBoolean("isRemeber",false)){//之前記住過密碼
account.setText(sharedPreferences.getString("account",""));
password.setText(sharedPreferences.getString("password",""));
isRemeber.setChecked(true);
}
Button button=(Button)findViewById(R.id.login);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String accountstr=account.getText().toString();
String passwordstr=password.getText().toString();
if(accountstr.equals("admin")&&passwordstr.equals("123456")) {
editor=sharedPreferences.edit();
if (isRemeber.isChecked()) {//選擇記住密碼
editor.putBoolean("isRemeber",true);
editor.putString("account", accountstr);//保存賬戶
editor.putString("password", passwordstr);//保存密碼
}else{
editor.clear();//如果沒選中說明清空
}
editor.apply();//運行
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this,"account or password is invald",Toast.LENGTH_SHORT).show();
}
}
});
}
}