Android Handler and Message

Handler class:
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue.

Message class:
Defines a message containing a description and arbitrary data object that can be sent to a Handler. This object contains two extra int fields and an extra object field that allow you to not do allocations in many cases.

private static final int LAT = 2;
private static final int LONG= 3;

Handler mHandler;

mHandler = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case LAT:
          String str = (String)msg.obj;
          break;
        case LONG:
          String str = (String)msg.obj;
          break;
      }
    }
};

// notify
Message.obtain(mHandler, LAT, location.getLatitude()+"").sendToTarget();
Message.obtain(mHandler, LONG,location.getLongitude()+"").sendToTarget();

0 意見: