Android架构分析之Android消息处理机制(一)
作者:劉昊昱?
博客:http://blog.csdn.net/liuhaoyutz
Android版本號:4.4.2?
在這個系列文章中我們將來分析Android消息處理機制。
本文介紹了一個使用Handler的Android應用程序,通過該程序,我們能夠了解Handler的基本使用方法。進而在后面的文章中,我們會擴展到Looper、Message、MessageQueue、Runnable等Android對象,對它們的實現進行具體分析,這些對象組成了Android消息處理系統。
在Android應用程序中,假設要運行一個用時比較長的操作。比如訪問網絡,為了避免出現No Response錯誤。我們要將該操作放在一個單獨的線程中運行。可是假設該操作完畢后。須要改動UI界面,則會出現故障。由于除了UI線程,其他線程不能改動UI界面,這樣的情況下,能夠使用handler。以下我們來看一個樣例,程序運行效果例如以下:
點擊Button1button后,該程序執行一個用時比較長的操作(我們用sleep10秒鐘來模擬該操作),然后在主界面上顯示“Button1 is clicked!”。執行效果例如以下:
點擊Button2button后,該程序執行一個用時比較長的操作(我們用sleep10秒鐘來模擬該操作),然后在主界面上顯示“Button2 is clicked!”,執行效果例如以下:
以下我們來看這個程序代碼。
主程序TestHandlerActivity.java內容例如以下:
package com.haoyu.testHandler;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;public class TestHandlerActivity extends Activity implements OnClickListener{TextView textView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button1 = (Button) findViewById(R.id.button1); Button button2 = (Button) findViewById(R.id.button2);textView = (TextView) findViewById(R.id.textView);button1.setOnClickListener(this);button2.setOnClickListener(this); }public Handler handler =new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);if(msg.what == 1)textView.setText("Button1 is clicked!");else if(msg.what == 2)textView.setText("Button2 is clicked!");elsetextView.setText("Unknown message!");} };@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = v.getId();if(id == R.id.button1){new Thread(new BackgroundTask(handler, 1)).start();}if(id == R.id.button2){new Thread(new BackgroundTask(handler, 2)).start();}} }BackgroundTask.java文件內容例如以下:
package com.haoyu.testHandler;import android.os.Handler; import android.os.Message;public class BackgroundTask implements Runnable {Handler mHandler;int mFlag;BackgroundTask(Handler handler, int flag) {mHandler = handler;mFlag = flag;}@Overridepublic void run() {Message message = new Message();// TODO Auto-generated method stubtry {Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}message.what = mFlag;mHandler.sendMessage(message);}}主布局文件main.xml內容例如以下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >" <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="60dp" android:textSize="20dp" android:gravity="center" android:id="@+id/textView" android:text="@string/prompt" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Button1" android:id="@+id/button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Button2" android:id="@+id/button2" /> </LinearLayout> </LinearLayout>
轉載于:https://www.cnblogs.com/gccbuaa/p/6747953.html
總結
以上是生活随笔為你收集整理的Android架构分析之Android消息处理机制(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日一个linux 命令-修改linux
- 下一篇: Android开发学习之路--Camer