I try to develop some fitness application which will use Google Fit.
And i have one question. Assume that user make some sport activity a few days in a week. And want to show him his best result. The only way I know I can do this - is load all data from user history and then select the set with maximum value.
And its obvious that I dont like that method because of the fact that I should load all the history. (And the user can have history for a few years).
Another way is to store data in local database (so i dont need to load all history every time the user want to see his best result) and synch it with google fit data, but I think its too dificult.
Do we have some other options for that kind of tasks?
Use sharedpreferences to save the value,
To set value
SharedPreferences prefs = this.getSharedPreferences("myhighestscore", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();
To get value
SharedPreferences prefs = this.getSharedPreferences("myhighestscore", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0);
here is a link.
Using Shared Preferences
Related
I want to create a game that counts the clicks made in 60 seconds and saves the record. I would like this record to be saved on the device even after the app is closed. Is there some kind of variable that allows me to do this?
Android's Shared Preferences seem to be the most relevant option for you.
Refer to the official documentation for an in depth look:
https://developer.android.com/training/data-storage/shared-preferences
These code samples should help you as well:
To save a value into the Shared Preferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key); //set a default/backup option
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
Make sure to keep the key identical between placing values into the preferences and retrieving.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
SharedPreferences is probably the place to start with this. You can view Android documentation related to it here along with code examples: Save key-value data
There are some other options for saving data in an Android app on the left pane of that page, but SharedPreferences is probably the most applicable based on your described use case.
I have a school planer app and I want to save the classes that are written in an EditText object. The problem is, the EditTexts are empty when I close the app and then open it again, and even when I press back. This may be a noob question, but I'm a beginner and I need help. Please answer quickly...
for saving edittext text you need to store the text into storage.
android support storage like
Database
Shared Preferences
For your problem you need to store your Edittext text into the storage when you change value.
And when you open it again you need to retrieve from the storage.
For you best option will be shared preferences. For more Go with This Example . This will make you understand save and retrieve shared preferences.
Short Example
Store Data
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = pref.edit();
editor.putString("first_edittext", "value");
editor.commit();
Retrieve Data
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
pref.getString("first_edittext", "");
I'd like my app to allow a single user to create a profile that is accessed every time they run the app. The user's profile contains a String entry and five int entries. I've thought about using an SQLite database to store the info between sessions, but it seems like overkill since I'd only need one row in a single table to store the data. However, I'm not sure if it's possible to use SharedPreferences on a String type of data.
Can anyone provide some insight into this issue please? Thank you!
Yes you can use SharedPreferences.
To write
Editor editor = getSharedPreferences("file_name", MODE_PRIVATE).edit();
editor.clear();
editor.putString("pref_name", myStringValue);
editor.commit();
To read back
SharedPreferences preferences = getSharedPreferences("file_name", MODE_PRIVATE);
String myStringValue = preferences.getString("pref_name", "default_value");
Regards.
i am new to android, i searched a lot and couldn't find a satisfying answer; what i need is to save some setting for my application such as
1> language, number of items to display, display/not display images, etc...
which i think is best done using the shared preferences
2> save which data categories to get from the internet...
here is my problem:
i have data divided into category objects with key, name, type, data[]...(data[] is changing all the time and not saved after exiting the application),(key, name, type are final values defined by programmer).
and because there are many categories which the user may or may not want to load(around 25), he/she can choose which categories to display, and these choices must be saved.
i think using shared preferences will not help because of the complexity of the data; i was thinking of using sqlite or xml, not sure which is the best choice keeping in mind efficiency and memory size.
note: i am using a global variable for the category information array, that is because data[] needs to be refreshed automatically every 2-3 minutes and must be available to all activities also efficiency and memory space are an issue.
i will appreciate any advice, thank you in advance.
1> SharedPreferences is the right choice
2> you have multiple possibilities
a) Use the internal storage and use Object Serialization (for simplicity reasons), xml (if you want to exchange the data) or use a own format
b) SQLite is the fastest solution. But you have to do more programming for that
b would be my choice, so create a DB-Object (static or singleton pattern) and write functions for every database task
1) Yes, SharedSettings should help youy. There is plenty information around, but post back if you are lost.
2) I would make a table with all the categories and a boolean value thats says "show" where you can keep if the user wants to show it or not.
Of course it depends on the size of the categories and how much they change, because otherwise you will be updating the database forever.
User shared preference in store your setting,
SharedPreferences sharedPreferences = this.applicationContext.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
//For saving the setting:
//for storing long
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
//for storing string editor.commit();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
//and similarly for int,float etc
//For Retrieving the string:
sharedPreferences.getLong(key, defaultValue); //for long value
sharedPreferences.getSting(key, defaultValue); //for string value
I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.