1

Zip ファイルをダウンロードし、ダウンロードの進行状況を通知内に表示する AsyncTask を作成しようとしています。

私は電話しています:

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);
}

通知をキャンセルする方法をまだ探しています。

4

2 に答える 2