如何将 Looper 与 asyncTask 一起使用?
调用 doImBackground 方法以编程方式创建名为 MyResultTable 的 TableLayout。而 MyResultTable 有一个处理程序,在 MotionEvent.ACTION_MOVE
期间由 onTouch 调用。
在收到关于在 asynchTask 中使用句柄的投诉后,我决定在 UIThread 上运行处理程序。但这导致我的 onTouch 方法响应缓慢。所以我的问题是,如何将循环程序与 asyncTask 一起使用?
UIThread代码:
activity.runOnUiThread(new Runnable() {
public void run() {
handler.post(mResizeViews);
}
});
我的 Looper 尝试:(不工作:黑屏)
protected MyResultTable doInBackground(Void... params) {
Looper.prepare();
MyResultTable table = new MyResultTable(context, other);
Looper.loop();
return tabl;
}
最佳答案
How would I use a Looper with an asyncTask?
你不会。
The doImBackground method is called to programmatically create a TableLayout called MyResultTable.
这不是 doInBackground()
的合适角色。使用 doInBackground()
处理缓慢的事情:网络 I/O、磁盘 I/O 等。使用 onPostExecute()
生成 UI(“名为 MyResultTable 的 TableLayout”)基于 doInBackground()
检索到的数据。
The MyResultTable, in turn, has a handler that is called by onTouch during MotionEvent.ACTION_MOVE.
这没有多大意义。您使用 Handler
将事件从后台线程转发到主应用程序线程。您的 onTouchEvent()
是在主应用程序线程上调用的,因此它不需要 Handler
。
关于android - 我如何将 Looper 与 asyncTask 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047466/