android-通过广播中断while循环来取消正在进行的IntentService下载

android-通过广播中断while循环来取消正在进行的IntentService下载,android,android-intentservice,android-broadcastreceiver,Android,Android Intentservice,Android Broadcastreceiver,我正在使用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.n

我正在使用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);
        }
    }
它可以正常工作,但是如果我想取消正在进行的下载或一个排队的文件,我仍然停留在这一步, 我在想,我可以在“取消”按钮上发送一个广播,单击服务将收听的位置,如果广播说“中断
”,而((bytesRead=bis.read(buffer))>0)
循环则会中断它

这种情况可能吗


关于

我相信您必须提出自己的实现,从您的服务创建和管理异步文件传输,以便服务本身能够对取消请求做出响应。可能需要查看一下。请注意,这与安卓
服务
没有任何关系!这只是一个巧合的命名冲突。