Shared Preferences KEY_FIRST_RUN is not working in android 1.6?
I am using the following code to check the application is first time running or not
SharedPreferences pref = getPreferences(MODE_PRIVATE);
if (!pref.contains(KEY_FIRST_RUN)) {
Some operation
}
pref.edit().putBoolean(K开发者_如何学运维EY_FIRST_RUN, false).commit();
It works all the android versions except android 1.6
Please any one help me any error in my program
Give me some suggestion
The following works for me:
private static String KEY_FIRST_RUN = "";
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sharedPreferences = getPreferences(MODE_PRIVATE);
if (!sharedPreferences.contains("KEY_FIRST_RUN")) {
KEY_FIRST_RUN = "something";
Log.d("First", "First run!");
} else {
Log.d("Second...", "Second run...!");
}
editor = sharedPreferences.edit();
editor.putString("KEY_FIRST_RUN", KEY_FIRST_RUN);
editor.commit();
}
精彩评论