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().