如果从Timer Task调用,则根本不显示Android Dialog

bar*_*ta7 1 multithreading android dialog timer

timer = new Timer("Timer Thread");

    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
                showDialog(0);
                timeBar.setProgress(time);
            }
        }
    }, INTERVAL, INTERVAL);`
Run Code Online (Sandbox Code Playgroud)

我的onCreateDialog方法工作正常,所以当我从Button使用showDialog(0)时它工作正常.但是如果调度程序调用该方法则不行.

Blu*_*ell 6

使用处理程序:

protected static final int DIALOG_OK= 0;

timer = new Timer("Timer Thread");

timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
            mDialogHandler.sendEmptyMessage(DIALOG_OK);
            timeBar.setProgress(time);
        }
    }
}, INTERVAL, INTERVAL);

 private Handler mDialogHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                    case DIALOG_OK:
                            // We are now back in the UI thread
                            showDialog(0);
                    }

            };
    };
Run Code Online (Sandbox Code Playgroud)

这允许您从其他线程调用UI线程上的方法.需要更多解释,请问.