问题
Hi in my application I am accepting pass code from user and storing it in shared preferences. And, I have provided pass code on/off functionality. If user checked on , app will ask user to enter pass code at the time of launch every time and will be off on checked of Off button.
Everything is working fine on android's ICS, Jellybean version and below. But it never works on Kitkat. Unfortunately, I don't have a Kitkat device to debug my app. Below is the my code I am using for shared preferences:-
SharedPreferences sharedPreferences;
//sharedPreferences = getActivity().getSharedPreferences("ServerData", Context.MODE_PRIVATE);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Editor editor = sharedPreferences.edit();
editor.putString("Passcode", Globals.str_Passcode);
editor.commit();
Does kitkat has any issues related to shared preferences. Please tell me how to resolve this? Or is there any other way to access pass code functionality i same way so it will be accessible in all versions.
回答1:
Try this way,hope this will help you to solve your problem.
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("Passcode", Globals.str_Passcode);
editor.commit();
sharedPreferences.getBoolean("Passcode",false);
回答2:
I had a similar problem. It was this missing from MainActivity,
prefs = PreferenceManager.getDefaultSharedPreferences(this);
PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
来源:https://stackoverflow.com/questions/24712821/issue-with-sharedpreferences-in-kitkat-version-in-android