我有一个简单的Android AsyncTask,后台操作很长:
HANDLER.post(new Runnable() {
@Override
public void run() {
new AsyncTask<Context, Integer, GameTypeAdapter>() {
@Override
protected GameTypeAdapter doInBackground(Context... contexts) {
return new GameTypeAdapter(contexts[0]);//Long database action
}
@Override
protected void onPreExecute() {
pbWaiter.setVisible(View.VISIBLE);
}
@Override
protected void onPostExecute(GameTypeAdapter gameTypeAdapter) {
txtGameName.setText(settings.getGameName());
cmbType.setAdapter(gameTypeAdapter);
if (settings.getGamePlugin() != null) {
cmbType.setSelection(((GameTypeAdapter) cmbType.getAdapter()).getItemPosition(settings.getGamePlugin()));
}
sldJoker.setProgress(settings.getJoker());
pbWaiter.setVisible(View.INVISIBLE);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity());
}
});
如果线程调用onPostExecute方法并调用最后一行(hideWaiter),则在1/10秒(调试模式下半秒)后可以看到UI调用的执行。在这段时间,UI挂起,服务员动画停止。
为什么UI线程会停止?
感谢。