Reputation: 11
How can I show a dialog box once every three days in an Android application?
Upvotes: 0
Views: 1064
Reputation: 6096
code flow should be like this
1.Create an alarmmanger like
AlarmManager alarmanager=alarmanager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this,DialogService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,intent , 0);
alarmanager.setRepeating(AlarmManager.RTC_WAKEUP,
System.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 3,
pendingIntent );
2.Create a service DialogService
Here u create dialog in onStartCommand
Upvotes: 0
Reputation: 7915
Use SharedPreferences.
Before displaying the dialog check the preferences to get the time that it was last displayed. If it's been more than three days, display the dialog and set a new time in SharedPrefs.
long time = sharedPrefs.getLong("displayedTime", 0);
if (time < System.currentTimeMillis() - 259200000) {
displayDialog();
prefsEditor.putLong("displayedTime", System.currentTimeMillis()).commit();
}
Upvotes: 2