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

时间:2016-11-23 15:43:04

标签: android service

我有一个IntentService我正在

中的另一个线程中执行某项任务

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();
        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;
    }

}

1 个答案:

答案 0 :(得分:2)

根据文件:

abstract void onHandleIntent(Intent intent)
  

在工作线程上调用此方法并请求处理。

由于此方法已在工作线程上调用,因此您无需启动另一个线程。

如果您这样做,onHandleIntent(Intent intent)将返回,IntentService会认为任务已完成,并且它将自行停止。

来源:onHandleIntent

以下是更新后的代码:

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;
    }