首先我们要明确在Android的开发过程中,有很多功能是不能放在Activity的里面的额。就是不能放在onCreate onStart方法里面,为什么呢?因为这样的操作相对来讲费时比较长,比如说我们要在网络上下载一个文件,这个下载的过程可能会比较长,如果我们把这个功能放在activity里面的话,可能会导致在下载的过程中,这个activity长时间没有响应的,这样会造成一个比较差的用户的感觉,另外如果长时间的下载不下来的话,这个Activfity会报错,所以说我们希望把下载啦,还有处理大量数据这样的功能放在一个单独的线程里面,比如说Activity是一个线程,然而下载文件又是另外一个线程,那么在另外一个线程当中下载文件的这个功能并不能影响我们当前Activiy的使用,这样的话能让用户感觉更好,那么Handler就是为了让我们更好的使用这个功能。
Handler 类是Android操作系统自带的一种类
Handler handler = new Handler(); 此时新建了一个handler类
package com.example.handleractivity;
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;
public class HandlerActivity extends Activity {
private Button startButton = null;
private Button endButton = null;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.start);
endButton = (Button) findViewById(R.id.end);
startButton.setOnClickListener(new StartButtonListener());
endButton.setOnClickListener(new EndButtonListener());
}
public class StartButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
handler.post(updateThread);
}
}
public class EndButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
handler.removeCallbacks(updateThread);
}
}
Runnable updateThread = new Runnable() {
@Override
public void run() {
System.out.println("updateThread");
handler.postDelayed(updateThread, 3000);
}
};
}
如上例所示,视图当中有两个按钮startButton endButton
startButton加入监听StartButtonListener
用于启动Handler handler.post(updateThread);
这就相当于给Handler消息队列当中加入了一个事件updateThread
当即执行 输出结果
但是在updatThread的run方法里面handler.postDelayed(updateThread, 3000);
在3秒钟之后又一次加入了updateThread消息事件 往复循环直到使用
endButton的监听类中的方法handler.removeCallbacks(updateThread);
清除掉这个线程。其实这也就是告诉了我们在这里如何使用Handler
void android.os.Handler.removeCallbacks(Runnable r)
public final void removeCallbacks (Runnable r)
Added in API level 1
Remove any pending posts of Runnable r that are in the message queue.
public final boolean post (Runnable r)
Added in API level 1
Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which
this handler is attached.
Parameters
r | The Runnable that will be executed. |
Returns
· Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure,
usually because the looper processing the message queue is exiting.
void android.os.Handler.removeCallbacks(Runnable r)
boolean android.os.Handler.postDelayed(Runnable r, long delayMillis)
public final boolean postDelayed (Runnable r, long delayMillis)
Added in API level 1
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time
elapses. The runnable will be run on the thread to which this handler is attached.
Parameters
r | The Runnable that will be executed. |
delayMillis | The delay (in milliseconds) until the Runnable will be executed. |
Returns
· Returns true if the Runnable was successfully placed in to the message queue. Returns false
on failure, usually because the looper processing the message queue is exiting. Note that a result
of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time
of the message occurs then the message will be dropped.
下面是配置文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HandlerActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="start">开始</string>
<string name="end">停止</string>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start"
></Button>
<Button
android:id="@+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/end"
></Button>
</LinearLayout>