App存儲數據,可以存儲在數據庫(database),文件(file),參數引用(preferences),內部或者外部可移除存儲(sd卡)
有五種存儲方式:
①SharedPreferences(是一個接口)
使用共享參數,SharedPreferences類提供了一個可以讓我們保存或者保存數據持久化的數據類型,這種數據是key-value鍵值對應類型的。保存的基本數據類型:boolean,float,int.long,string.
爲了獲得SharedPreferences對象可以使用任何一種方法:getSharedPreferences()或者getPreferences()
getSharedPreferences()這種方法的共享參數是通過標識符來命名的文件,這個文件是被多個應用程序訪問的。
getPreferences()這種共享參數的文件只是給你的當前的Activity訪問。
步驟:
1.調用edit(),得到SharedPreferences.Editor
2.把這個values值添加到putBoolean()和putString()裏面
3.調用commit()方法
新建工程android_data_storage
這裏要用到單元測試(複習):添加單元測試的包和單元測試的約束
雙擊AndroidManifest.xml->Instrumentation->Add 雙擊Instrumentation後ok->Name Browse->輸入框中點擊一下,等待搜索,選中Instrumentation TestRunner點擊ok->Target Package Browse 選中自己的工程名中的包,點擊ok->點擊AndroidManifest.xml中的application節點中添加<uses-library android:name="android.test.runner"/>保存。接下來就可以使用單元測試了
在工程中新建包com.example.android_data_storage.sharepreference下創建類LoginService.java
(在qq的登陸界面有隱身,離線,忙碌等選項,如果選中登錄,下次登錄的時候就會勾上,這種功能的實現就是使用共享參數來實現的。)
如果第一次登陸成功,就可以把用戶名和密碼保存在本地,下次再登錄的時候,匹配本地的賬號就不需要再匹配服務器的數據了,省去一箇中間環節。
SharedPreferences是一個接口,把一個臨時的信息保存在一個文件裏面,這個文件是以.xml作爲擴展名。getSharedPreferences(String name,int mode)方法,接收的是一個文件名,返回一個共享參數的對象,僅僅用單例模式提供了一個文件,意味着每一個編輯都可以得到這個編輯的對象來修改。
name:如果這個文件不存在,當調用edit()方法時會幫我們創建,並且當commit()方法時數據發生改變。文件不要加後綴名,系統自動以.xml文件形式保存。
mode:文件操作的權限模式。MODE_PRIVATE表示只有當前的Activity纔有權限訪問文件。如果想讓某個文件既可讀又可寫則用“+”把兩個權限相加。
package com.example.android_data_storage.sharepreference;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class LoginService {
private Context context;
public LoginService(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
public boolean saveLoginMsg(String name,String password){
boolean flag=false;
//文件名不要加後綴名,系統會自動以.xml的文件保存
SharedPreferences preferences=context.getSharedPreferences("login", Context.MODE_PRIVATE);
Editor editor=preferences.edit();
editor.putString("username", name);
editor.putString("password", password);
flag=editor.commit();//只有調用commit方法纔會把傳回來的參數保存在文件上
return flag;
}
}
在com.example.android_data_storage中寫一個MyTest類繼承android.test.AndroidTestCase
package com.example.android_data_storage;
import com.example.android_data_storage.sharepreference.LoginService;
import android.test.AndroidTestCase;
import android.util.Log;
public class MyTest extends AndroidTestCase {
private final String TAG="MyTest";
public MyTest() {
// TODO Auto-generated constructor stub
}
public void save(){
LoginService service=new LoginService(getContext());
boolean flag=service.saveLoginMsg("admin", "123");
Log.i(TAG, "---->"+flag);
}
}
雙擊save()方法,右鍵Run As ->Android JUnit Test進行單元測試,出現如下界面
在File Explorer下的data->data中找到工程名,會看到裏面有個login.xml文件
把login.xml文件導出來,打開看到如下
封裝成工具類:在LoginService.java中添加方法saveSharedPreferences
/**
*
* @param fileName
* @param map
* @return
*/
public boolean saveSharedPreferences(String fileName,Map<String, Object> map){
boolean flag=false;
SharedPreferences preferences=context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor=preferences.edit();
for(Map.Entry<String, Object> entry:map.entrySet()){
String key=entry.getKey();
Object object=entry.getValue();
if(object instanceof Boolean){
Boolean new_name=(Boolean) object;
editor.putBoolean(key, new_name);
}else if(object instanceof Integer){
Integer integer =(Integer)object;
editor.putInt(key, integer);
}else if(object instanceof Float){
Float f=(Float) object;
editor.putFloat(key, f);
}else if(object instanceof Long){
Long l=(Long) object;
editor.putLong(key, l);
}else if(object instanceof String){
String s=(String) object;
editor.putString(key, s);
}
}
flag=editor.commit();
return flag;
}
在MyTest.java添加測試方法save2()測試public void save2(){
LoginService service=new LoginService(getContext());
Map<String, Object> map=new HashMap<String, Object>();
map.put("name", "CCNU");
map.put("age", 123);
map.put("salary", 3500.1f);
map.put("id", 123456789l);
map.put("isMale",true);
boolean flag=service.saveSharedPreferences("msg", map);
Log.i(TAG, "---->"+flag);
}
雙擊save2()方法,右鍵Run As ->Android JUnit Test進行單元測試,出現如下界面
同樣在File Explorer多了一個文件msg.xml
導出msg.xml並打開看到如下
這個工具類其實在開發當中不用寫,直接拿來用就行了。至此,數據文件已經能保存進來。接下來就是怎麼取數據文件?
在LoginService.java中添加讀取數據文件的方法getSharePreference
public Map<String, ?> getSharePreference(String fileName) {
Map<String, ?> map = null;
SharedPreferences preferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
map = preferences.getAll();
return map;
}
在MyTest.java中添加測試方法read()
public void read() {
LoginService service = new LoginService(getContext());
Map<String, ?> map = service.getSharePreference("msg");
Log.i(TAG, "--->" + map.get("name"));
Log.i(TAG, "--->" + map.get("age"));
Log.i(TAG, "--->" + map.get("salary"));
Log.i(TAG, "--->" + map.get("id"));
Log.i(TAG, "--->" + map.get("isMale"));
}
雙擊read()方法,右鍵Run As ->Android JUnit Test進行單元測試,出現如下界面
<RelativeLayout 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.android_data_storage.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="用戶名:" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginTop="25dp"
android:text="密碼:" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="21dp"
android:layout_marginTop="151dp"
android:text="登錄" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText1"
android:layout_marginRight="41dp"
android:text="取消" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="21dp"
android:text="記住用戶名" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignRight="@+id/button2"
android:text="靜音登錄" />
</RelativeLayout>
在MainActivity.java寫業務邏輯如下package com.example.android_data_storage;
import java.util.HashMap;
import java.util.Map;
import com.example.android_data_storage.sharepreference.LoginService;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private Button button;
private EditText editText, editText2;
private CheckBox checkBox;// 記住用戶名
private CheckBox checkBox2;// 靜音登錄
private LoginService service;
Map<String, ?> map = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
service = new LoginService(this);
button = (Button) this.findViewById(R.id.button1);
editText = (EditText) this.findViewById(R.id.editText1);
editText2 = (EditText) this.findViewById(R.id.editText2);
checkBox = (CheckBox) this.findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) this.findViewById(R.id.checkBox2);
map = service.getSharePreference("login");//提取數據,第一次進來的時候要讀文件
if (map != null && !map.isEmpty()) {
editText.setText(map.get("username").toString());
checkBox.setChecked((Boolean) map.get("isname"));
checkBox2.setChecked((Boolean) map.get("isquiet"));
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (editText.getText().toString().trim().equals("admin")) {
Map<String, Object> map = new HashMap<String, Object>();
if(checkBox.isChecked()){
map.put("username", editText.getText().toString().trim());
}else{
map.put("username", "");
}
map.put("isname", checkBox.isChecked());
map.put("isquiet", checkBox2.isChecked());
service.saveSharedPreferences("login", map);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
第一次運行界面如下
②Internal Storage
③External Storage
④SQLite Databases
⑤Network Connection