ch022 Android Handler

--------------------------------------------AndroidManifest.xml----------------------------------

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.ch22"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MainActivity"

            android:label="@string/title_activity_main" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

--------------------------------------------Layout activity_main.xml-----------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:id="@+id/btn"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" 

        android:text="Button"/>

</LinearLayout>

--------------------------------------------MainActivity.java--------------------------------------

package com.ch22;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

 * 

 * 项目名称:com.ch22    

 * 类名称:MainActivity    

 * 类描述:Handler

 * 创建人:方勇   

 * 创建时间:2012-12-10 下午8:29:04   

 * Copyright (c) 方勇-版权所有

 */

public class MainActivity extends Activity {

private Button btn;

/* 更新UI主线程对象 */

private Handler handler = new Handler() {

/* 异步接受消息 */

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

/* 获取Map数据 */

Bundle bundle = msg.getData();

/* 取得颜色值 */

int color = bundle.getInt("color");

MainActivity.this.btn.setBackgroundColor(color);

Log.i("fy""Handler=" + Thread.currentThread().getName());

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findView();

setListeners();

Log.i("fy""MainHandler=" + Thread.currentThread().getName());

}

/* 实例化UI */

private void findView() {

this.btn = (Button) findViewById(R.id.btn);

}

/* 设置监听 */

private void setListeners() {

btn.setOnClickListener(new btnOnClickListener());

}

class btnOnClickListener implements OnClickListener {

public void onClick(View v) {

MyThread my = new MyThread();

/* 启动线程 */

new Thread(my).start();

}

};

class MyThread implements Runnable {

public void run() {

try {

Thread.sleep(3000);// 子线程睡眠3秒

catch (InterruptedException e) {

e.printStackTrace();

}

/* 实例化消息 */

Message msg = new Message();

Bundle bundle = new Bundle();

bundle.putInt("color", Color.RED);

msg.setData(bundle);

/* 发送消息,告诉老大(主线程)请更新 */

MainActivity.this.handler.sendMessage(msg);

Log.i("fy""MyThread=" + Thread.currentThread().getName());

}

};

}

--------------------------------------------结果-----------------------------------------------------

点击按钮前

点击按钮后的3秒,按钮变为红色

<!--EndFragment-->

猜你喜欢

转载自fangyong2006.iteye.com/blog/1745436