天天看點

Android 開發中的 Handler ,Thread ,Message ,Runnable 的綜合使用方法

邊做邊學的方法。

多線程更新UI的方法

layout  的布局檔案

<?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"   

        android:text="@string/hello"  

        />  

         <TextView  

        android:id="@+id/text2"    

        android:layout_width="fill_parent"   

        android:layout_height="wrap_content"   

        android:text="@string/hello"  

        />  

    <Button  

        android:id="@+id/startservice"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:text="startThread1"  

    />  

    <Button  

        android:id="@+id/stopservice"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:text="stopThread1"  

    />  

    <Button  

        android:id="@+id/bindservice"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:text="startThread2"  

    />  

    <Button  

        android:id="@+id/unbindservice"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:text="stopThread2"  

    />  

     <Button  

        android:id="@+id/over"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:text="over"  

    />  

</LinearLayout>

======================2個textView 一個用來顯示時間,一個計數==========================

代碼如下:

package com.dhanzhang;

import java.text.SimpleDateFormat;

import java.util.Date;

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 TestHandler extends Activity implements OnClickListener {

private TextView mTextView;

private TextView mTextView2;

private Button startServiceButton;

private Button stopServiceButton;

private Button bindServiceButton;

private Button unbindServiceButton;

private Button over;

protected static final int GUIUPDATEIDENTIFIER = 0x101;

int i = 0;

Thread myRefreshThread = null;

Handler handler2 = new Handler();

// 1.定義一個Handler(一般更新View)

Handler myHandler = new Handler() {

// 2.重寫消息處理函數

public void handleMessage(Message msg) {

switch (msg.what) {

// 判斷發送的消息

case TestHandler.GUIUPDATEIDENTIFIER:

// 更新View

Date d = new Date(System.currentTimeMillis());

SimpleDateFormat sf = new SimpleDateFormat(

"yyyy-MM-dd hh:mm:ss.SSS");

mTextView.setText(String.format("Runnable:%s ", sf.format(d)));

break;

}

super.handleMessage(msg);

}

};

Runnable runnable = new Runnable() {

@Override

public void run() {

mTextView2.setText(String.valueOf(i++));

handler2.postDelayed(runnable, 1000);

}

};

class myThread implements Runnable {

public void run() {

while (!Thread.currentThread().isInterrupted()) {

// 3.發送消息

Message message = new Message();

// 發送消息與處理函數裡一緻

message.what = TestHandler.GUIUPDATEIDENTIFIER;

// 内部類調用外部類的變量

TestHandler.this.myHandler.sendMessage(message);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

}

}

}

}

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.test);

setupViews();

}

public void setupViews() {

mTextView = (TextView) findViewById(R.id.text);

mTextView2 = (TextView) findViewById(R.id.text2);

mTextView.setText("Begin To");

mTextView2.setText("Y");

startServiceButton = (Button) findViewById(R.id.startservice);

stopServiceButton = (Button) findViewById(R.id.stopservice);

bindServiceButton = (Button) findViewById(R.id.bindservice);

unbindServiceButton = (Button) findViewById(R.id.unbindservice);

over = (Button) findViewById(R.id.over);

startServiceButton.setOnClickListener(this);

stopServiceButton.setOnClickListener(this);

bindServiceButton.setOnClickListener(this);

unbindServiceButton.setOnClickListener(this);

over.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

setResult(Activity.RESULT_OK);

finish();

}

});

}

@Override

public void onDestroy() {

super.onDestroy();

if (myRefreshThread != null) {

myRefreshThread.interrupt();

myRefreshThread.stop();

}

handler2.removeCallbacks(runnable) ;

}

public void onClick(View v) {

if (v == startServiceButton) {

myRefreshThread = new Thread(new myThread());

myRefreshThread.start();

} else if (v == stopServiceButton) {

myRefreshThread.interrupt();

} else if (v == bindServiceButton) {

handler2.post(runnable); 

} else if( v== unbindServiceButton)  {

handler2.removeCallbacks(runnable) ;

}

}

}

====================PS:===============

onDestroy()的重寫可能有問題,不過在虛拟機裡看不出來啥問題,不知道實際情況會如何。

Android 版本是2.2 

Runnable 接口并非真正的開啟了線程(具體的請參見: http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038516.html 這篇文章)

轉載于:https://www.cnblogs.com/dhz123/archive/2011/08/19/2194247.html