save time stamp in time picker - android

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.

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.

Get shared preferences specific value last updated date and time

I am using shared preferences to store userId once user login to the app. I need to expire(delete) the stored userId after 30 days once the userId is stored on shared preferences.
Is it possible to get the last modified date and time of specific shared preference value.
It's not possible, SharedPreferences don't have any built in method to do this. You'll have to store last modification date manually, possible in SharedPreferences as well.
I would say you can get the path of the SharedPreferences file and try using lastModified() of File class like,
File file = new File("path of shared-preference file");
file.lastModified();
Actually you could create an Alarm: you can set it to run within 30 days from the "registration" for the value stored in SharedPreferences.
You can then use a Receiver (search for more examples) to catch the alarm and run your code.
An example here

How to make android app load data from CSV to SQLite once?

I am loading getting data from a csv file then inserting them into SQLite at the launch of my app but it does this every time I open my app. Is there a way that that the app will only iterate through the CSV only on the first use of the app so that the succeeding launches will be quicker?
Thank you in advance :)
You can use SharedPreferences to determine if you already parsed your csv file.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("someKey", "1");
editor.commit();
And before you run your task you should check if you had manipulated your csv already by checking if the "someKey" exsit.
There are several ways that you can avoid to call for CSV file at each time.
You can save a small data (Saving Key-Value Sets) on the app setting using SharedPrefernce.
You can save a small int value on shared preference that indicates you have already called the CSV file or not.
At the beginning you have to call for the CSV load function with in a if conduction that checks whether the sharedpreference value has 1 or 0
if the shared pererence value has 0 the u will enter to the function and insert to the database. after that with in the if condition you will change the shared preference value to 1..
so the next time when you open the app, the shared preference value will be 1... if condition will not be satisfied.. the function will not be called...
Hope u understand

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

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

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.

Categories

Resources