问题
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