gpt4 book ai didi

java - 无法让 ProgressDialog 显示在 android 中

转载 作者:行者123 更新时间:2023-12-01 11:45:04 25 4
gpt4 key购买 nike

我无法在需要时显示进度对话框。我尝试将它放入我的 asyncTask ui 类以及在 ui 上运行的它自己的线程中,但没有一个起作用。谁能帮我吗?

调用progressDialog方法的方法:

 public void shareTest(View view){       //method called to jump to share activity if criteria matched

if(checkInputs()) { //call to check inputs
Share start = new Share();
boolean isConnected=start.connectToServer(); //connectToServer
Intent intent = new Intent(HomeScreen.this, Share.class); //create intent to move to share class from this activity

startProgressDialog();
if (isConnected) { //check to see if isconnected was succesful
if (Share.matchFound ){ //check to see if a match was found
progress.dismiss();
startActivity(intent); //if true jump to share activity
} else {
while (!Share.timedOut) { //While the time has not timedOut
if (Share.matchFound) { //if a share has been found

startActivity(intent); //start share activity
break; //if true then break
}
}
if (Share.timedOut) {
//send an notice that a match wasn't found
sendToast(getString(R.string.noShare)); //if not true then send Toast
}
}
}
else sendToast(getString(R.string.errServCon)); //if connection to server failed then send toast
}

}

方法是这样的:

无效startProgressDialog() {

    new Thread(new Runnable() {
@Override

public void run() { //creates a new runnable thread
// Issue command() on a separate thread
while (!Share.matchFound) { //while havent been asked to disconnect //if a new location has been recieved
activity.runOnUiThread(new Runnable() {
@Override
public void run() { //run on the ui thread act
progress.show(); //call the method that does the update
}
});

}
progress.dismiss();
}
}).start();
}

最佳答案

声明一个全局变量,如下所示:

ProgressDialog progress;

无论您想在哪里显示进度,请粘贴以下代码:

progress = ProgressDialog.show(this, "Please wait",
"Loading..", true);

完成后,只需将其关闭即可:

progress.dismiss();

关于java - 无法让 ProgressDialog 显示在 android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com