【问题标题】:AsyncTask with notification带通知的 AsyncTask
【发布时间】:2014-01-01 22:14:25
【问题描述】:

我尝试创建一个 AsyncTask 来下载 Zip 文件并在通知中显示下载进度。

我在打电话:

new MyAsyncTask.DownloadTask(context, position,list).execute(0);

指的是这个:

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;

public class DownloadTask extends AsyncTask<Integer, Integer, Void> {
    private NotificationHelper mNotificationHelper;

    public int position;
    public ArrayList<HashMap<String, String>> list;

    public DownloadTask(Context context,int position, ArrayList<HashMap<String, String>> list) {
        mNotificationHelper = new NotificationHelper(context);
        this.position = position;
        this.list = list;
    }

    protected void onPreExecute() {
        mNotificationHelper.createNotification();
    }

    @SuppressLint("NewApi")
    @Override
    protected Void doInBackground(Integer... integers) {

            int count;
            try {
                URL url = new URL("http://myurl/test.zip");
                URLConnection conection = url.openConnection();
                conection.connect();
                int lenghtOfFile = conection.getContentLength();
                Log.d("Size: ", Integer.toString(lenghtOfFile));

                InputStream input = new bufferedInputStream(url.openStream(),8192);

                OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+ "/"+ list.get(position).get("Name") + ".zip");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress((int) ((total * 100) / lenghtOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        mNotificationHelper.progressUpdate(progress[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        mNotificationHelper.completed();
    }
}

它似乎工作得很好,但是当我单击按钮运行 AsyncTask 时,所有系统都在变慢,并且在下载完成之前无法再使用平板电脑。 (当 zip 为 100mo 时不是很有用)。

另外,我想让取消下载成为可能,但是当我尝试从我的主要活动中执行它时(使用类似这样的东西:MyAsynctask.cancel(true);),应用程序崩溃了。所以我想知道是否有适当的方法来做到这一点(也许从通知中提供最佳用户体验)。

编辑:

由于更新时间不那么重要的 buptcoder,延迟的问题得到了解决:

while ((count = input.read(data)) != -1) {
    total += count;
    if ((count % 10) == 0) {
        publishProgress((int) ((total * 100) / ;lenghtOfFile));
    }
    output.write(data, 0, count);
}

我仍在寻找取消通知的方法。

【问题讨论】:

  • 添加你的崩溃日志

标签: android android-asynctask android-notifications


【解决方案1】:

您更新通知的速度太快了。您必须降低更新频率。

【讨论】:

  • 它似乎适用于:while ((count = input.read(data)) != -1) { total += count; if ((count % 10) == 0) { publishProgress((int) ((total * 100) / ;lenghtOfFile)); } output.write(data, 0, count); },谢谢!
【解决方案2】:
new MyAsyncTask.DownloadTask(context, position,list).execute(0);

你没有类的实例,所以你不能取消它。

MyAsyncTask myTask = new MyAsyncTask.DownloadTask(context, position,list);
myTask.execute(0);
myTask.cancel(true);

【讨论】:

  • 我试过你的例子,但是 myTask.cancel(true);没有停止我的 AsyncTask,并且通知仍在运行。唯一的好处是应用程序不会崩溃。也许有办法中断html请求并从内部退出任务?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多