Let ScheduledThreadPoolExecutor execute tasks on UI thread

穿精又带淫゛_ 提交于 2019-12-24 03:23:36

问题


How can I use a ScheduledThreadPoolExecutor and schedule a task which is always executed on the UI thread?

Currently, I do the following

    mScheduledTask = sBackgroundExecutor.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            if (mActivity != null) {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // do UI stuff
                    }
                });
            }
        }
    }, 0, 200, TimeUnit.MILLISECONDS);

This seems unnecessary and hard to read, but I couldn't find anything in the docs. Is it possible or is this the only way?

来源:https://stackoverflow.com/questions/29078627/let-scheduledthreadpoolexecutor-execute-tasks-on-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!