Android 线程 实例介绍
2012-06-09 13:34:02 来源:WEB开发网package gongzibai.co.cc;
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.TextView;
public class EX3Activity extends Activity {
public static final int UPDATE_DATA = 0;
public static final int UPDATE_COMPLETED = 1;
TextView mTextView;
Button mButton;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_DATA:
mTextView.setText("正在更新来自线程的数据:" + msg.arg1 + "%......");
break;
case UPDATE_COMPLETED:
mTextView.setText("已完成来自线程的数据");
break;
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView1);
mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(30);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Message m = mHandler.obtainMessage();
m.what = UPDATE_DATA;
m.arg1 = i + 1;
mHandler.sendMessage(m);
}
mHandler.sendEmptyMessage(UPDATE_COMPLETED);
}
}.start();
}
});
}
}
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
赞助商链接