Android---Handler多线程通信示例(ProgressBar)
1、修改main.xml文件
[html]
<?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
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressBar1"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:visibility="visible"
android:layout_height="wrap_content"
android:layout_width="match_parent"></ProgressBar>
</LinearLayout>
<?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
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressBar1"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:visibility="visible"
android:layout_height="wrap_content"
android:layout_width="match_parent"></ProgressBar>
</LinearLayout>2、声明子线程SonThread.java
[java]
package com.bison;
import android.os.Message;
public class SonThread extends Thread {
private int i = 0;
@Override
public void run() {
while (i < 100) {
i = i + 10;
Message msg = new Message();
msg.arg1 = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HandlerTestActivity.myHandler.sendMessage(msg);
}
if (i == 100) {
HandlerTestActivity.myHandler.removeCallbacks(this);
}
}
}
package com.bison;
import android.os.Message;
public class SonThread extends Thread {
private int i = 0;
@Override
public void run() {
while (i < 100) {
i = i + 10;
Message msg = new Message();
msg.arg1 = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HandlerTestActivity.myHandler.sendMessage(msg);
}
if (i == 100) {
HandlerTestActivity.myHandler.removeCallbacks(this);
}
}
}3、MainActivity代码
[java]
package com.bison;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class HandlerTestActivity extends Activity {
private ProgressBar progressBar;
private SonThread myThread;
public static Handler myHandler;
public void init() {
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
myThread = new SonThread();
myThread.start();
myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressBar.setProgress(msg.arg1);
}
};
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}
package com.bison;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class HandlerTestActivity extends Activity {
private ProgressBar progressBar;
private SonThread myThread;
public static Handler myHandler;
public void init() {
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
myThread = new SonThread();
myThread.start();
myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressBar.setProgress(msg.arg1);
}
};
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ma
补充:移动开发 , Android ,