user3423033
user3423033

Reputation: 11

How to show a dialog box three days once in an android application

How can I show a dialog box once every three days in an Android application?

Upvotes: 0

Views: 1064

Answers (3)

Shakeeb Ayaz
Shakeeb Ayaz

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

Tech Agent
Tech Agent

Reputation: 657

You need to use alarmmanager to trigger the event.

Upvotes: 0

eski
eski

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

Related Questions