gpt4 book ai didi

android - 加载数据时显示进度对话框

转载 作者:行者123 更新时间:2023-11-29 14:51:35 26 4
gpt4 key购买 nike

我想在从远程服务器加载一些数据时显示一个进度对话框:

我正在使用以下线程来获取数据并且它正在运行,但我无法显示 Activity 的进度条:

public class Request {

public String text ;
public boolean downloadText(String urlStr) {
final String url = urlStr;
new Thread () {
public void run() {
int BUFFER_SIZE = 2000;
InputStream in = null;
Message msg = Message.obtain();
msg.what=2;
try {
in = openHttpConnection(url);

InputStreamReader isr = new InputStreamReader(in);
int charRead;
text = "";
char[] inputBuffer = new char[BUFFER_SIZE];

while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
text += readString;
inputBuffer = new char[BUFFER_SIZE];
}
Bundle b = new Bundle();
b.putString("text", text);
msg.setData(b);
in.close();

}catch (IOException e) {
e.printStackTrace();
}


}
}.start();

}

你能告诉我我该怎么做吗!!

最佳答案

如下创建类,只调用该类的对象。

class MyTask extends AsyncTask<Void, Void, Void> {

ProgressDialog Asycdialog = new ProgressDialog(ActivityName.this);

@Override
protected void onPreExecute() {

super.onPreExecute();
Asycdialog.setMessage("Loading...");
Asycdialog.show();
}

@Override
protected Void doInBackground(Void... arg0) {

// do the task you want to do. This will be executed in background.
return null;
}

@Override
protected void onPostExecute(Void result) {

super.onPostExecute(result);
Asycdialog.dismiss();
}
}

关于android - 加载数据时显示进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16275913/

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