I have an alarm notification with operating normally while the phone is connected, but it reiniciaá the prefrências are reset. How to save?
You have a number of choices for persisting information. The two most common are
Sqlite database
Using preferences.
Example to set a preference:
SharedPreferences prefs = context.getSharedPreferences(<Unique string>, Activity.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putBoolean(Strings.STATUS_INTERNET, isInternetConnected);
editor.putString(Strings.USER_NAME, userName);
editor.commit();
Example to get a preference:
isDefaultAccountEnabled = prefs.getBoolean(Strings.DEFAULT_ACCOUNT, true);
Related
I'm doing a quiz where the user unlocked the levels, but when he closes the app all progress is lost, and he has to redo everything again. How can I fix this and make the user's progress automatically saved? Or with it by clicking a button, I do not know
You can save it in SharedPreferences and check the value of it every time application is opened.
To save value:
SharedPreferences sp = getSharedPreferences("YourFileName", Context.MODE_PRIVATE); //YourFileName= Any file name you give it
SharedPreferences.Editor editor = sp.edit();
editor.putString("levels", your_level_value); //levels=key at which data is stored, your_level_value=No. of levels completed
editor.apply();
To retrieve value:
SharedPreferences sp = getSharedPreferences("YourFileName", Context.MODE_PRIVATE); //YourFileName= Any file name you give it
String levelCompleted = sp.getString("levels", "0"); //levels=key at which no. of levels is saved.
The app I am developing should know if the user/android* has cleared cache or clear data , so that I logout the user. How to do this? How to find out if user has cleared cache?
Can the android OS clear the cache of an app by itself (without human intervention)?
Use SharedPreference to store value in Application cache
SharedPreference prefs = getSharedPreferences("UserInfo", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
when App started Retrieve data from SharedPreference
SharedPreference prefs = getSharedPreferences("UserInfo", 0);
String username = prefs.getString("username","");
String password = prefs.getString("password","");
if cache is cleared SharedPreference also cleared.so you have to make a condition like if username and password empty means not enter Application.
Im creating a alarm app that will have multiple alarm.. so i want to store the times in shared preference. if there is any solution to store integer list in shared preference
SharedPreferences preferences = getSharedPreferences("prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("key", yourIntValue);
editor.apply();
Use editor.apply() to save preferences asynchronously. apply() is preferable in the most cases.
Use editor.commit() to save preferences synchronously.
We are facing an issue with android SharedPreferences in our application. Our application is a cordova application. We are storing few data in the SharedPreferences. For example, storing userid, last visited page html contents, few json values.
Following are code samples for managing sharedpreferences (get/set/remove).
//get
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
Object obj = sharedPrefs.getAll().get(key);
//remove
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
if (sharedPrefs.contains(key)) {
Editor editor = sharedPrefs.edit();
editor.remove(key);
}
//store
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
Editor editor = sharedPrefs.edit();
editor.putString(key, value);
editor.commit();
The storing retrieving deleting everything working fine. The problem we are facing is, the data is not persisting in a consistent manner.
For example, user opens the app and then he force close the app, again takes the app. All data in the sharedpreference is persists. But when he force close 2nd time and come back to the app once again, then the data from the sharedpreference is deleted. I could not find any log in the logcat also. Any help will be much appreciated.
I want to make a reminder application without using a database. How can I save reminders (like reminder 1, reminder 2 goes on up to 3) in list view items so that by clicking any list view item, I can open that reminder set by me earlier?
There is a number of storage options available in Android. May be simply storing in files will work for you.
Well just for three reminders I might go for SharedPreferences
Use Shared Preferences for that.
Use below code for save data into shared preferences
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();
Use below code for get data from shared preferences
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
Shared Preferences
Internal Sotrage
External Storage
SqLite Databases
Network Storage