Android close dialog after 5 seconds?

后端 未结 8 800
北海茫月
北海茫月 2020-12-05 02:00

I\'m working on an accesibility app. When the user wants to leave the app I show a dialog where he has to confirm he wants to leave, if he doesn\'t confirm after 5 seconds t

相关标签:
8条回答
  • 2020-12-05 02:44

    Use CountDownTimer to achieve.

          final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
                .setTitle("Leaving launcher").setMessage(
                        "Are you sure you want to leave the launcher?");
           dialog.setPositiveButton("Confirm",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                         exitLauncher();
    
                    }
                });
        final AlertDialog alert = dialog.create();
        alert.show();
    
        new CountDownTimer(5000, 1000) {
    
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
    
                alert.dismiss();
            }
        }.start();
    
    0 讨论(0)
  • 2020-12-05 02:49
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
    dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            exitLauncher();
        }
    });     
    final AlertDialog alert = dialog.create();
    alert.show();
    
    // Hide after some seconds
    final Handler handler  = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (alert.isShowing()) {
                alert.dismiss();
            }
        }
    };
    
    alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            handler.removeCallbacks(runnable);
        }
    });
    
    handler.postDelayed(runnable, 10000);
    
    0 讨论(0)
  • 2020-12-05 02:51

    Late, but I thought this might be useful for anyone using RxJava in their application.

    RxJava comes with an operator called .timer() which will create an Observable which will fire onNext() only once after a given duration of time and then call onComplete(). This is very useful and avoids having to create a Handler or Runnable.

    More information on this operator can be found in the ReactiveX Documentation

    // Wait afterDelay milliseconds before triggering call
    Subscription subscription = Observable
            .timer(5000, TimeUnit.MILLISECONDS) // 5000ms = 5s
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Long>() {
                @Override
                public void call(Long aLong) {
                    // Remove your AlertDialog here
                }
            });
    

    You can cancel behavior triggered by the timer by unsubscribing from the observable on a button click. So if the user manually closes the alert, call subscription.unsubscribe() and it has the effect of canceling the timer.

    0 讨论(0)
  • 2020-12-05 02:51
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.game_message);
            game_message = builder.create();
            game_message.show();
    
    
            final Timer t = new Timer();
            t.schedule(new TimerTask() {
                public void run() {
                    game_message.dismiss(); // when the task active then close the dialog
                    t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                }
            }, 5000);
    

    Reference : https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/

    0 讨论(0)
  • 2020-12-05 02:51

    For Kotlin inspired by Tahirhan's answer. This is what worked for my current project. Hope it will help someone else in the near future. Im calling this function in a fragment. Happy coding!

     fun showAlert(message: String) {
            val builder = AlertDialog.Builder(activity)
            builder.setMessage(message)
    
            val alert = builder.create()
            alert.show()
    
            val timer = Timer()
            timer.schedule(object : TimerTask() {
                override fun run() {
                    alert.dismiss()
                    timer.cancel()
                }
            }, 5000)
        }
    
    0 讨论(0)
  • 2020-12-05 02:52

    I added automatic dismiss with the time remaining shown in the positive button text to an AlertDialog.

    AlertDialog dialog = new AlertDialog.Builder(getContext())
            .setTitle(R.string.display_locked_title)
            .setMessage(R.string.display_locked_message)
            .setPositiveButton(R.string.button_dismiss, null)
            .create();
    
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            final Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            final CharSequence positiveButtonText = positiveButton.getText();
            new CountDownTimer(AUTO_DISMISS_MILLIS, 100) {
                @Override
                public void onTick(long millisUntilFinished) {
                    positiveButton.setText(String.format(Locale.getDefault(), "%s (%d)",
                            positiveButtonText,
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) + 1));
                }
    
                @Override
                public void onFinish() {
                    dismiss();
                }
            }.start();
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题