我无法显示我的 ProgressDialog,我不确定为什么?
代码:
public class Activity001 extends Activity implements OnClickListener
{
public void onClick(View view)
{
ProgressDialog progressDialog = ProgressDialog.show(Activity001.this, "", "wait ", true, false);
// Sending some files to server .. takes time .. - (method that take 1-2 minutes )
progressDialog.dismiss();
}
}
有人可以帮我吗?
最佳答案
试试这个。
在 onCreate() 中执行此操作
Handler handler;
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
Pdialog.dismiss();
}
};
现在使用线程将文件上传到服务器。修改这段代码,
public class Activity001 extends Activity implements OnClickListener
{
public void onClick(View view)
{
ProgressDialog progressDialog = ProgressDialog.show(Activity001.this, "", "wait ", true, false);
Thread ProgressThread = new Thread() {
@Override
public void run() {
try {
// Sending some files to server .. take time .. - (method that take 1-2 minuts )
} catch(InterruptedException e) {
// do nothing
} finally {
handler.sendEmptyMessage(0);
}
}
};
ProgressThread.start();
}
}
编辑
进度对话框是一个轻量级对象。它并不优先于主线程中发生的其他任务。例如,当您在主线程中调用一个进度对话框的 show() ,然后调用一个 http 进程或类似的东西时,它将不允许轻量级的 prgoress 对话框优先。显然它只会在 http 进程结束时出现。
关于android - 为什么我看不到使用此 Activity 的 ProgressDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8712295/