How to close a dialog after certain seconds of inactivity?

I have an application containing a dialog

I want to close this dialog box after x seconds when the user is not interacting with the application, for example, the volume search popup (which opens when you press the volume button and closes after 2 seconds of inactivity). What is the easiest way to implement this?

Thank you

+4
source share
4 answers

You can, for example, use the Handler and call its .removeCallbacks () and .postDelayed () methods every time the user interacts with the dialog.

When interacting, the .removeCallbacks () method cancels the execution of .postDelayed (), and immediately after that starts a new Runnable with .postDelayed ()

Inside this Runnable, you can close the dialog.

// a dialog final Dialog dialog = new Dialog(getApplicationContext()); // the code inside run() will be executed if .postDelayed() reaches its delay time final Runnable runnable = new Runnable() { @Override public void run() { dialog.dismiss(); // hide dialog } }; Button interaction = (Button) findViewById(R.id.bottom); final Handler h = new Handler(); // pressing the button is an "interaction" for example interaction.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { h.removeCallbacks(runnable); // cancel the running action (the hiding process) h.postDelayed(runnable, 5000); // start a new hiding process that will trigger after 5 seconds } }); 

To track user interactions, you can use:

 @Override public void onUserInteraction(){ h.removeCallbacks(runnable); // cancel the running action (the hiding process) h.postDelayed(runnable, 5000); // start a new hiding process that will trigger after 5 seconds } 

What is available in your business.

+6
source

I like to do this with AsyncTask:

 class ProgressDialogTask extends AsyncTask<Integer, Void, Void> { public static final int WAIT_LENGTH = 2000; private ProgressDialog dialog; public ProgressDialogTask(Activity activity) { dialog = new ProgressDialog(activity); } @Override protected void onPreExecute() { dialog.setMessage("Loading"); } @Override protected Void doInBackground(final Integer... i) { long start = System.currentTimeMillis(); while(!isCancelled()&&System.currentTimeMillis()-start< WAIT_LENGTH){} return null; } @Override protected void onPostExecute(final Void v) { if(dialog.isShowing()) { dialog.dismiss(); } } } 

Then activate it from your activity when pressed:

 Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ProgressDialogTask task = new ProgressDialogTask(this); task.execute(0); } }); 

If you need higher accuracy, you can also use System.nanoTime ()

+1
source

I am also new to android, but recommend creating a timer, and if we say that the timer t is greater than or equal to 2, you are doing something. It would look like this.

 if (t >= 2.0){ //Do whatever you want it to do } 

This may not work for your purposes, but it is just that in the end it may require fewer lines of code. (as I always say, less code is more code). I know that timers are basically easy to make, but I have never used them in an application. I would not know how to make a timer, but I'm sure you can find a youtube tutorial.

0
source

This is a question that arose when I was looking for things related to time. I followed an answer that uses AsyncTask , Handler and Runnable . I am providing my answer here as a potential template for future answer search engines.

 private class DownloadTask extends AsyncTask<Void, CharSequence, Void> { //timeout timer set here for 2 seconds public static final int timerEnd = 2000; private Handler timeHandler = new Handler(); @Override protected void onPreExecute() { ProgressDialog dProgress = new ProgressDialog(/*Context*/); dProgress.setMessage("Connecting..."); dProgress.setCancelable(false); dProgress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Dismissing dProgress dialog.dismiss(); //Removing any Runnables timeHandler.removeCallbacks(handleTimeout); //cancelling the AsyncTask cancel(true); //Displaying a confirmation dialog AlertDialog.Builder builder = new AlertDialog.Builder(/*Context*/); builder.setMessage("Download cancelled."); builder.setCancelable(false); builder.setPositiveButton("OK", null); builder.show(); } //End onClick() }); //End new OnClickListener() dProgress.show(); } //End onPreExecute() @Override protected Void doInBackground(Void... params) { //Do code stuff here //Somewhere, where you need, call this line to start the timer. timeHandler.postDelayed(handleTimeout, timerEnd); //when you need, call onProgressUpdate() to reset the timer and //output an updated message on dProgress. //... //When you're done, remove the timer runnable. timeHandler.removeCallbacks(handleTimeout); return null; } //End doInBackground() @Override protected void onProgressUpdate(CharSequence... values) { //Update dProgress text dProgress.setMessage(values[0]); //Reset the timer (remove and re-add) timeHandler.removeCallbacks(handleTimeout); timeHandler.postDelayed(handleTimeout, timerEnd); } //End onProgressUpdate() private Runnable handleTimeout = new Runnable() { public void run() { //Dismiss dProgress and bring up the timeout dialog dProgress.dismiss(); AlertDialog.Builder builder = new AlertDialog.Builder(/*Context*/); builder.setMessage("Download timed out."); builder.setCancelable(false); builder.setPositiveButton("OK", null); builder.show(); } }; //End Runnable() } //End DownloadTask class 

For those who are a little familiar with using AsyncTask , you should make a DownloadTask object and call .execute() .

For instance:

 DownloadTask dTaskObject = new DownloadTask(); dTaskObject.execute(); 

I also took this code further than what you see, having all the doInBackground() code executed through a function, so I really needed to call onProgressUpdate() and other functions using the DownloadTask object.

0
source

Source: https://habr.com/ru/post/1496060/


All Articles