Handler和AsyncTask
阿新 • • 發佈:2018-04-15
創建 ets handle class 循環 hand 自動 tails ati
原文地址:https://blog.csdn.net/LogicTeamLeader/article/details/51165375
https://blog.csdn.net/logicteamleader/article/details/46591499
Handler:
要理解Handler,必然要綜合理解Looper,Messagequeue,HandlerThread。
Looper:其中有一個Messagequeue,looper負責輪詢Messagequeue,取出其中的Message交給Handler處理
Handler:負責處理Message。Handler的創建需要Looper對象,它創建時會自動匹配該線程的looper對象,若在子線程創建,需要looper.prepare()創建looper對象。(主線程本身就存在looper)
HandlerThread:顧名思義,Handler存在的線程,Handler在哪個線程創建就存在於哪個線程中。這個大部分時間都在執行looper.loop()方法。getlooper()可以獲取當前線程的looper。
關系圖:
具體的用法:
(1)主線程創建Handler
package com.example.wxbhandlertest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; importandroid.os.MessageQueue; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /** * @author wxb * Android中的消息處理實例之一 * * 一、在主線程內發送消息 * 1.使用post * 2.使用postDelay * 3.使用sendMessage * 4.使用Message.sentToTarget * 二、在子線程中使用Handler * 1.使用post * 2.使用postDelay * 3.使用sendMessage * 4.使用Message.sentToTarget*/ public class MainActivity extends Activity { private Runnable runnable=null; private Runnable runnableDelay=null; private Runnable runnableInThread=null; private Runnable runnableDelayInThread=null; private static TextView tv; private static TextView tvOnOtherThread; //自定義Message類型 public final static int MESSAGE_WXB_1 = 1; public final static int MESSAGE_WXB_2 = 2; public final static int MESSAGE_WXB_3 = 3; public final static int MESSAGE_WXB_4 = 4; public final static int MESSAGE_WXB_5 = 5; private static Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case MESSAGE_WXB_1: tv.setText("invoke sendMessage in main thread"); break; case MESSAGE_WXB_2: tv.setText("Message.sendToTarget in main thread"); break; case MESSAGE_WXB_3: tvOnOtherThread.setText("invoke sendMessage in other thread"); break; case MESSAGE_WXB_4: tvOnOtherThread.setText("Message.sendToTarget in other thread"); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.tvOnMainThread); tvOnOtherThread = (TextView) this.findViewById(R.id.tvOnOtherThread); //方法1.post runnable = new Runnable(){ public void run(){ tv.setText(getString(R.string.postRunnable)); } }; Button handler_post = (Button) this.findViewById(R.id.btnHandlerpost); handler_post.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mHandler.post(runnable); } }); //方法2:postDelay runnableDelay = new Runnable(){ public void run(){ tv.setText(getString(R.string.postRunnableDelay)); } }; Button handler_post_delay = (Button) this.findViewById(R.id.btnHandlerPostdelay); handler_post_delay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mHandler.postDelayed(runnableDelay, 1000); //1秒後執行 } }); //方法3:sendMessage Button btnSendMessage = (Button) this.findViewById(R.id.btnSendMessage); btnSendMessage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Message msg = mHandler.obtainMessage(); msg.what = MESSAGE_WXB_1; mHandler.sendMessage(msg); } }); //方法4:Message.sendToTarget Button btnSendtoTarget = (Button) this.findViewById(R.id.btnSendtoTarget); btnSendtoTarget.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Message msg = mHandler.obtainMessage(); msg.what = MESSAGE_WXB_2; msg.sendToTarget(); } }); //在其他線程中發送消息 //1.post runnableInThread = new Runnable(){ public void run(){ tvOnOtherThread.setText(getString(R.string.postRunnableInThread)); } }; Button btnPost = (Button) this.findViewById(R.id.btnPost); btnPost.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(){ @Override public void run() { super.run(); mHandler.post(runnableInThread); } }.start(); } }); //2.postDelay runnableDelayInThread = new Runnable(){ public void run(){ tvOnOtherThread.setText(getString(R.string.postRunnableDelayInThread)); } }; Button btnPostDelay = (Button) this.findViewById(R.id.btnPostDelay); btnPostDelay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(){ @Override public void run() { super.run(); mHandler.postDelayed(runnableDelayInThread, 1000); } }.start(); } }); //3.sendMessage Button btnSendMessage2 = (Button) this.findViewById(R.id.btnSendMessage2); btnSendMessage2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(){ @Override public void run() { super.run(); Message msg = mHandler.obtainMessage(); msg.what = MESSAGE_WXB_3; mHandler.sendMessage(msg); } }.start(); } }); //方法4:Message.sendToTarget Button btnSendToTarget2 = (Button) this.findViewById(R.id.btnSendToTarget2); btnSendToTarget2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(){ @Override public void run() { super.run(); mHandler.obtainMessage(MESSAGE_WXB_4).sendToTarget(); } }.start(); } }); } }
(2)子線程創建Handler
package com.example.wxbloopinthread; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private static TextView tv = null; //自定義Message類型 public final static int MESSAGE_WXB_1 = 1; //主線程中創建Handler private static Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case MESSAGE_WXB_1: tv.setText("主線程發送,子線程接收消息後回發,主線程修改UI"); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.textView1); //創建子線程 new LooperThread().start(); //點擊按鈕向子線程發送消息 Button btn = (Button) this.findViewById(R.id.btnSendMessage); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LooperThread.sHandler.sendEmptyMessage(MESSAGE_WXB_1); } }); } //定義子線程 static class LooperThread extends Thread { public static Handler sHandler = null; public void run() { //創建消息循環 Looper.prepare(); sHandler = new Handler() { public void handleMessage(Message msg) { switch(msg.what){ case MESSAGE_WXB_1: mHandler.sendEmptyMessage(MESSAGE_WXB_1); break; } } }; //開啟消息循環 Looper.loop(); } } }
Handler和AsyncTask