0

Android サービスを使用して URL から画像ファイルをダウンロードする必要があります。ファイルはダウンロードされますが、通知に進行状況バーを追加しようとすると、ダウンロードの進行状況を確認できるように、デバイスがハングするか、進行状況バーが実行され続けてファイルがダウンロードされません。どうすれば達成できますか?

ファイルをダウンロードし、通知に進行状況バーを表示する Android サービスをコーディングする

package com.tutorial.app;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.State;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.util.ByteArrayBuffer;



import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.widget.ProgressBar;
import android.widget.RemoteViews;


public class DownloadProgress extends IntentService {

    public DownloadProgress() {
        super("");
        // TODO Auto-generated constructor stub
    }
    ProgressBar progressBar;
    private List<Integer> progress = new  ArrayList<Integer>();
    int currentProgress = 5;
//    Notification notification;
//    NotificationManager notificationManager;
    String filepath = "/mnt/sdcard/downloaf/first.jpg";



    @Override
    protected void onHandleIntent(Intent intent1) {

    try {
        UpdateProgress();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    }

    private void UpdateProgress() throws IOException {
        Thread download = null;
        Intent intent = new Intent(this, DownloadProgress.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
        @SuppressWarnings("deprecation")
        final Notification notification = new Notification(R.drawable.icon, "simulating a download", System
                .currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
        notification.contentView.setTextViewText(R.id.status_text, "simulation in progress");
        notification.contentView.setProgressBar(R.id.status_progress, 100, currentProgress, false);
        final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                getApplicationContext().NOTIFICATION_SERVICE);
        notificationManager.notify(42, notification);

        download = new Thread() {

            @Override
            public void run() {

                for (int i = 1; i < 20; i++) {
                    currentProgress ++;//+= (progress.get(i)*10)/100;
                    notification.contentView.setProgressBar(R.id.status_progress, 100, currentProgress, false);

                    // inform the progress bar of updates in progress
                    notificationManager.notify(42, notification);

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                // remove the notification (we're done)


            }

        };
        URL url = new URL("http://www.ilikewallpaper.net/The-new-iPad-wallpapers/download/4755/Assassins-Creed-3-The-new-iPad-wallpaper-ilikewallpaper_com_1024.jpg");
        File file = new File(filepath);
        URLConnection con = url.openConnection();
        long fileLength = con.getContentLength();
        InputStream stream = con.getInputStream();
        byte[] buff = new byte[5 * 1024];
        BufferedInputStream buffer = new BufferedInputStream(stream,1024*5);
        FileOutputStream outputStream = new FileOutputStream(file);

        int current = 0;

             while ((current = buffer.read()) != -1) {
                    progress.add(current);
                    synchronized (outputStream) {
                        download.run();
                        outputStream.write(buff, 0, current);
                        notificationManager.cancel(42);
                    }


                }



//          download.run();
             outputStream.flush();
        outputStream.close();
        stream.close();






    }


}
4

2 に答える 2

0

通知の頻度が高すぎる可能性があります。それがフリーズする理由です。それらをより大きな間隔で更新させます。OK は 1 秒または 2 秒に 1 回です。通知サービスでAndroidメモリリーク
も確認してください

于 2012-09-19T07:24:29.370 に答える
0

このスレッドは古すぎます。しかし、誰かが私のために働いたことを助けてくれることを願っています.

APIレベル9で追加されたAndroidで使用DownloadManagerしました。使い方は非常に簡単で、通知の表示について心配する必要はありません。ほんの数行のコード

DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("your_any_download_url");
DownloadManager.Request request = new Request(uri);
Long reference = downloadmanager.enqueue(request);

詳細についてDownloadManagerは、Android Developer Siteを参照してください。また、こちらのチュートリアルに従ってください。

于 2016-06-20T05:58:39.277 に答える