开发者

A common class for asynctask UI handling

I have used a lot of asynctask class in my app开发者_如何学Clication. Is it possible to write common class and update the user interface value?


As long as Java has no closures, I don't think this makes a lot of sense.

If you are always doing the same task and only want to modify different UI elements, you can go and pass them in a constructor and then later modify them in onPostExecute().


Instead of using async tasks you can post messages to a common handler to handle UI messages

Common UI handler

private Handler messageHandler = new Handler() {

  @Override
  public void handleMessage(Message msg) {  
      switch(msg.what) {
          //handle update
          //.....
      }
  }

};

Thread to post the message

Thread t = new Thread() {

      public void run() {
          while (true) {

              mResults = doSomethingExpensive();

              //Send update to the main thread
              messageHandler.sendMessage(Message.obtain(messageHandler, mResults)); 
          }

      }
  };
  t.start();
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜