Handler 有兩種使用方式誊抛,一個是Post列牺,一個是sendMessage。
post方式
- post(Runnable r): 一般的post方式拗窃。
- postAtTime(Runnable r, long uptimeMillis):設(shè)置在某一個絕對時間執(zhí)行
- postAtTime(Runnable r, Object token, long uptimeMillis)
- postDelayed(Runnable r, long delayMillis):設(shè)置延遲執(zhí)行時間執(zhí)行
定一個Handler
private Handler mPostHandler = new Handler();
起一個子線程:
private class TestThread extends Thread{
@Override
public void run() {
try {
Thread.sleep(5000);
/**
* Post允許把一個Runnable對象入隊到消息隊列中.
* 方法有:
* post(Runnable)
* postAtTime(Runnable,long)
* postDelayed(Runnable,long)
*
* 對于Handler的Post方式來說瞎领,它會傳遞一個Runnable對象到消息隊列中,在這個Runnable對象中随夸,重寫run()方法九默。一般在這個run()方法中寫入需要在UI線程上的操作。
*/
mPostHandler.post(new Runnable() {
@Override
public void run() {
text.setText("Handler的Post方式到主線程");
LogHelper.i("Handler的Post方式到主線程");
}
});
/**
* postAtTime: 在設(shè)置的某一個絕對時間執(zhí)行宾毒。
*/
mPostHandler.postAtTime(new Runnable() {
@Override
public void run() {
text.setText(text.getText().toString()+"\n Handler的postAtTime方式到主線程");
LogHelper.i("Handler的postAtTime方式到主線程");
}
}, SystemClock.uptimeMillis() + 5000);
/**
* postDelayed:延遲執(zhí)行.
*/
mPostHandler.postDelayed(new Runnable() {
@Override
public void run() {
text.setText(text.getText().toString()+"\n Handler的postDelayed方式到主線程");
LogHelper.i("Handler的postDelayed方式到主線程");
}
},1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
sendMessage 方式
首先要在Handler中處理Message:
sendMessage(Message msg):
sendMessageDelayed(Message msg, long delayMillis):
sendMessageAtTime(Message msg, long uptimeMillis) :
sendEmptyMessage(int what)
sendEmptyMessageDelayed(int what, long delayMillis)
-
sendEmptyMessageAtTime(int what, long uptimeMillis) :
private Handler mHandler = new Handler(){
@Override public void handleMessage(Message msg) { switch (msg.what){ case MSG_ONE: String obj = (String)msg.obj; LogHelper.i("\n obj is : " + obj); text.setText(text.getText().toString()+"obj is : " + obj); int arg1 = msg.arg1; int arg2 = msg.arg2; LogHelper.i("\n arg1 is : " + arg1 + " arg2 is : " + arg2); text.setText(text.getText().toString()+"arg1 is : " + arg1 + " arg2 is : " + arg2); if(msg.getData() != null){ String test = (String)msg.getData().get("test"); LogHelper.i("\n test is : " + test); text.setText(text.getText().toString()+"test is : " + test); } break; case MSG_TWO: LogHelper.i("receive MSG_TWO "); break; } }
};
創(chuàng)建子線程
private class TestMessageThread extends Thread {
@Override
public void run() {
try {
Thread.sleep(5000);
/**
* 有3中方式獲取Message:
*
* 1驼修、Message message = new Message();
* 2、Message message = Message.obtain();
* 3诈铛、Message message = mHandler.obtainMessage();
*
*/
Message message = Message.obtain();
/**
* what是我們自定義的一個Message的識別碼乙各,以便于在Handler的handleMessage方法中根據(jù)what識別出不同的Message,以便我們做出不同的處理操作
*/
message.what = MSG_ONE;
/**
* 可以通過arg1和arg2給Message傳入簡單的數(shù)據(jù)
*/
message.arg1 = 100;
message.arg2 = 101;
/**
* 可以通過給obj賦值Object類型傳遞向Message傳入任意數(shù)據(jù)
*/
message.obj = new String("Obj String");
/**
* 通過setData方法和getData方法向Message中寫入和讀取Bundle類型的數(shù)據(jù)
*/
Bundle bundle = new Bundle();
bundle.putString("test","test string");
message.setData(bundle);
mCallHandler.sendMessage(message);
/**
* 傳遞空消息幢竹。
*/
mCallHandler.sendEmptyMessage(MSG_TWO);
/**
* 在設(shè)置的某一個絕對時間執(zhí)行耳峦。
*/
mHandler.sendMessageAtTime(message,SystemClock.uptimeMillis() + 5000);
/**
* 延遲執(zhí)行
*/
mHandler.sendMessageDelayed(message,1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Handler中還有一種帶Callback的構(gòu)造函數(shù):
/**
* Callback 回調(diào)如果返回的是true,則不往下執(zhí)行焕毫,如果返回的是false蹲坷,則會繼續(xù)執(zhí)行下面的handleMessage,達到攔截的效果.
*/
private Handler mCallHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if(msg.what == MSG_TWO){
LogHelper.i("Callback MSG_TWO ");
return true;
}
return false;
}
}){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case MSG_ONE:
String obj = (String)msg.obj;
LogHelper.i("\n obj is : " + obj);
text.setText(text.getText().toString()+"obj is : " + obj);
int arg1 = msg.arg1;
int arg2 = msg.arg2;
LogHelper.i("\n arg1 is : " + arg1 + " arg2 is : " + arg2);
text.setText(text.getText().toString()+"arg1 is : " + arg1 + " arg2 is : " + arg2);
if(msg.getData() != null){
String test = (String)msg.getData().get("test");
LogHelper.i("\n test is : " + test);
text.setText(text.getText().toString()+"test is : " + test);
}
break;
case MSG_TWO:
LogHelper.i("receive MSG_TWO ");
break;
}
}
};