I am developing Android app in which i got stuck at one point,
What i want to do is,
When user launches the app for the first time in a day, i want to show him a some alert. And when he opens a app for the second time in same day it will not get an alert. (he will get an alert only for the first launch of app in day).
next day again if he opens the app for the first time again he will get alert and on second time he will not get an alert.
In short: User should get alert on first launch of each day.
Any idea, how should i achieve this ? Thanks in advance.
SharedPrefrences
will help you to accomplish your task.While opening the application just check the value of someboolean
variable from the shared preferences.Set an alarm usingAlarmManager
which set the value of someboolean
variable tofalse
everyday in yoursharedpreferences
at00:00
through a service and then change the value of that variable totrue
when the user opens your application.If the variable is alreadytrue
then nothing will happen for the rest of the day .In actual you have to check the value every time your application starts,nothing must turn that variable to false except that service that will run at 00:00How to use SharedPreferences
see this Alarm Manager Example
We can achieve this via a shared preference. In your first activity, have a method which does the following step by step in oncreatemethod:
Sample code:
A more explicit example of Nishanthi's solution that works great for me:
Please see below code.
1. DateHelper class.
2. SharedPreference class.
3.Implement function showMessageOnceDay in Activity class.
It is not a good solution, but I won't show message until user kill the app. Here is my code:
if (isFirstLaunch){ //show dialog isFirstLauch = false; }