Android Handler

1.handler作用:?

1)傳遞消息Message

//2種創(chuàng)建消息方法

//1.通過handler實(shí)例獲取

Handler handler =newHandler();

Message message=handler.obtainMessage();

//2.通過Message獲取

Message message=Message.obtain();

//源碼中第一種獲取方式其實(shí)也是內(nèi)部調(diào)用了第二種:

publicfinalMessageobtainMessage(){

returnMessage.obtain(this);

}

不建議直接new Message,Message內(nèi)部保存了一個(gè)緩存的消息池,我們可以用obtain從緩存池獲得一個(gè)消息,Message使用完后系統(tǒng)會(huì)調(diào)用recycle回收,如果自己new很多Message,每次使用完后系統(tǒng)放入緩存池,會(huì)占用很多內(nèi)存的。?

//傳遞的數(shù)據(jù)

Bundle bundle =newBundle();

bundle.putString("msg","傳遞我這個(gè)消息");

//發(fā)送數(shù)據(jù)

Message message = Message.obtain();

message.setData(bundle);//message.obj=bundle? 傳值也行

message.what =0x11;

handler.sendMessage(message);

//數(shù)據(jù)的接收

finalHandler handler =newHandler() {

@Override

publicvoidhandleMessage(Message msg){

super.handleMessage(msg);

if(msg.what ==0x11) {

? ? ? ? ? ? ? ? Bundle bundle = msg.getData();

String date = bundle.getString("msg");

? ? ? ? ? ? }

? ? ? ? }

};

2)子線程通知主線程更新ui

//創(chuàng)建handler

finalHandler handler =newHandler() {

@Override

publicvoidhandleMessage(Message msg){

super.handleMessage(msg);

if(msg.what ==0x11) {

//更新ui

? ? ? ? ? ? ? ? ? ? ? ? ? ......

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? };

newThread(newRunnable() {

@Override

publicvoidrun(){

//FIXME 這里直接更新ui是不行的

//還有其他更新ui方式,runOnUiThread()等? ? ? ? ?

message.what =0x11;

? ? ? ? ? ? ? ? handler.sendMessage(message);?

? ? ? ? ? ? }

? ? ? ? }).start();

2.常用api

//消息

? ? Message message = Message.obtain();

//發(fā)送消息

newHandler().sendMessage(message);

//延時(shí)1s發(fā)送消息

newHandler().sendMessageDelayed(message,1000);

//發(fā)送帶標(biāo)記的消息(內(nèi)部創(chuàng)建了message,并設(shè)置msg.what = 0x1)

newHandler().sendEmptyMessage(0x1);

//延時(shí)1s發(fā)送帶標(biāo)記的消息

newHandler().sendEmptyMessageDelayed(0x1,1000);

//延時(shí)1秒發(fā)送消息(第二個(gè)參數(shù)為:相對(duì)系統(tǒng)開機(jī)時(shí)間的絕對(duì)時(shí)間,而SystemClock.uptimeMillis()是當(dāng)前開機(jī)時(shí)間)

newHandler().sendMessageAtTime(message, SystemClock.uptimeMillis() +1000);

//避免內(nèi)存泄露的方法:

//移除標(biāo)記為0x1的消息

newHandler().removeMessages(0x1);

//移除回調(diào)的消息

newHandler().removeCallbacks(Runnable);

//移除回調(diào)和所有message

newHandler().removeCallbacksAndMessages(null);

3.handler使用避免內(nèi)存泄露

1)handler怎么使用會(huì)產(chǎn)生內(nèi)存泄露?

publicclassMainActivityextendsAppCompatActivity{

finalHandler handler =newHandler() {

@Override

publicvoidhandleMessage(Message msg){

super.handleMessage(msg);

? ? ? ? ? ? ? ? ......

? ? ? ? }

? ? };

@Override

protectedvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);

//activity被執(zhí)行時(shí),被延遲的這個(gè)消息存于主線程消息隊(duì)列中1分鐘,

//此消息包含handler引用,而handler由匿名內(nèi)部類創(chuàng)建,持有activity引用,

//activity便不能正常銷毀,從而泄露

handler.postDelayed(newRunnable() {

@Override

publicvoidrun(){

? ? ? ? ? ? ? ? ......

? ? ? ? ? ? }

},1000*60);

? ? }

}

2)如何避免handler的內(nèi)存泄露?

publicclassMainActivityextendsAppCompatActivity{

//創(chuàng)建靜態(tài)內(nèi)部類

privatestaticclassMyHandlerextendsHandler{

//持有弱引用MainActivity,GC回收時(shí)會(huì)被回收掉.

privatefinalWeakReference mAct;

publicMyHandler(MainActivity mainActivity){

mAct =newWeakReference(mainActivity);

? ? ? ? }

@Override

publicvoidhandleMessage(Message msg){

? ? ? ? ? ? MainActivity mainAct=mAct.get();

super.handleMessage(msg);

if(mainAct!=null){

//執(zhí)行業(yè)務(wù)邏輯

? ? ? ? ? ? }

? ? ? ? }

? ? }

privatestaticfinalRunnable myRunnable =newRunnable() {

@Override

publicvoidrun(){

//執(zhí)行我們的業(yè)務(wù)邏輯

? ? ? ? }

? ? };

@Override

protectedvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);

MyHandler myHandler=newMyHandler(this);

//延遲5分鐘后發(fā)送

myHandler.postDelayed(myRunnable,1000*60*5);

? ? }

}

3)? 雷區(qū)

a)Handler.post(Runnable)其實(shí)就是生成一個(gè)what為0的Message,調(diào)用

myHandler.removeMessages(0);

會(huì)使runnable任務(wù)從消息隊(duì)列中清除。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容