显示对话框几秒钟

Show dialog box for a few seconds

单击按钮后,它会调用显示对话框的函数。我希望对话框保留大约 4 秒,然后关闭,然后再打开 activity。使用我编写的代码,它等待 4 秒,显示对话框并立即进入下一个 activity。

  public void show()
      {  final Dialog dialog = new Dialog(invoice.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.popup);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = Math.round((width * 10) / 15);
        lp.height = Math.round(height / 3);
        dialog.getWindow().setAttributes(lp);

       Intent intent = new Intent(invoice.this, First_grid.class);
        intent.putExtra("yo", 0);
        try {


            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT >= 16) {
            //The code below is for the cool transition effect that happens once the player clicks the play button..!
            Bundle translateBundle = ActivityOptions.makeCustomAnimation(invoice.this,
                    R.anim.activity_slide_up, R.anim.activity_slide_out_upwards).toBundle();
            startActivity(intent, translateBundle);
            dialog.dismiss();
            finish();

        } else {
            startActivity(intent);
            dialog.dismiss();
            finish();

        }
        dialog.show();
        }

试试这个,

public void show(){
    final Dialog dialog = new Dialog(invoice.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.popup);

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = Math.round((width * 10) / 15);
    lp.height = Math.round(height / 3);
    dialog.getWindow().setAttributes(lp);    
    dialog.show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(invoice.this, First_grid.class);
            intent.putExtra("yo", 0);
            if (Build.VERSION.SDK_INT >= 16) {
                //The code below is for the cool transition effect that happens once the player clicks the play button..!
                Bundle translateBundle = ActivityOptions.makeCustomAnimation(
                    invoice.this,
                    R.anim.activity_slide_up, 
                    R.anim.activity_slide_out_upwards).toBundle();
                startActivity(intent, translateBundle);
                dialog.dismiss();
                finish();
            } else {
                startActivity(intent);
                dialog.dismiss();
                finish();
            }
        }
    }, 4000);
}