意图服务未明确停止

时间:2017-06-27 06:10:03

标签: android android-service android-notifications android-intentservice

我使用意向服务创建了下载任务。它显示带有百分比的进度条的通知。我正在使用本地广播管理器在下载时传递数据。我还在通知中添加了一个按钮来取消下载但问题是我点击取消时下载它不停止意向服务。我怎么能停止意向服务。在这里,我把我的Intent服务代码和广播接收器关闭服务。

public class DownloadService extends IntentService {
    public DownloadService() {
        super(DownloadService.class.getName());
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        InputStream input;
        OutputStream output;
        HttpURLConnection connection;
        Intent intent1 = new Intent();
        intent1.setAction("com.demo.downloading");
        try {
            URL url = new URL(urlToDownload);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return;
            }

            final int fileLength = connection.getContentLength();
            input = connection.getInputStream();
            output = new FileOutputStream(file);

            byte data[] = new byte[8192];
            long total = 0;
            int count, latestPercentDone;
            int percentDone = -1;
            while ((count = input.read(data)) != -1) {
                total += count;
              latestPercentDone = (int) (total * 100 / fileLength);
                if (percentDone != latestPercentDone) {
                    percentDone = latestPercentDone;
                    if (percentDone < 100) {
                        if (percentDone != 0) {
                            intent1.putExtra("progress", "" + percentDone);
                            intent1.putExtra("IsCancel", false);
                            LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
                        }
                    }
                    if (percentDone == 100) {
                        intent1.putExtra("progress", "" + 0);
                        intent1.putExtra("IsCancel", false);
                        LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
                    }
                }
                output.write(data, 0, count);
                if (StaticFields.cancelDownload) {
                    Log.d(TAG, "onHandleIntent: Download Cancel");
                    this.stopSelf();
                }
            }
            output.close();
        } catch (IOException e) {
            intent1.putExtra("progress", "" + (-1));
            intent1.putExtra("IsCancel", true);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
            e.printStackTrace();
            is_all_download = false;
        }
    }
}

我的广播接收器类 OnReceive方法()

if (intent.getAction() != null) {
            if (action.equals("notification_cancelled")) {
                    Global.cancelDownload = true;
                    Intent intent1 = new Intent();
                    intent1.setAction("com.demo.downloading");
                    intent1.putExtra("IsCancel", true);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent1);
                }
            }
        }

LocalBroadcast manager接收方法

boolean isCancel = intent.getBooleanExtra("IsCancel", false);
            if (isCancel) {
                Global.cancelDownload = true;
                mContext.stopService(serviceIntent);
}

1 个答案:

答案 0 :(得分:1)

IntentService中,onHandleIntent()在工作(后台)线程上调用。当您调用stopService()时,这对工作线程没有影响,因为它们仍将运行完成。无论如何,在stopService()上调用IntentService毫无意义,因为IntentService在有工作要做的时候会运行,并在完成所有工作后自行停止。

您需要在stopService()中设置一个可以在Service方法的循环中检查的标记,而不是调用onHandleIntent()。如果设置了该标志,则应该中止循环中的进一步处理并自行结束工作线程。