HandlerActivity.java

来源:互联网 发布:如何成为优秀的程序员 编辑:程序博客网 时间:2024/05/17 20:35
package com.lx;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HandlerActivity extends Activity {

    private TextView textView;
    private MyHandler myHandler;
    private Button button;
    private ProgressBar progressBar;
    private MyThread m = new MyThread();
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handlermain);
        textView = (TextView) findViewById(R.id.text);
        button = (Button) findViewById(R.id.startButton);
        progressBar = (ProgressBar) findViewById(R.id.bar);
        progressBar.setMax(100);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                myHandler = new MyHandler();
                new Thread(m).start();
                System.out.println("onCreate--The Thread is: "
                        + Thread.currentThread().getId());
            }
        });
    }

    // 在对UI进行更新时,执行时所在的线程为主UI线程
    class MyHandler extends Handler {// 继承Handler类时,必须重写handleMessage方法
        public MyHandler() {
        }

        public MyHandler(Looper loop) {
            super(loop);
        }

        @Override
        public void handleMessage(Message msg) {// 执行接收到的通知,此时执行的顺序是按照队列进行,即先进先出
            System.out.println("handleMessage--The ThreadId is: "
                    + Thread.currentThread().getId());
            super.handleMessage(msg);
            Bundle b = msg.getData();
            String textStr0 = textView.getText().toString();
            String textStr1 = b.getString("textStr");
            HandlerActivity.this.textView.setText(textStr0 + " " + textStr1);// 更改TextView中的值
            int barValue = b.getInt("barValue");
            HandlerActivity.this.progressBar.setProgress(barValue);
        }

    }

    // 该线程将会在单独的线程中运行

    class MyThread implements Runnable {
        int i = 1;
        @Override
        public void run() {
            while (i < 11) {
                System.out.println("MyThread--run ThreadId is: "
                        + Thread.currentThread().getId());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                Bundle b = new Bundle();
                b.putString("textStr", "线程运行" + i + "次");
                b.putInt("barValue", i * 10);
                i++;
                msg.setData(b);
                HandlerActivity.this.myHandler.sendMessage(msg);// 通过sendMessage向Handler发送更新UI的消息
            }
        }
    }

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
       
<TextView android:id="@+id/text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
 
>
</TextView>

<Button android:id="@+id/startButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       
>
</Button>

<ProgressBar android:id="@+id/bar"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
>


</ProgressBar>
        
</LinearLayout>


原创粉丝点击