0

How to pop up an alert dialog after 30 days of the initial first-time launch of the app by the user.

I want to display an alert dialog after 30 days of using the app, the method of accessing the calendar works, but then when the user changes the date of the phone using settings the method fails.

What is the idle way for this to work? Can this be achieved using Sessions, Handlers or Runnable methods?

  • No.Using Handlers is not a good idea. Just get the Epoch and save it somewhere like shared preferences and then check it every time that if it is one month later. – Omid.N Mar 12 '20 at 10:13

3 Answers3

1

On first launch of the app, get the current epoch time and save it in shared preferences as say, FIRST_LAUNCH_SECONDS. On every subsequent launch, check if current epoch time is greater than FIRST_LAUNCH_SECONDS by 2592000 seconds. (30 days = 2592000 seconds)

In main activity onCreate, call the following method:

void showAlertAfterOneMonth(){ 
    long currentTime = System.currentTimeMillis() / 1000;      //in seconds

    SharedPreferences prefs = getSharedPreferences("SHARED_PREF", MODE_PRIVATE);
    long firstLaunchTime = pref.getLong("FIRST_LAUNCH_SECONDS", currentTime)
    long isAlertShown = pref.getBoolean("ALERT_SHOWN", false)

    if (isAlertShown){
        // alert has already been shown once after one month. so do nothing.
        return
    }
    else if (currentTime - firstLaunchTime == 0){
        // this is first launch, set the time in shared preference
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putString("FIRST_LAUNCH_SECONDS", currentTime);
        editor.commit(); 
    }
    else if(currentTime - firstLaunchTime >= 2592000){
        // one month has completed after first launch, show alert.

        // Save a flag in shared preference so that alert will be shown 
        // only ONCE after one month is completed.
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putBoolean("ALERT_SHOWN", true);
        editor.commit(); 
    }
}

However, shared preferences will get cleared the app is reinstalled or if app cache is cleared.

UPDATE: Apparently, currentTimeMillis changes if user changes the system time. If you want more foolproof solution, you can use System.nanoTime() instead of System.currentTimeMillis().

Tony
  • 2,242
  • 22
  • 33
  • This doesn't solve the time changing trick. Users can set the time later. Then it wouldn't start in desired time. – Omid.N Mar 12 '20 at 10:59
  • Oh, it can. I just wrote it my program. I obtained `System.currentTimeMillis()` and logged it. The second time I set the time one month later and my number in Logcat was changed by one month. Just try it! – Omid.N Mar 12 '20 at 11:06
  • you are right System.nanoTime() does not change by changing wall-clock. But I calculated its origin and it was only 4.5 hours ago. – Omid.N Mar 12 '20 at 12:01
0

I guess you save the date on the first day somewhere in a file or a database. Why you just don’t take the date from an online clock instead of the device. Here is an example for that: how to make my java app get global time from some online clock

You simply can save the date in a file and compare it with the current time (from server) every time the user opens the app.

0

In order to avoid the user's hack to change the date, you have to get the time from an time server.

This post will help you out:

How to get current time from internet in android

So for the rest, you can just save the time fetched from the time server in your shared preferences or any other method mentioned here:

How to store data locally in an Android app

and then you either run a check when you startup the application or, if the application is running for 30 days straigh, you can set up a background thread/asyncstask with sleep so that to check it periodically.

Probably using asynctask and a while loop is not a good practice so I would check for the best solution online.

As for the comment on the answer of user9828889, it doesn't seem that you have any other solution other than online checking of the current time.

Painful
  • 137
  • 1
  • 11
  • What say you about this: I once installed photoshop and then when it started my free trial counting I set the time a little later so my free trial is longer but it didn't work even though I didn't connect to the internet. – Omid.N Mar 12 '20 at 10:56