Home>

I wanted to delay execution while creating an Android application, so as follows
When I tried it, I got an error.

I couldn't solve it even if I looked it up ...
Thank you.

Error message
Error: Incompatible type: Unable to convert  to Context:
                AlertDialog.Builder builder = new AlertDialog.Builder (this);
Corresponding source code
mReplyDelay = (new Random (). nextInt (4) * 3000);
        new Handler (). postDelayed (new Runnable () {
            @Override
            public void run () {
                AlertDialog.Builder builder = new AlertDialog.Builder (this, 4);
                builder.setTitle ("Please Wait!");
                builder.setMessage ("Now Loading ...");
                builder.setCancelable (false);
                mDialog = builder.create ();
                Handler handler = new Handler ();
                Runnable dialogDismiss = new Runnable () {
                    public void run () {
                        mDialog.dismiss ();
                    }
                };
                mDialog.show ();
                handler.postDelayed (dialogDismiss, 20000);
            }
        }, mReplyDelay);
  • Answer # 1

    The command you want to execute

    new Handler (). postDelayed (new Runnable ()
    {...}

    I was able to make it a method outside of and call it inside.

    As follows

          mReplyDelay = (new Random (). nextInt (4) * 10000);
            new Handler (). postDelayed (new Runnable () {
                @Override
                public void run () {
                    dialog ();
                    Handler handler = new Handler ();
                    Runnable dialogDismiss = new Runnable () {
                        public void run () {
                            mDialog.dismiss ();
                        }
                    };
                    mDialog.show ();
                    handler.postDelayed (dialogDismiss, 20000);/*** /
                }
            }, mReplyDelay);
    }
    private void dialog () {
        AlertDialog.Builder builder = new AlertDialog.Builder (this, 4);
        builder.setTitle ("Please Wait!");
        builder.setMessage ("Now Loading ...");
        builder.setCancelable (false);
        mDialog = builder.create ();
    }