参照网上的例子,自己实现了一个进度条对话框,模拟图片上传,效果图如下:
布局很简单
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
下面是实现代码:
public class ProgressActivity extends Activity {
/** Called when the activity is first created. */
private Button button1;
private ProgressDialog dialog;
int fileSize;
int downLoadFileSize;
int m_count;
// 定义Handler对象
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case 0:
dialog.setMax(fileSize);
case 1:
dialog.setProgress(downLoadFileSize);
break;
case 2:
//dialog.setMessage("上传完成");
dialog.dismiss();
new AlertDialog.Builder(ProgressActivity.this).setTitle("提示信息")
.setMessage("上传完成!")
.setNegativeButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
break;
case -1:
String error = msg.getData().getString("error");
Toast.makeText(ProgressActivity.this, error, 1).show();
break;
}
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Resources res= this.getResources();
// 查看UI组件所在的线程名
Log.i("tag", "onCreate()-->" + Thread.currentThread().getName());
// 定义UI组件
button1 = (Button) findViewById(R.id.button1);
// 给按钮绑定单击事件监听器
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击按钮后去处理长耗时操作
processThread();
}
});
}
private void processThread() {
// 构建一个下载进度条
// 创建ProgressDialog对象
dialog = new ProgressDialog(ProgressActivity.this);
// 设置进度条风格,风格为长形
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 标题
dialog.setTitle("提示信息");
// 设置ProgressDialog 提示信息
dialog.setMessage("正在上传......");
// 设置ProgressDialog 标题图标
// dialog.setIcon(R.drawable.img2);
// 设置ProgressDialog 进度条进度
dialog.setProgress(100);
// 设置ProgressDialog 的进度条是否不明确
dialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
dialog.setCancelable(true);
// 设置ProgressDialog 的一个Button
// dialog.setButton("取消", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int i)
// {
// //点击“确定按钮”取消对话框
// dialog.cancel();
// }
// });
// 让ProgressDialog显示
dialog.show();
Log.i("tag", "processThread()-->" + Thread.currentThread().getName());
new Thread() {
@Override
public void run() {
Log.i("tag", "run()-->" + Thread.currentThread().getName());
// 在新线程里执行长耗时方法
longTimeMethod();
// 执行完毕后给handler发送一个空消息
handler.sendEmptyMessage(0);
}
}.start();
}
// 模拟下载文件的长耗时方法
protected void longTimeMethod() {
// TODO Auto-generated method stub
try {
Log.i("tag", "longTimeMethod-->" + Thread.currentThread().getName());
// Thread.sleep(10000);
fileSize = 100;
sendMsg(0);
while (m_count <= 100) {
// 由线程来控制进度。
// dialog.setProgress(m_count++);
downLoadFileSize = m_count;
sendMsg(1);
Thread.sleep(100);
m_count++;
}
sendMsg(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendMsg(int flag) {
Message msg = new Message();
msg.what = flag;
handler.sendMessage(msg);
}
}