HandlerThread

1)首先创建looper对象

private static final int MSG_LOAD_DATA = 0;
private static final int MSG_COMMIT_DATA = 1;
private static HandlerThread mThread = new          
        HandlerThread("greenThread");
static {
     mThread.start();
}

private Callback mCallback = new Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_LOAD_DATA:
                getMessage();
                break;
            case MSG_COMMIT_DATA:
                CommitPassword();
                break;
            default:
                break;
        }
        return true;
    }
};

private Handler mHandler = new Handler(mThread.getLooper(), mCallback);

2)之后,便可以通过handler进行消息的传递了,如下:

Message msg = mHandler.obtainMessage();
mHandler.removeMessages(MSG_LOAD_DATA);
mHandler.sendEmptyMessageDelayed(MSG_LOAD_DATA, 1 * 300L);

你可能感兴趣的:(HandlerThread)