您好,登錄后才能下訂單哦!
Handler的實(shí)質(zhì)就是異步消息處理。
實(shí)例一:
Handler的基本用法
XML布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="高興點(diǎn),未來不是夢"/> <Button android:id="@+id/btnStart" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="動(dòng)起來!"/> <Button android:id="@+id/btnEnd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="歇會(huì)吧!"/> <Button android:id="@+id/btnClear" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清零!"/> </LinearLayout>
Java代碼:
public class HandlerActivity extends Activity { private Button btnStart; private Button btnEnd; private Button btnClear; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handler); tvResult = (TextView) findViewById(R.id.tvResult); btnStart = (Button) findViewById(R.id.btnStart); btnEnd = (Button) findViewById(R.id.btnEnd); btnClear = (Button) findViewById(R.id.btnClear); btnStart.setOnClickListener(new StartButtonListener()); btnEnd.setOnClickListener(new EndButtonListener()); btnClear.setOnClickListener(new ClearButtonListener()); } // 創(chuàng)建一個(gè)Handler對(duì)象 Handler handler = new Handler(); private int i = 0; // 將要執(zhí)行的操作寫在線程對(duì)象的run方法當(dāng)中 Runnable updateThread = new Runnable() { public void run() { System.out.println("高興點(diǎn),追夢!--" + i); tvResult.setText("高興點(diǎn),追夢!--" + i); i++; // 在run方法內(nèi)部,執(zhí)行postDelayed或者是post方法 handler.postDelayed(updateThread, 3000); }; }; class StartButtonListener implements OnClickListener { @Override public void onClick(View v) { // 調(diào)用Handler的post方法,將要執(zhí)行的線程對(duì)象添加到隊(duì)列當(dāng)中 handler.post(updateThread); } } class EndButtonListener implements OnClickListener { @Override public void onClick(View v) { // 調(diào)用Handler的post方法,將要updateThread從隊(duì)列當(dāng)中移除 handler.removeCallbacks(updateThread); } } class ClearButtonListener implements OnClickListener { @Override public void onClick(View v) { // 調(diào)用Handler的post方法,將要updateThread從隊(duì)列當(dāng)中移除 handler.removeCallbacks(updateThread); i = 0; tvResult.setText("高興點(diǎn),未來不是夢"); } } }
實(shí)例二:
Handler與ProgressBar的結(jié)合使用
XML布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressbar" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="前進(jìn)吧,小摩托!" /> </LinearLayout>
Java代碼:
public class HandlerProgressBarActivity extends Activity { private ProgressBar progressbar; private Button btnStart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlerprogressbar); progressbar = (ProgressBar) findViewById(R.id.progressbar); btnStart = (Button) findViewById(R.id.btnStart); btnStart.setOnClickListener(new StartOnClick()); } class StartOnClick implements OnClickListener { @Override public void onClick(View v) { progressbar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } Handler updateBarHandler = new Handler() { @Override public void handleMessage(Message msg) { progressbar.setProgress(msg.arg1); updateBarHandler.post(updateThread); } }; Runnable updateThread = new Runnable() { int i = 0; @Override public void run() { System.out.println("Begin Thread!" + i); i = i + 10; Message msg = updateBarHandler.obtainMessage(); msg.arg1 = i; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } updateBarHandler.sendMessage(msg); if (i == 100) { updateBarHandler.removeCallbacks(updateThread); } } }; }
實(shí)例三:
Handler與線程的關(guān)系(一)
XML布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="前進(jìn)吧,小摩托!" /> </LinearLayout>
Java代碼:
public class HandlerThreadActivity extends Activity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { handler.post(r); setContentView(R.layout.activity_handlerthread); System.out.println("activity--->"+Thread.currentThread().getId()); System.out.println("activityname--->"+Thread.currentThread().getName()); } Runnable r = new Runnable(){ @Override public void run() { System.out.println("handler--->"+Thread.currentThread().getId()); System.out.println("handlername--->"+Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }; }
實(shí)例四:
Handler與線程的關(guān)系(二)
XML布局代碼:
同【實(shí)例三】中的布局代碼
Java代碼:
public class HandlerThreadActivity extends Activity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlerthread); Thread t = new Thread(r); t.start(); System.out.println("activity--->" + Thread.currentThread().getId()); System.out.println("activityname--->" + Thread.currentThread().getName()); } Runnable r = new Runnable() { @Override public void run() { System.out.println("handler--->" + Thread.currentThread().getId()); System.out.println("handlername--->" + Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }; }
實(shí)例五:
Handler實(shí)現(xiàn)真正的異步消息處理,在新線程中處理消息
XML布局代碼:
同【實(shí)例三】中的布局代碼
Java代碼:
public class HandlerMsgActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlermsg); // 打印當(dāng)前線程 System.out.println("activity---->" + Thread.currentThread().getId()); // 生成一個(gè)HandlerThread對(duì)象(具有循環(huán)處理消息的功能),實(shí)現(xiàn)了Looper來處理消息隊(duì)列 HandlerThread handlerThread = new HandlerThread("handler_thread"); // 讓一個(gè)線程運(yùn)行,必須用[線程名稱].start()方法 handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); // 將msg發(fā)動(dòng)到目標(biāo)對(duì)象,所謂目標(biāo)對(duì)象,就是生成msg的handler對(duì)象 msg.sendToTarget(); } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override // 每當(dāng)向Handler發(fā)送一個(gè)msg的時(shí)候,就會(huì)執(zhí)行handleMessage()方法 public void handleMessage(Message msg) { System.out.println("handler---->" + Thread.currentThread().getId()); System.out.println("handlerMessage"); } } }
實(shí)例六:
Handler實(shí)現(xiàn)真正的異步消息處理,在新線程中處理消息
XML布局代碼:
同【實(shí)例三】中的布局代碼
Java代碼:
public class HandlerMsgActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlermsg); // 打印當(dāng)前線程 System.out.println("activity---->" + Thread.currentThread().getId()); // 生成一個(gè)HandlerThread對(duì)象(具有循環(huán)處理消息的功能),實(shí)現(xiàn)了Looper來處理消息隊(duì)列 HandlerThread handlerThread = new HandlerThread("handler_thread"); // 讓一個(gè)線程運(yùn)行,必須用[線程名稱].start()方法 handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("age", 20); b.putString("name", "滔滔1024"); msg.setData(b); //msg.arg1與msg.arg2的消耗比msg.setData()小,但只能傳遞整型變量 // 將msg發(fā)動(dòng)到目標(biāo)對(duì)象,所謂目標(biāo)對(duì)象,就是生成msg的handler對(duì)象,本例中是發(fā)送到MyHandler對(duì)象中 msg.sendToTarget(); } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override // 每當(dāng)向Handler發(fā)送一個(gè)msg的時(shí)候,就會(huì)執(zhí)行handleMessage()方法 public void handleMessage(Message msg) { Bundle b = msg.getData(); int age = b.getInt("age"); String name = b.getString("name"); System.out.println("age->"+age+"\nname->"+name); System.out.println("handler---->" + Thread.currentThread().getId()); System.out.println("handlerMessage"); } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。