Android preference SharedPreference-android learning Tour (6)
SharedPrefenence uses key-value pairs for storage and internal storage.
Instance
Public class MainActivity extends Activity {private SharedPreferences sp; private CheckBox cb = null; public static final String KEY_SHOW_DIALOG = "show_dialog"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // The first parameter is the preference name. If not, it is automatically created. The second parameter is the access method. This parameter can only be accessed by your own program or the same id, another method is to allow multi-process access. Sp = getSharedPreferences ("mysp", Context. MODE_PRIVATE); cb = (CheckBox) findViewById (R. id. cb); cb. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stub Editor edit = sp. edit (); edit. putBoolean (KEY_SHOW_DIALOG, isChecked); edit. commit () ;}}); cb. setChecked (sp. getBoolean (KEY_SHOW_DIALOG, false); if (cb. isChecked () {new AlertDialog. builder (this ). setTitle ("welcome "). setMessage ("Hello, welcome to use me "). setPositiveButton ("off", null );}}}
If this option is selected, a dialog box is displayed.