日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android架构分析之Android消息处理机制(一)

發布時間:2025/3/14 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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消息处理机制(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。