android:Handler整理总结 收藏

android:Handler整理总结 收藏
首先声明,以下是我转载整理的。

感谢

http://blog.csdn.net/Android_Tutor/archive/2010/05/08/5568806.aspx

http://www.pocketdigi.com/20100814/45.html

这些朋友的共享!

当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无响应,如果时间过长,程序还会挂掉。Handler就是把这些功能放到一个单独的线程里执行,与Activity互不影响。

我们简单用例子来说明:

小实例一:5秒钟更新一下Title(标题),如图:



代码如下:

view plaincopy to clipboardprint?
package com.android.tutor;  
 
import java.util.Timer;  
import java.util.TimerTask;  
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Message;  
 
public class HandlerDemo extends Activity {  
 
    // title为setTitle方法提供变量,这里为了方便我设置成了int型  
    private int title = 0;  
 
    private Handler mHandler = new Handler() {  
 
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 1:  
                updateTitle();  
                break;  
            }  
        };  
    };  
 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
 
        Timer timer = new Timer();  
        timer.scheduleAtFixedRate(new MyTask(), 1, 5000);  
    }  
 
    private class MyTask extends TimerTask {  
        @Override 
        public void run() {  
 
            Message message = new Message();  
            message.what = 1;  
            mHandler.sendMessage(message);  
 
        }  
    }  
 
    public void updateTitle() {  
 
        setTitle("Welcome to Mr Wei's blog " + title);  
        title++;  
    }  

package com.android.tutor;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class HandlerDemo extends Activity {

// title为setTitle方法提供变量,这里为了方便我设置成了int型
private int title = 0;

private Handler mHandler = new Handler() {

public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
updateTitle();
break;
}
};
};

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTask(), 1, 5000);
}

private class MyTask extends TimerTask {
@Override
public void run() {

Message message = new Message();
message.what = 1;
mHandler.sendMessage(message);

}
}

public void updateTitle() {

setTitle("Welcome to Mr Wei's blog " + title);
title++;
}
}

小实例二:用Handler更新进度条ProgressBar ;如图:当按开始按钮时,进度条会每隔500毫秒加1,直到加到100或者按停止时停止




代码:

view plaincopy to clipboardprint?
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Handler;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ProgressBar;  
 
public class main extends Activity {  
    /** Called when the activity is first created. */ 
    ProgressBar pb1;  
    Handler handle = new Handler();  
    // 新建一个Handler对象  
    Button b1;  
    Button b2;  
 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        pb1 = (ProgressBar) findViewById(R.id.pb1);  
        pb1.setProgress(0);  
        b1 = (Button) findViewById(R.id.b1);  
        b1.setOnClickListener(b1Lis);  
        b2 = (Button) findViewById(R.id.b2);  
        b2.setOnClickListener(b2Lis);  
 
    }  
 
    private OnClickListener b1Lis = new OnClickListener() {  
 
        @Override 
        public void onClick(View v) {  
            // TODO Auto-generated method stub  
            handle.post(add);  
            // 开始执行add  
        }  
 
    };  
    private OnClickListener b2Lis = new OnClickListener() {  
 
        @Override 
        public void onClick(View v) {  
            // TODO Auto-generated method stub  
            handle.removeCallbacks(add);  
            // 停止执行  
            pb1.setProgress(0);  
        }  
 
    };  
    int pro = 0;  
    Runnable add = new Runnable() {  
        // 定义add  
        @Override 
        public void run() {  
            // TODO Auto-generated method stub  
            pro = pb1.getProgress() + 1;  
            pb1.setProgress(pro);  
            setTitle(String.valueOf(pro));  
            if (pro < 100) {  
                handle.postDelayed(add, 500);  
                // 如果进度小于100,,则延迟500毫秒后重复执行add  
            }  
 
        }  
 
    };  
 

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class main extends Activity {
/** Called when the activity is first created. */
ProgressBar pb1;
Handler handle = new Handler();
// 新建一个Handler对象
Button b1;
Button b2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb1 = (ProgressBar) findViewById(R.id.pb1);
pb1.setProgress(0);
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(b1Lis);
b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(b2Lis);

}

private OnClickListener b1Lis = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handle.post(add);
// 开始执行add
}

};
private OnClickListener b2Lis = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handle.removeCallbacks(add);
// 停止执行
pb1.setProgress(0);
}

};
int pro = 0;
Runnable add = new Runnable() {
// 定义add
@Override
public void run() {
// TODO Auto-generated method stub
pro = pb1.getProgress() + 1;
pb1.setProgress(pro);
setTitle(String.valueOf(pro));
if (pro < 100) {
handle.postDelayed(add, 500);
// 如果进度小于100,,则延迟500毫秒后重复执行add
}

}

};

}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufenglong/archive/2010/08/27/5843513.aspx

你可能感兴趣的:(android)