Close Dialog after 10 seconds open

2

I would like dialog to close after 10 seconds. I can only do this by clicking the button.

Follow the code.

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        WebView wv = new WebView (this);
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        wv.loadUrl("http://192.168.200.233:8888/Propagandas/Propagandas.html");
        wv.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        alert.setView(wv);
        alert.setNegativeButton("Fechar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            alert.show();
        Handler handler = new Handler();
        handler.postDelayed( this, 2000 );

Is it possible?

    
asked by anonymous 14.09.2017 / 19:39

1 answer

6

10 Seconds in milliseconds would be 10000 . Here is an example of how you would use the setOnDismissListener() method of your dialog box:

Note: Create a variable of type Dialog to get your alert.show() , so you can use the dimiss() method.

... 
//alert.show();
final Dialog dialog = alert.show();

final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // verificar se a caixa de diálogo está visível
        if (dialog.isShowing()) {
            // fecha a caixa de diálogo
            dialog.dismiss();
        }
    }
};

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(runnable);
    }
});

handler.postDelayed(runnable, 10000);

Then you need to declare your AlertDialog.Builder globally, out of onCreate . See:

public AlertDialog.Builder alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    alert = new AlertDialog.Builder(this);
    // restante do conteúdo
    
14.09.2017 / 19:47