1

How would I use a Looper with an asyncTask?

The doImBackground method is called to programmatically create a TableLayout called MyResultTable. The MyResultTable, in turn, has a handler that is called by onTouch during MotionEvent.ACTION_MOVE.

After getting complaints about using handle inside asynchTask, I decide to run handler on UIThread. But that's causing my onTouch method to have slow response. So my question is, how do I use a looper with an asyncTask?

UIThread code:

activity.runOnUiThread(new Runnable() {
  public void run() {
handler.post(mResizeViews);
  }
});

My Looper attempt: (not working: blank screen)

protected MyResultTable doInBackground(Void... params) {
            Looper.prepare();
            MyResultTable table = new MyResultTable(context, other);
            Looper.loop();
            return tabl;
        }
Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87

1 Answers1

3

How would I use a Looper with an asyncTask?

You wouldn't.

The doImBackground method is called to programmatically create a TableLayout called MyResultTable.

That is not an appropriate role for doInBackground(). Use doInBackground() for slow things: network I/O, disk I/O, etc. Use onPostExecute() for generating the UI ("a TableLayout called MyResultTable") based upon the data retrieved by doInBackground().

The MyResultTable, in turn, has a handler that is called by onTouch during MotionEvent.ACTION_MOVE.

That does not make much sense. You use a Handler to forward events to the main application thread from a background thread. Your onTouchEvent() is called on the main application thread, and therefore it does not need a Handler.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Are you sure? I know you usually have great answers. But I always assemble my tables in the background and then use `onPostExecute` to pass the custom table to the linearLayout holding it. Otherwise android tends to say my UI is working too much. – Cote Mounyo Aug 05 '13 at 18:56
  • @CoteMounyo: "Otherwise android tends to say my UI is working too much" -- use tools like Traceview to find the real reason behind performance issues. – CommonsWare Aug 05 '13 at 19:00
  • Ok. So I try your suggestions. And it works (+1). No complaint from android about UI working hard. But my slider is still not smooth. It takes a while to start moving and then jump to where user has displaced finger. Ref http://stackoverflow.com/questions/17958671/increase-sensitivity-to-user-touch – Cote Mounyo Aug 05 '13 at 19:20