当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无响应,如果时间过长,程序还会挂掉。Handler就是把这些功能放到一个单独的线程里执行,与Activity互不影响。
当用户点击一个按钮时如果执行的是一个常耗时操作的话,处理不好会导致系统假死,用户体验很差,而Android则更进一步,如果任意一个Acitivity没有响应5秒钟以上就会被强制关闭,因此我们需要另外起动一个线程来处理长耗时操作,而主线程则不受其影响,在耗时操作完结发送消息给主线程,主线程再做相应处理。那么线程之间的消息传递和异步处理用的就是Handler。
以下模拟一个简单的相册查看器,每隔2秒自动更换下一张照片。
main.xml布局文件
<?
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 "
android:gravity = " center " >
< ImageView android:id = " @+id/imageView "
android:layout_width = " fill_parent "
android:layout_height = " wrap_content "
android:src = " @drawable/p1 "
android:gravity = " center " />
</ LinearLayout >
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation = " vertical "
android:layout_width = " fill_parent "
android:layout_height = " fill_parent "
android:gravity = " center " >
< ImageView android:id = " @+id/imageView "
android:layout_width = " fill_parent "
android:layout_height = " wrap_content "
android:src = " @drawable/p1 "
android:gravity = " center " />
</ LinearLayout >
HandleActivity类
package
com.ljq.handle;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
public class HandleActivity extends Activity {
private ImageView imageView = null ;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0 :
imageView.setImageResource(R.drawable.p1);
break ;
case 1 :
imageView.setImageResource(R.drawable.p2);
break ;
case 2 :
imageView.setImageResource(R.drawable.p3);
break ;
case 3 :
imageView.setImageResource(R.drawable.p4);
break ;
}
super .handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
thread.start();
}
int what = 0 ;
Thread thread = new Thread( new Runnable() {
public void run() {
while ( true ) {
handler.sendEmptyMessage((what ++ ) % 4 );
try {
Thread.sleep( 2000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
public class HandleActivity extends Activity {
private ImageView imageView = null ;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0 :
imageView.setImageResource(R.drawable.p1);
break ;
case 1 :
imageView.setImageResource(R.drawable.p2);
break ;
case 2 :
imageView.setImageResource(R.drawable.p3);
break ;
case 3 :
imageView.setImageResource(R.drawable.p4);
break ;
}
super .handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
thread.start();
}
int what = 0 ;
Thread thread = new Thread( new Runnable() {
public void run() {
while ( true ) {
handler.sendEmptyMessage((what ++ ) % 4 );
try {
Thread.sleep( 2000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
运行结果