gpt4 book ai didi

java - onProgressUpdate 不起作用

转载 作者:行者123 更新时间:2023-12-02 07:07:21 25 4
gpt4 key购买 nike

private class DownloadTextTask extends AsyncTask<String,Long,Long> {

CharSequence contentText;
Context context;
CharSequence contentTitle;
PendingIntent contentIntent;
int ID = 1;
long time;
int icon;
CharSequence tickerText;

@Override
protected Long doInBackground(String... urls) {
InputStream inputStream = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(urls[0]));
inputStream = httpResponse.getEntity().getContent();
byte[] buffer = IOUtils.toByteArray(inputStream);
FileOutputStream fos = new FileOutputStream(MEDIA_PATH + "/fileName.mp3");
fos.write(buffer);
fos.flush();
fos.close();
} catch (Exception e) {
}
return (long) 100;
}

@Override
protected void onPostExecute(Long result) {
contentText = result + "% complete";
contentTitle="Downloading Finished!";
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(ID, notification);
}

@Override
protected void onPreExecute() {
super.onPreExecute();
downloadNotification();
}

@Override
public void onProgressUpdate(Long... progress) {
super.onProgressUpdate(progress);
contentText = progress[0] + "% complete";
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(ID, notification);
}

public void downloadNotification(){
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);

icon = R.drawable.downicon;
//the text that appears first on the status bar
tickerText = "Downloading...";
time = System.currentTimeMillis();

notification = new Notification(icon, tickerText, time);

context = getApplicationContext();
//the bold font
contentTitle = "Your download is in progress";
//the text that needs to change
contentText = "0% complete";
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
// notificationIntent.setType("audio/*");
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(ID, notification);

}
}

我编写了这段代码来下载 mp3 文件,这里的问题是,它没有更新下载文件的进度!我正在使用 IOUtils 类将 InputStream 转换为 byte[]。我不知道在这种情况下如何发布进展!请帮助我。

最佳答案

需要在doInBackground()中调用publishProgress(param)

http://developer.android.com/reference/android/os/AsyncTask.html

  1. doInBackground(Params...),在 onPreExecute() 完成执行后立即在后台线程上调用。此步骤用于执行可能需要很长时间的后台计算。异步任务的参数传递到这一步。该步骤必须返回计算结果,并将其传回上一步。 此步骤还可以使用publishProgress(Progress...)来发布一个或多个单位的进度。这些值在 onProgressUpdate(Progress...) 步骤中发布在 UI 线程上。

  2. onProgressUpdate(Progress...),在调用publishProgress(Progress...)后在UI线程上调用。执行的时间未定义。 此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于制作进度条动画或在文本字段中显示日志。

一个例子@http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/

        OutputStream output = new FileOutputStream("/sdcard/file_name.extension")
long total = 0;
int count;
while ((count = inputStream.read(buffer) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(buffer, 0, count);
}

关于java - onProgressUpdate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934487/

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