When button is clicked, save the time to a text file - android

So I want to build an app in Android Studio that will help my family and I quit smoking. I want the app to save the current time when the button is clicked. Then I want the app to display the time of "the last time you smoked" as well as all the all the previous "last time you smoked". I'm pretty new to java, could anyone help me or guide me in the direction?

Use SharedPreferences for that, like this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = preferences.edit();
editor.putString("LAST_SMOKE_DATE", new Date().toString());
editor.apply();
To recover the value use this:
String dateStr = preferences.getString("LAST_SMOKE_DATE", "");
You can format your date following some tutorials on StackOverflow:
Change date format in a Java string

I recommend SQLiteOpenHelper.
SQLiteOpenHelper Android Documentation
Each time the button is pressed, insert a new record into it. And when you want it to display, retrieve the record using a cursor object.

Related

How Do I Set The Value Of A String In Android?

I have a string and a button as follows:
String message = "Hello World"
button.OnClickListener(......){
message = "123456789";
}
So here is what I want,
When the app starts the string is "Hello World" but when the user clicks the button it changes to "123456789" and I want the string to change permanently for lifetime.
So when the user restarts the application or reinstall it the string is still
"123456789".I think this comes under Shared Preferences.
Please Help,I really need this
You can save that string in Shared Preferences, and get it all the from there. If is no value, you can get default value from resources or you can provide your own default String. Note that you can do that for lifetime. If user delete the app or clear cache, your view will display default value, Hello World.
Edit
You can use android:allowBackup="true" from manifest in order to keep old Shared Preference values.
There is a chance data from SharedPreferences is gone when you reinstall especially if you uninstall the app first, same case as local database using SQLite. You can try using database from your local server.
To save a String in SharedPreferences :
SharedPreferences.Editor editor = getSharedPreferences("com.package.name", Context.MODE_PRIVATE).edit();
editor.putString("keyName", message);
editor.apply();
To get it back :
SharedPreferences sharedPrefs = getSharedPreferences("com.package.name", Context.MODE_PRIVATE);
message = sharedPrefs.getString("keyName",defaultValue);
When the user modifies it, save it. When the app start, get it with as default value "Hello World" so that while the user hadn't click the button, it will stay as Hello World.
For Restarting Shared Preference is the best mechanism but for reinstall allowbackup works on API level 23 and above,
you can consider to update the value on server once you update in your Shared Preferences for the first time and during reinstall can do a API call check for the same for the particular user.

AndroidStudio saving data from bluetooth device

I am working with Android Studio 2.2.3 in order to watch data received from a bluetooth module in my smartphone's screen. I can see these values in my activity but as I am receiving values each 10 seconds (for example) I would like to save all this data in an array, database or anyplace. Once it was saved I would like to build a graphic using this saved data.
Here I have a picture of what I receive:
enter image description here
So now I would like to save those values (53,54,55...) and I don't know how. Is there any way like making an ArrayList or some function to save it? I read about "SharedPreferences" but I think it is not designed for what I am looking for.
Thank you very much!
P.D: I hope I explained okay and sorry for my english.
Shared Preferences could be, it allow to use Sets. You could convert your List into a HashSet or something similar and store it like that. When your read it back, convert it into an ArrayList.
//Retrieve the values
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Set<String> set = prefs .getStringSet("key", null);
yourListOfStrings.addAll(set);
//Set the values
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Set<String> set = new HashSet<String>();
set.addAll(yourListOfStrings);
prefs.putStringSet("key", set);
prefs.commit();
Regards,

Android Saving data on Button

I have a button in a customView which has several numbers on it individually,it counts backward
now my program doesn't keep value on it when user taps back and closes the program How can I prevent this ?
You can pass value from one activity to other using intents. If you want to persist data,the simplest way to achieve this would be saving it in Shared Preferences.
Developer link has good information on how to implement this -
You can save the value Key, Value pair and then retrieve it when the app is up again.
you can Use SharedPreferences its easy to write and get.
You can create one like this
SharedPreferences prefs = getSharedPreferences("yours", MODE_PRIVATE);
and write to like this
SharedPreferences.Editor prefsEditor=prefs.edit();
prefsEditor.putString("what you want", "value of what you want");
prefsEditor.apply();
and retrive like this
String s=prefs.getString("what you want", "value to be retrived if failur happens in retreiving")
if the value not important after end the program you can save it into public static variable
but if you need the variable after end the program you can use SharedPreferences
follow this tutorial http://developer.android.com/reference/android/content/SharedPreferences.html

save time stamp in time picker

I am developing a android application where I need time stamp should be saved in time picker even if we click back button in android.Since I am new to android I need help.Thanks in advance for help.
System.currentTimeMillis();
Will get you the current timestamp.If you want to save it somehow you may use SharedPrefences or sqlite db or something like this.
I think I would prefer SharedPreferences since you want the timestamp to be there after clicking the back button. Clicking that button might exit you application so some persistent storage would be fine. The timestampt is no POJO so SharePreferences are my first choice.
Something like that should help:
// That will save the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.edit().putLong("PREF_TAG_TIMESTMAP", System.currentTimeMillis()).commit();
pref.edit().clear().commit();
// That will get you the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.getLong("PREF_TAG_TIMESTMAP", -1.0);
If you need to persist you data for a later run of the activity you can do that using the sql database, shared preferences or if you only need to persist during the current run of the app (not for subsequent launched of the app) you could create a static variable holding the values you want to store for you.
From what I understand you should go with the static variable approach.

Android - ImageButton Values / SharedPreference

If you have time , please can you take some time for me ? I really need some help.
Let me explain;
Im working on an android app.
There is a layout and it has 5 ImageButton and a webview. When the users click on a imagebutton, without problem it calls a website below..but more or less i have 20 web site.i want to add an option for users..for example the user will choose a web site from Prefs. screen then automatically one of imagebuttons values (i mean icon AND its loadurl function) will change.
I created Pref Screen and i can see my website in this which i wrote in array.xml
but totally im not able to set them to Imagebuttons..
im beggin u.its our last curve..then it ll finish.
Im tried to use this code :
Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
but i couldnt.
Please explain me step by step clearly.
also i dont want that only for me,on internet there is no source for this issue.im searching and trying everything what i can think more than 6 days but nothing.
Thank you so much
SharedPreference Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
Basically what this does is allows you to store user information such as scores for a game, stats, and other variables.
The first line gets reference or creates the file to write the data to.
The second line allows you to edit the file to write new information to it..
e.putString()
Takes two parameters the first one being the Key to pull the value you out later, and the second being the value you want to put into the file.
The last line commits the data so that it is saved to the file.
You can get more info from the docs here
Also if you want to pull the data out just do
SharedPreference Data = getSharedPreferences(filename, 0);
String value = Data.getString("website"); // use the key here to pull the data out
EDIT:
So for example if the user selects a certain image you could use a key to refer to each image and get the value later to decided which icon the user picked before.

Categories

Resources