android - 我如何将 Looper 与 asyncTask 一起使用?

标签 android android-layout event-handling android-asynctask looper

如何将 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/

相关文章:

android - 处理屏幕方向 - Android

android - 在模块 guava-20.0.jar (com.google.guava :guava:20. 0) 中找到重复的类 com.google.common.util.concurrent.ListenableFuture

java - 为什么服务会在主进程停止时销毁?

android - RecyclerView 直到滚动后才显示

android - 如何让 Fragment 在其宿主 Activity 上转换阴影?

c++ - 在 C++ Builder 中覆盖事件处理程序

java - 时间表 Android 应用程序无法运行

android - 在 Listview 中使用 ViewPager 时不会调用 OnItemClickListener

android - 在同一个 fragment 中实现多个事件监听器 - Android

java - GWTP:关闭演示者后从未调用 onUnbind()