• Android应用开发学习笔记之多线程与Handler消息处理机制


    作者:刘昊昱 

    博客:http://blog.csdn.net/liuhaoyutz

    和JAVA一样,Android下我们可以通过创建一个Thread对象实现多线程。Thread类有多个构造函数,一般通过构造函数Thread(Runnable runnable)实现多线程,代码如下:

    Thread thread = new Thread(new Runnable() {
             //重写Runnable的run()方法
             publicvoid run() {
                       //子线程操作实现
             }
    });
    thread.start();          // 启动线程thread执行


    使用标准的JAVA线程类Thread可以创建子线程,但是在Android下,用Thread创建的线程有一个问题就是Thread创建的子线程不能对UI界面进行任何操作。为此,Android引入了Handler消息处理机制,通过Handler在子线程和UI线程之间传递信息,达到更新UI界面的目的。

    下面看一个使用Handler处理消息的例子,其运行效果如下:

    先来看主布局文件,其内容如下:

     

    <?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:id="@+id/textView" />
     
    </LinearLayout>


    下面看主Activity文件的实现,其内容如下:

    package com.liuhaoyu;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    	public Handler handler;
    	
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            final TextView text = (TextView)findViewById(R.id.textView);
            handler = new Handler() {
        		public void handleMessage(Message msg) {
        			Log.i("Looper",String.valueOf(msg.what));
        			if(msg.what == 0x0)
        			{
        				text.append(Integer.toString(msg.arg1));
        			}
        			} 
        		};
     
        	Thread thread = new Thread(new Runnable() {
        		public void run() {
        			int i = 0;
        			while(true)
        			{
        				try {
    						Thread.sleep(1000);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
                        Message message = new Message();
                        message.arg1 = i;
                        message.what = 0x0;
                        handler.sendMessage(message);
                        
                        if(i < 9)
        					i++;
        				else
        					i = 0;
        			}
        		}
        	});
        	thread.start();
        }
    }


  • 相关阅读:
    Java匹马行天下之JavaSE核心技术——工具类
    微信小程序
    在express3里用ejs模版引擎时,如何使其支持'.html'后缀
    node
    webstorm常用快捷键
    Meta标签中的format-detection属性及含义
    使用console进行 性能测试 和 计算代码运行时间
    JS获取浏览器URL中查询字符串的参数
    jspm 简介
    vue2.0 keep-alive最佳实践
  • 原文地址:https://www.cnblogs.com/james1207/p/3278123.html
Copyright © 2020-2023  润新知