SharedPreferences là gì?
SharedPreferences lưu các dữ liệu thô dưới dạng các cặp khóa và giá trị (key-value) vào các tập tin của ứng dụng. Bạn cũng có thể chọn một chế độ lưu trữ riêng tư (PRIVATE) mà các ứng dụng khác không thể truy cập vào các tập tin này, chính vì vậy nó là an toàn.
Ví dụ: bạn cần lưu thông tin của 1 ứng dụng game như âm lượng, độ sáng màn hình, … thì có thể sử dụng SharedPreferences giống như hình minh hoạ dưới
Ví dụ minh hoạ
Sử dụng Android Studio, tạo một project Android, đặt tên: DemoSharedPreferences
Thiết kế giao diện cho ứng dụng như sau (tập tin activity_main.xml):
xml code của tập tin activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="37dp" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" android:text="Game Settings" android:textSize="24sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="24dp" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" android:text="Sound:" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <SeekBar android:id="@+id/seekBar_sound" android:layout_width="0dp" android:layout_height="24dp" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="22dp" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" android:text="Bightness:" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/seekBar_sound" /> <SeekBar android:id="@+id/seekBar_brightness" android:layout_width="0dp" android:layout_height="22dp" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView3" /> <TextView android:id="@+id/textView4" android:layout_width="0dp" android:layout_height="27dp" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" android:text="Difficulty Level:" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/seekBar_brightness" /> <RadioGroup android:id="@+id/radioGroup_diffLevel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="24dp" android:layout_marginEnd="24dp" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView4"> <RadioButton android:id="@+id/radioButton_easy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Easy" /> <RadioButton android:id="@+id/radioButton_medium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium" /> <RadioButton android:id="@+id/radioButton_hard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hard" /> </RadioGroup> <Button android:id="@+id/button_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="Save" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/radioGroup_diffLevel" /> </androidx.constraintlayout.widget.ConstraintLayout>
code của tập tin MainActivity.java
package vn.aptech.demosharedpreferences; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.SeekBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private SeekBar seekBarSound ; private SeekBar seekBarBrightness; private RadioGroup radioGroupDiffLevel; private RadioButton radioButtonEasy; private RadioButton radioButtonMedium; private RadioButton radioButtonHard; private Button buttonSave; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.seekBarBrightness= (SeekBar)this.findViewById(R.id.seekBar_brightness); this.seekBarSound= (SeekBar)this.findViewById(R.id.seekBar_sound); this.seekBarBrightness.setMax(100); this.seekBarSound.setMax(100); this.radioGroupDiffLevel= (RadioGroup) this.findViewById(R.id.radioGroup_diffLevel); this.radioButtonEasy=(RadioButton) this.findViewById(R.id.radioButton_easy); this.radioButtonMedium = (RadioButton)this.findViewById(R.id.radioButton_medium); this.radioButtonHard=(RadioButton) this.findViewById(R.id.radioButton_hard); this.buttonSave = (Button) this.findViewById(R.id.button_save); this.buttonSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MainActivity.this.doSave(view); } }); // Load saved game setting. this.loadGameSetting(); } private void loadGameSetting() { SharedPreferences sharedPreferences= this.getSharedPreferences("gameSetting", Context.MODE_PRIVATE); if(sharedPreferences!= null) { int brightness = sharedPreferences.getInt("brightness", 90); int sound = sharedPreferences.getInt("sound",95); int checkedRadioButtonId = sharedPreferences.getInt("checkedRadioButtonId", R.id.radioButton_medium); this.seekBarSound.setProgress(sound); this.seekBarBrightness.setProgress(brightness); this.radioGroupDiffLevel.check(checkedRadioButtonId); } else { this.radioGroupDiffLevel.check(R.id.radioButton_medium); Toast.makeText(this,"Use the default game setting",Toast.LENGTH_LONG).show(); } } // Called when user click to Save button. public void doSave(View view) { // The created file can only be accessed by the calling application // (or all applications sharing the same user ID). SharedPreferences sharedPreferences= this.getSharedPreferences("gameSetting", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("brightness", this.seekBarBrightness.getProgress()); editor.putInt("sound", this.seekBarSound.getProgress()); // Checked RadioButton ID. int checkedRadioButtonId = radioGroupDiffLevel.getCheckedRadioButtonId(); editor.putInt("checkedRadioButtonId", checkedRadioButtonId); // Save. editor.apply(); Toast.makeText(this,"Game Setting saved!",Toast.LENGTH_LONG).show(); } }