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即可,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/237092.html
標籤:其他
下一篇:Vue 實作撥打電話操作
