today date only once toast message show - android

I'm implementing the concept of if today date is present one alert message show one time only.
Example 5-8-14 only once toast message show.
6-8-14 only once toast message show
'
in every date only once toast show.
Edit:
Only after first start of App, a toast with current date should appear. If I start my app second or third time, then there should no toast appear

Logic:
What:
1. When ever you show the toast, Save the date/day in sharedPreferences.
2. Then each time, compare the value of "today's date" with the sharedPref value, if its different, show the toast.
How:
- Make a function for the toast, inside which update the Shared Pref value.
- In an "if" loop, compare the default shared pref value - so when you run the app for the first time, it'l return the default value and enter the show toast function.
Eg.
if(!(sharedPrefSavedDate.equals(sharedPrefDefaultValue))){
if(!(String.valueOf(new SimpleDateFormat("dd").format(new java.util.Date())).equals(sharedPrefSavedDate)){
showToast();
}
}
Inside showToast(), save the value of today's date in Shared Preferences.
The saved value can be integer or String. Can change .equals to != of = accordingly.
May require:
How to use shared preferences
Example with integer on android developers

Look into sharedPreferences.
Here a Link to android developer: sharedPreferences
Set a bool-value after first start and after each start check this value and if it's true, then don't show toast
GetDate:
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy");
String strDate = sdf.format(c.getTime());
Link: Get current time and date
Write:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBool("Toast", True);
editor.putString("Date", strDate);
editor.commit();
Read:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
bool bToast = sharedPref.getBool("Toast", False);
String strDate = sharedPref.getString("Date", null);

Related

Android: set timer to allow opening activity

I have a spin to win activity in my app that user can use to get coins.
i would like to make that after the user use the spinner he would have to wait for 8 hours before being allowed to open the spin activity again.
how to do that ?
one way will be:
determine those 8 hours from the moment he spinned ->
long duration = System.currentTimeMillis() + your8hours;
save this data (sharedpreference) ->
SharedPreferences sharedPreferences = new SharedPreferences();
sharedPreferences = getSharedPreferences("MYDURATIONSAVED", Context.MODE_PRIVATE)
Editor editor = sharedPreferences.edit();
editor.putInt("durationSaved", duration);
editor.apply();
in the new session, you retrieve the data ->
long alreadyExistingDuration = sharedPreferences.getLong("durationSaved", -1);
and simply compare it in order to check if the user is allowed or not to spin again ->
long check = System.currentTimeMillis();
if(check >= alreadyExistingDuration){
allow...
}
if you would have put a sample of your code I would have use it
for example, if you use sqlite, firebase or files to store your data, it may have been a different answer
But the data that you need to store is primitive so sharedpreference should be enough

fetch integer value from sharedpreference

I use the following helper to save user input to sharedpreference:
protected void storeData(SharedPreferences.Editor editor,
String key, EditText et) {
String content = et.getText().toString();
if ("".equals(content)||" ".equals(content)) {
editor.remove(key);
} else {
editor.putString(key, content);
}
}
then
number1 = (EditText)findViewById(R.id.number1);
SharedPreferences sharedPreferences = getSharedPreferences("Database", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
storeData(editor,"number1", number1);
editor.commit();
I wish to ask if how can I retrieve that value as a integer, then use it for some calculation.
I been searching and found this , found that they use editor.putInt(key,content);
But is that possible to extract the value as integer straight from my method?
thank you.
Your storeData() method is setting a string (putString()). Shared prefs store typed values distinctly different. That is, you cannot put "1" as a string then get it our later as an integer.
You need to use put/getInt() consistently. Alternatively you could store it as a string, and again consistently get it as a string and coerce it to an int as you need.
Your really should be using putInt(), but you can also use Integer.parseInt("some string") to convert your String value to an integer.
Use editor.commit() inside the else part
A suggestion:
use editor.apply() instead of editor.commit() as commit handles the job in foreground whereas apply handles that asynchronously.
I know, the question has been asked long time agao. I came up with similar situation. So, the might help someone in the future.
//for integer values
port = (EditText) findViewById(R.id.devicePort);
int devicePort = Integer.parseInt(port.getText().toString());
editor.putInt(getString(R.string.devicePort), devicePort);
editor.apply();

next button functionality in android

I am developing an application in which I want to implement the next and previous functionality. User can go forward or backward on stories through this functionality. I have stored current name of story in shared preferences but I am stuck now. I don't have idea of how to read the name of story defined in string.xml. So what should I do to implement next/previous functionality.
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE);
String currstory= currentstory.getString("currentstory","mystory");
In sharedpreference I have stored the name of current story. So I will get the name of current story by getsharedpreferences() in anther activity. But I want to get the storyname of the previous story defined in the story.xml.
You can read string from string.xml, useing this code:
Resources resources = getResources();
final String storyName = resources.getString(R.string.story_name);
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE);
String currstory= currentstory.getString("currentstory", storyName );
And save:
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE);
Editor editor = currentstory.editor();
editor.putString("currentstory", "New Story Name");
editor.commit();
editor = null;

Edit SharedPreferences value?

I have problem understanding shared preferences. I have activity where user will insert password, start price, waiting price etc. My plan was to set starting value, and than user would change that value if he wants.
My question is: If I create prefs in onCreate() method, how change would apply (using SharedPreferences.Editor) when every time i run application it should create new values in prefs.
To obtain shared preferences, use the following method In your activity:
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To read preferences:
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();
No it will change the previous one by key....
http://mobile.tutsplus.com/tutorials/android/android-application-preferences/

How to use shared preferences here

I am new to android and java too. Can anyone please suggest how to use shared preferences to stack history of products searched and push and remove when it reaches a certain number.
I have product list in a listview in activity1 and product details in activity2.
In activity2 context menu I want to add Add to fav and History of searched
products. Once a product is added i want that fav context menu to disable. How can I stack
history..It is a offline app.....push and remove once the limit of history reaches.
How can I do that ..? Thank you..
//Obtain shared preferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
//obtain boolean value stored in preferences
boolean booelanExample = settings.getBoolean("boolean_example", false);
//obtain string value stored in preferences
String stringExample = settings.getString("string_example", "");
//Obtain settings editor put new values and commint again
Editor settingsEditor = PreferenceManager.getDefaultSharedPreferences(this).edit();
settingsEditor.putString("string_example", "stringvalue");
settingsEditor.putBoolean("boolean_example", false);
settingsEditor.commit();
To obtain shared preferences, use the following method
In your activity:
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To read preferences:
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long long = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

Categories

Resources