0

我正在使用 IntentService 从 url 下载文件,使用以下代码:

    package com.example.myapp;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;

    import android.app.Activity;
    import android.app.IntentService;
    import android.content.Intent;
    import android.os.Environment;
    import android.util.Log;

    public class DownloadService extends IntentService {

        private int result = Activity.RESULT_CANCELED;
        public static final String URL = "urlpath";
        public static final String FILENAME = "filename";
        public static final String DOWNLOAD_POSITION = "position";
        public static final String FILEPATH = "filepath";
        public static final String RESULT = "result";
        public static final String PERCENTAGE = "PERCENTAGE";
        public static final String FILE_SIZE = "FILESIZE";
        public static final String NOTIFICATION = "service receiver";
        public static BufferedInputStream bis;
        public static long lengthofFile;
        public int dnlPosition;
        public String fileName;
        public DownloadService() {
            super("DownloadService");
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            String urlPath = intent.getStringExtra(URL);
            fileName = intent.getStringExtra(FILENAME);
            dnlPosition = intent.getIntExtra("downloadPosition", 0);
            File output = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    fileName);
            if (output.exists()) {
                output.delete();
            }

            URLConnection streamConnection = null;
            InputStream stream = null;
            FileOutputStream fos = null;
            try {

                URL url = new URL(urlPath);
                streamConnection = url.openConnection();
                stream = streamConnection.getInputStream();
                streamConnection.connect();
                lengthofFile = streamConnection.getContentLength();
                String fileSize = Long.toString(lengthofFile);
                InputStream reader = stream;

                bis = new BufferedInputStream(reader);
                fos = new FileOutputStream(output.getPath());
                int next = -1;
                int progress = 0;
                int bytesRead = 0;
                byte buffer[] = new byte[1024];

                while ((bytesRead = bis.read(buffer)) > 0) {
                    fos.write(buffer, 0, bytesRead);
                    progress += bytesRead;
                    int progressUpdate = (int)((progress * 100) / lengthofFile);
                    Intent testIntent = new Intent("com.example.myapp.MESSAGE_INTENT");
                    testIntent.putExtra(PERCENTAGE, progressUpdate);
                    testIntent.putExtra(FILE_SIZE, lengthofFile);
                    testIntent.putExtra(DOWNLOAD_POSITION, dnlPosition);
                    testIntent.putExtra(FILENAME, fileName);
                    sendBroadcast(testIntent);
                }
                result = Activity.RESULT_OK;
                fos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (stream != null) {
                    try {
                        stream.close();
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            publishResults(output.getAbsolutePath(), result);
        }
    }

它可以正常工作,但是如果我想取消正在进行的下载或其中一个排队的文件,我仍然停留在这一步,我想我可以在取消按钮点击服务将发送广播听它,如果广播说打破while ((bytesRead = bis.read(buffer)) > 0)循环,它就会打破它。

这种情况甚至可能吗?

问候

4

0 回答 0