Handler的使用:
??? 由于Handler运行在主线程中(UI线程),它与子线程可以通过Message对象来传递数据。
Handler的作用:
?? 1.安排消息或Runnable在某个主线程中某个地方执行。
?? 2.安排一个动作在不同的线程中执行。
?
代码一、
package com.ko8e; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class MyActivity extends Activity { /** Called when the activity is first created. */ private ProgressBar progressbar = null; private Button button = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressbar = (ProgressBar) findViewById(R.id.progressbar); button = (Button) findViewById(R.id.button); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { progressbar.setVisibility(View.VISIBLE); handler.post(runnable); }}); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { progressbar.setProgress(msg.arg1); super.handleMessage(msg); } }; private Runnable runnable = new Runnable() { int i = 0; public void run() { // TODO Auto-generated method stub i = i + 10; Message msg = handler.obtainMessage(); msg.arg1 = i; handler.sendMessage(msg); if(i == progressbar.getMax()) { handler.removeCallbacks(runnable); progressbar.setVisibility(View.GONE); } } }; }
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"
>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
style="?android:attr/progressBarStyleHorizontal"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout>
?
?
?
?
?
代码二、
package com.mnkjxy;
import android.app.Activity;
import android.app.ProgressDialog;
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;
public class MyActivity extends Activity {
private Button button = null;
private ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
processThread();
}
});
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 程序运行到这里关闭
dialog.dismiss();
}
};
public void processThread() {
dialog = ProgressDialog.show(MyActivity.this, "正在下载资源", "请稍等···");
new Thread() {
public void run() {
longTimeWait();
handler.sendEmptyMessage(0);
}
}.start();
}
public void longTimeWait() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
? 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"
>
<TextView
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
/>
</LinearLayout>
?
?
?