1

I am showing an alert when a user exits a certain area:

   /*Function to show alert when Geofence breached*/
   private void showAlert() {
       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Geofence Breached");
        builder.setMessage("User has breached the Geofence Boundary!");
        builder.setPositiveButton(android.R.string.ok, null);
        builder.show();
    }

And I am calling it like so:

 if( distance[0] > mCircle.getRadius()  ){                      
     showAlert();

}

Is there any way to set it that an alert only goes off every 2 minutes as the location is getting checked all the time then the notification keeps appearing. I have read about, Timers, Timertasks and alarmManagers but I dont think it will work for me. Any help would be appreciated.

Natalie Carr
  • 3,707
  • 3
  • 34
  • 68
  • You want to cancel the dialog after 3 mins from the time it opened ? – Triode Feb 24 '14 at 15:45
  • @RajeshCP No I seen a solution to that, my user may click OK and at the moment it displays another alert instantly, I want to set it that an alert can only be shown every 2 minutes if possible. – Natalie Carr Feb 24 '14 at 15:47

3 Answers3

1

Have a member variable in your Activity/Service to record the time when the dialog was last shown:

long timeDialogShown = 0;

When checking whether to display the dialog, compare the time now with the time that the dialog was last shown. If more than 2 minutes have elapsed, or if it never has been shown before, display the dialog and update the timestamp. Otherwise simply do nothing.

if( distance[0] > mCircle.getRadius() )
{
    long timeNow = System.currentTimeMillis()/1000;  //Timestamp in seconds
    if ( (timeNow - timeDialogShown) > 120 || timeDialogShown == 0)  //Show if 2 minutes have passed
    {
        timeDialogShown = System.currentTimeMillis()/1000; //Timestamp in seconds
        showAlert();
    }
}
NigelK
  • 8,255
  • 2
  • 30
  • 28
0

Here it is, sir.

final AlertDialog dialog = ....
new Handler().postDelayed(new Runnable() 
{
  public void run() 
  {
    dialog.dismiss();
  }
}, 1000 * 60 * 2);

It will dismiss the dialog after 2 mins (1000 ms * 60 sec * 2 mins)

Manitoba
  • 8,522
  • 11
  • 60
  • 122
  • Thank you, please see my comments, I am not looking to dismiss the dialog I am looking to set a delay between showing it. – Natalie Carr Feb 24 '14 at 16:03
0

See this -https://stackoverflow.com/a/6203816/881771 You need to setup a dismiss listener on your dialog, which blocks your showDialog() method for 2 mins.

  1. Maintain a boolean value that suggest if two mins have passed since last alert was shown/dismissed.

    boolean twoMinsElapsed=true;

  2. Inside your code where you are display alertDialog check this boolean value as well

    if (distance[0] > mCircle.getRadius() && twoMinsElapsed) {
               //block the showDialog() method until this value is set to true again
                twoMinsElapsed=false;
                showDialog();
            }
    
  3. Setup a dismisslistener for your alertdialog

    alertDialog.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                   //Set this boolean value back to true. will be called after two mins
                    twoMinsElapsed= true;
                }
    
            }, 120000);
    
        }
    });
    

Consider this as a pseudo code :)

Community
  • 1
  • 1
Gaurav
  • 840
  • 4
  • 16