通过服务下载文件时获得巨大滞后

时间:2012-04-14 10:24:00

标签: android service delay lag

我有一个服务,它接收要下载的URL。 该服务比下载这些URL并将其保存到SD卡上的文件中。

当我在下载队列中放入2个以上的项目时,我的设备无法使用。它几乎冻结了。巨大的滞后等等。 有什么想法吗?

源码:

private static boolean downloading = false;
    private static ArrayList<DownloadItem> downloads = new ArrayList<DownloadItem>();

    /**
     * called once when the service started
     */
    public static void start() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    if (downloads.size() > 0 && !downloading) {
                        downloading = true;
                        DownloadItem item = downloads.get(0);
                        downloadSingleFile(item.getUrl(), item.getFile());
                    }
                }
            }
        });
        thread.start();
    }

    public static void addDownload(DownloadItem item) {
        downloads.add(item);
    }

    private static void downloadSuccessfullFinished() {
        if (downloads.size() > 0)
            downloads.get(0).setDownloaded(true);
        downloadFinished();
    }


    private static void downloadFinished() {
        // remove the first entry; it has been downloaded
        if (downloads.size() > 0)
            downloads.remove(0);
        downloading = false;
    }

    private static void downloadSingleFile(String url, File output) {
        final int maxBufferSize = 4096;

        HttpResponse response = null;
        try {
            response = new DefaultHttpClient().execute(new HttpGet(url));
            if (response != null
                    && response.getStatusLine().getStatusCode() == 200) {
                // request is ok
                InputStream is = response.getEntity().getContent();
                BufferedInputStream bis = new BufferedInputStream(is);
                RandomAccessFile raf = new RandomAccessFile(output, "rw");
                ByteArrayBuffer baf = new ByteArrayBuffer(maxBufferSize);
                long current = 0;
                long i = 0;

                // read and write 4096 bytes each time
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                    if (++i == maxBufferSize) {
                        raf.write(baf.toByteArray());
                        baf = new ByteArrayBuffer(maxBufferSize);
                        i = 0;
                    }
                }

                if (i > 0) // write the last bytes to the file
                    raf.write(baf.toByteArray());

                baf.clear();
                raf.close();
                bis.close();
                is.close();

                // download finished get start next download
                downloadSuccessfullFinished();
                return;
            }
        } catch (Exception e) {
            // not successfully downloaded
            downloadFinished();
            return;
        }
        // not successfully downloaded
        downloadFinished();
        return;
    }

1 个答案:

答案 0 :(得分:0)

您应该使用AsyncTask或开始下载过程的线程。