import android.app.Activity; import android.content.SharedPreferences; public class Calc extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; . . . @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . //載入設定檔 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //或者使用 SharedPreferences settings = getPreferences(0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); //寫入設定檔。可以使用SharedPreferences.Editor來輔助解決。 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); editor.commit(); //一定要記得提交 //或者再簡單化一可以這樣寫 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); setting.edit().putBoolean(“silentMode”,mSilentMode).commit(); } } |