Android 意图服务在完成任务之前停止?

Android 意图服务在完成任务之前停止?,android,service,Android,Service,我有一个IntentService我正在另一个线程中执行一些任务 onHandleIntent(意图) 为什么在执行操作(任务)之前,IntentService会停止 这是我的密码: public class SampleIntentService extends IntentService { public static final int DOWNLOAD_ERROR = 10; public static final int DOWNLOAD_SUCCESS = 11;

我有一个
IntentService
我正在另一个线程中执行一些任务

onHandleIntent(意图)

为什么在执行操作(任务)之前,
IntentService
会停止

这是我的密码:

public class SampleIntentService extends IntentService {

    public static final int DOWNLOAD_ERROR = 10;
    public static final int DOWNLOAD_SUCCESS = 11;

    public SampleIntentService() {
        super(SampleIntentService.class.getName());

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        final String url = intent.getStringExtra("url");
        final ResultReceiver receiver = intent.getParcelableExtra("receiver");
        final Bundle bundle = new Bundle();
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                return downloadFile(url, receiver);
            }

            @Override
            protected void onPostExecute(String filePath) {
                super.onPostExecute(filePath);
                bundle.putString("filePath", filePath);
                receiver.send(DOWNLOAD_SUCCESS, bundle);
            }
        };

    }

    private String downloadFile(String url, ResultReceiver receiver) {
        File downloadFile = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "test.png");
        if (downloadFile.exists())
            downloadFile.delete();
        try {
            downloadFile.createNewFile();
            URL downloadURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) downloadURL
                    .openConnection();
            int responseCode = conn.getResponseCode();
            if (responseCode != 200)
                throw new Exception("Error in connection");
            InputStream is = conn.getInputStream();
            FileOutputStream os = new FileOutputStream(downloadFile);
            byte buffer[] = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                os.write(buffer, 0, byteCount);
            }
            os.close();
            is.close();

            String filePath = downloadFile.getPath();
            return filePath;
        } catch (Exception e) {
            receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY);
            e.printStackTrace();
        }
        return null;
    }

}
公共类SampleIntentService扩展了IntentService{
公共静态最终整数下载错误=10;
公共静态最终int下载_成功=11;
公共SampleIntentService(){
super(SampleIntentService.class.getName());
}
@凌驾
受保护的手部内容无效(意图){
最终字符串url=intent.getStringExtra(“url”);
最终结果接收方=intent.getParcelableExtra(“接收方”);
最终捆绑=新捆绑();
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
返回下载文件(url、接收者);
}
@凌驾
受保护的void onPostExecute(字符串文件路径){
onPostExecute(文件路径);
bundle.putString(“filePath”,filePath);
发送(下载成功,捆绑);
}
};
}
私有字符串下载文件(字符串url,ResultReceiver接收器){
File downloadFile=新文件(Environment.getExternalStorageDirectory()+File.pathseptor+“test.png”);
if(downloadFile.exists())
downloadFile.delete();
试一试{
downloadFile.createNewFile();
URL downloadURL=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)下载URL
.openConnection();
int responseCode=conn.getResponseCode();
如果(响应代码!=200)
抛出新异常(“连接错误”);
InputStream is=conn.getInputStream();
FileOutputStream os=新的FileOutputStream(下载文件);
字节缓冲区[]=新字节[1024];
int字节数;
而((字节数=is.read(缓冲区))!=-1){
写操作(缓冲区,0,字节数);
}
os.close();
is.close();
字符串filePath=downloadFile.getPath();
返回文件路径;
}捕获(例外e){
receiver.send(下载错误,Bundle.EMPTY);
e、 printStackTrace();
}
返回null;
}
}
根据文件:

abstract void onHandleIntent(Intent intent)
此方法在具有处理请求的工作线程上调用

由于此方法已在工作线程上调用,因此无需启动其他线程

如果您这样做,
onHandleIntent(Intent-Intent)
将返回,而
IntentService
将认为任务已完成,它将自行停止

资料来源:

以下是更新的代码:

public class SampleIntentService extends IntentService {

    public static final int DOWNLOAD_ERROR = 10;
    public static final int DOWNLOAD_SUCCESS = 11;

    public SampleIntentService() {
        super(SampleIntentService.class.getName());

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        final String url = intent.getStringExtra("url");
        final ResultReceiver receiver = intent.getParcelableExtra("receiver");
        final Bundle bundle = new Bundle();
        String filePath = downloadFile(url, receiver);
        bundle.putString("filePath", filePath);
        receiver.send(DOWNLOAD_SUCCESS, bundle);

    }

    private String downloadFile(String url, ResultReceiver receiver) {
        File downloadFile = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "test.png");
        if (downloadFile.exists())
            downloadFile.delete();
        try {
            downloadFile.createNewFile();
            URL downloadURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) downloadURL
                    .openConnection();
            int responseCode = conn.getResponseCode();
            if (responseCode != 200)
                throw new Exception("Error in connection");
            InputStream is = conn.getInputStream();
            FileOutputStream os = new FileOutputStream(downloadFile);
            byte buffer[] = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                os.write(buffer, 0, byteCount);
            }
            os.close();
            is.close();

            String filePath = downloadFile.getPath();
            return filePath;
        } catch (Exception e) {
            receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY);
            e.printStackTrace();
        }
        return null;
    }

嘿,这个问题没有提到。但是,您是如何使用hj.o作为GC根用户来解决您的主要问题的呢?我也有同样的问题。谢谢:)在搜索我的问题时,我在谷歌缓存中遇到了你的问题。