SharedPreferences是Android系統提供的一種輕量級數據存儲方式,其數據存儲是以key-value對方式存儲基本數據類型:整型、布爾型、長整型和字符串,常用來存儲應用的配置信息、用戶設置參數等數據量不大的數據。
SharedPreferences本身是一個接口,程序無法直接創建SharedPreferences實例,實際開發中會根據以下方法獲取對應的SharedPreferences對象:
- Context:getSharedPreferences(String name,int mode) ,獲取文件名對應的SharedPreferences對象,name:對應的XML文件名稱,mode:訪問模式。
- Activity: getPreferences(int mode) 只需指定mode即可,默認採用所在的類名作爲xml文件的名稱
- PreferenceManager: getDefaultSharedPreferences(Context); 每個應用有一個默認的偏好文preferences.xml,使用getDefaultSharedPreferences獲取。
Mode參數常用值:
- Context.MODE_PRIVATE: SP中的數據只能被本應用程序讀寫。
- Context.MODE_WORLD_READABLE: SP中的數據能被其他應用程序讀,但不能寫。
- Context.MODE_WORLD_WRITEABLE: SP中的數據能被其他應用程序讀、寫。
SharedPreferences常用方法:
- boolean contains(String key) 判斷是否包含某個配置信息
- SharedPreferences.Editor edit() 獲得Preferences編輯的Editor對象
- Map<String ,?>getAll() 獲取所有配置信息
- XXX getXXX(String key,XXX defValue) 從配置文件中獲取對應的信息
SharedPreferences接口本身沒有提供寫入數據的能力,通過SharedPreferences的內部接口實現。
SharedPreferences調用edit()方法獲取它對應的Editor對象,Editor提供了方法向sharedPreferences寫入數據。
SharedPreferences.Editor常用方法
- SharedPreferences.Editor clear():清空SharedPreferences中的數據。
- SharedPreferences.Editor putXxx(String key, xxx Value)向SharedPreferences存入指定key對應的數據。
- Boolean commit():當Editor編輯完成後,調用該方法提交修改。
存儲和獲取數據:
//實現數據存儲
SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("stuId",strStuId);
editor.putString("stuName",strStuName);
editor.commit();
//獲取數據
SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
String strStuId=sharedPreferences.getString("stuId","");
String strStuName=sharedPreferences.getString("stuName","");
案例:記住密碼功能
佈局文件:activity_shared_preferences_save.xml
<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:layout_margin="30dp"
tools:contex**加粗樣式**t=".SharedPreferencesSaveActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學號:"
android:id="@+id/txtStuId"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="75dp"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:id="@+id/txtStuName"
android:layout_below="@+id/txtStuId"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtStuId"
android:layout_alignTop="@+id/txtStuId"
android:layout_toEndOf="@+id/txtStuId"
android:layout_toRightOf="@+id/txtStuId" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtStuName"
android:layout_below="@+id/edtStuId"
android:layout_alignStart="@+id/edtStuId"
android:layout_alignLeft="@+id/edtStuId" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edtStuName"
android:layout_marginTop="5dp">
<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:text="Remember password"
android:textSize="16sp"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:id="@+id/btnLogin"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
運行代碼:
public class SharedPreferencesSaveActivity extends AppCompatActivity {
private Button btnLogin;
private EditText edtStuId,edtStuName;
private CheckBox rememberPass;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences_save);
pref = getSharedPreferences("data", Context.MODE_PRIVATE);
btnLogin=(Button) findViewById(R.id.btnLogin);
edtStuId=(EditText)findViewById(R.id.edtStuId);
edtStuName=(EditText)findViewById(R.id.edtStuName);
rememberPass=(CheckBox)findViewById(R.id.remember_pass);
boolean isRemember = pref.getBoolean("remember_password",false);
if(isRemember){
String stuId=pref.getString("stuId","");
String stuName=pref.getString("stuName","");
edtStuId.setText(stuId);
edtStuName.setText(stuName);
rememberPass.setChecked(true);
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strStuId=edtStuId.getText().toString();
String strStuName=edtStuName.getText().toString();
editor=pref.edit();
if(rememberPass.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("stuId",strStuId);
editor.putString("stuName",strStuName);
}else{
editor.clear();
}
editor.commit();
Intent intent=new Intent(SharedPreferencesSaveActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
運行結果:
讀取其他的SharedPreferences,當應用程序創建的SharedPreferences指定了可被其他應用訪問的權限時,該SharedPreferences中的數據可以被其他程序讀取。
在應用程序中訪問其他程序創建的SharedPreferences的步驟:
1、創建其他程序對應的Context;
2、調用其他應用的Context的getSharedPreferences(String name,int mode)獲得相應的SharedPreferences對象;
3、如果需要寫入數據,調用SharedPreferences的edit()方法獲得相應的Editor即可。