4

i have an app that contains a dialog

i want to close this dialog after x second, when user haven't any interact with app, like volume seekbar popup(that's open when the volume button clicked, and closed after 2 second of inactivity). what is the simplest way to implement this?

thank you

4

4 回答 4

6

例如,您可以使用 Handler 并在每次用户与对话框交互时调用其 .removeCallbacks() 和 .postDelayed() 方法。

在交互时,.removeCallbacks() 方法将取消 .postDelayed() 的执行,然后,您使用 .postDelayed() 启动一个新的 Runnable

在这个 Runnable 中,您可以关闭对话框。

    // 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
        }
    });

要跟踪用户交互,您可以使用:

@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
}

在您的活动中可用。

于 2013-08-08T22:37:29.587 回答
1

我喜欢用 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();
        }

    }
}

然后在点击时从您的 Activity 触发它:

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);
    }
});

如果您需要更高的精度,您还可以使用 System.nanoTime()

于 2013-08-08T23:02:38.533 回答
0

这是我在查找有关超时的事情时出现的问题。我已经实现了一个使用AsyncTask,Handler和的答案Runnable。我在这里提供我的答案,作为未来答案搜索者的潜在模板。

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's 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

对于那些对使用有些陌生的人AsyncTask,您必须DownloadTask object拨打电话.execute()

例如:

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

实际上,通过让我的所有代码都通过一个函数完成,我实际上还比你看到的更进一步doInBackground(),所以我实际上不得不使用该对象调用onProgressUpdate()和其他函数。DownloadTask

于 2014-06-03T20:30:06.007 回答
0

我也是 android 的新手,但我建议创建一个计时器,当我们说计时器 t 大于或等于 2 时,你就做点什么。它看起来像这样

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

这可能不适用于您的目的,但最终可能需要更少的代码行。(正如我常说的,更少的代码就是更多的代码)我知道定时器基本上很容易制作,但我从未在应用程序中使用过定时器。我不会特别知道如何制作计时器,但我相信你可以在 youtube 上找到教程。

于 2013-08-08T23:54:40.047 回答