Java 有没有在Asynctask中为onProgressUpdate使用两个不同类的示例?
我一直在到处寻找这个,我得到的唯一答案是“使用配对”,但我也无法让它工作 以下是我需要做的: 在Asynctask中,我需要更新进度条和文本。正因为如此,我的Asynctask generic不能只是整数,而不仅仅是字符串,而是两者兼而有之。这样我就可以在“onProgressUpdate”方法中拥有这两个类 有谁能给我一些例子或链接,告诉我如何在“doInBackground”中添加字符串和增加整数,以及如何在“onProgressUpdate”中实现这一点Java 有没有在Asynctask中为onProgressUpdate使用两个不同类的示例?,java,android,android-asynctask,Java,Android,Android Asynctask,我一直在到处寻找这个,我得到的唯一答案是“使用配对”,但我也无法让它工作 以下是我需要做的: 在Asynctask中,我需要更新进度条和文本。正因为如此,我的Asynctask generic不能只是整数,而不仅仅是字符串,而是两者兼而有之。这样我就可以在“onProgressUpdate”方法中拥有这两个类 有谁能给我一些例子或链接,告诉我如何在“doInBackground”中添加字符串和增加整数,以及如何在“onProgressUpdate”中实现这一点 多谢各位 您可以创建自己的简单类来
多谢各位 您可以创建自己的简单类来保存变量,然后传递它吗 或者,如果您传递一个可以解析并获取所需值的字符串,该怎么办?如果您使用第一个字符串+=“:”+int,那么可以使用
String myString = passedString.substring(0, passedString.lastIndexOf(":")))
int i = Integer.parseInt(passedString.substring(passedString.lastIndexOf(":")+1));
据我所知,你的问题;您主要想做两件事: 1) 在doIneBackground()中处理UI线程。 2) 实现onProgressUpdate() 基本上,我们不应该在后台进程运行时尝试访问UI线程。 原因很清楚…@操作系统级会有这么多线程在运行。如果我们可以从后台线程更新UI,那么屏幕上将会很混乱 对于第二个示例,我建议您看看这个示例:
ProgressDialog mProgressDialog;
mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
final DownloadTask downloadTask = new DownloadTask(YourActivity.this);
downloadTask.execute("the url to the file you want to download");
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
在AsynTask中:
private class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
getClass().getName());
wl.acquire();
try {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/file_name.extension");
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled())
return null;
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
}
catch (IOException ignored) { }
if (connection != null)
connection.disconnect();
}
} finally {
wl.release();
}
return null;
}}
问候
SathyaThanx,但我唯一的问题是如何将两种类型传递给onProgressUpdate。您的答案显示了整个asynctask工作流的运行情况,但只将1种类型传递给onProgressUpdate,这不是我想要的。无论如何,Thanx是的,我想这是唯一的办法。我只是想知道是否有其他方法可以传递这两个值,而不必创建自定义类。。。塔克斯。
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
if (result != null)
Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
else
Toast.makeText(context,"File downloaded", Toast.LENGTH_SHORT).show();
}