gpt4 book ai didi

Android : AsyncTask, 如何更新 ProgressDialog 增量

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:55 25 4
gpt4 key购买 nike

我想解析一个网页并在水平方向上可视化一个 progressdialog 样式并将其逐字节递增,这可能吗?

最佳答案

尝试这样的事情,

创建 ProgressDialog。

ProgressDialog mProgressDialog = new ProgressDialog(Your_Activity.this);
mProgressDialog.setMessage("Here you can set a message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
MyAsyncTask obj = new MyAsyncTask ();
obj.execute("url");

您的 AsyncTask 类。

private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int length = connection.getContentLength();

// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.txt");

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/length));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
@Override
public void onProgressUpdate(String... args){
mProgressDialog.setProgress(args[0]);
}
}
}

您必须在 AndroidManifest 文件中授予这些权限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于Android : AsyncTask, 如何更新 ProgressDialog 增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590555/

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