IntentService onHandleIntent()的onDestroy后仍在运行()发射 [英] IntentService onHandleIntent() still running after onDestroy() fired

查看:696
本文介绍了IntentService onHandleIntent()的onDestroy后仍在运行()发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的preference画面,我想启动一个服务从互联网时的preference一个被点击了下载文件。如果服务已经运行(下载文件),则服务应停止(取消下载)。

In my preference screen, I want to start a service to download files from the internet when one of the preference is being clicked. If the service is already running (downloading files), then the service should be stopped (cancel download).

public class Setting extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    downloadPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference pref) {
            if (DownloadService.isRunning) {
                Setting.this.stopService(new Intent(Setting.this,
                    DownloadService.class));
            } else {
                Setting.this.startService(new Intent(Setting.this,
                    DownloadService.class));
            }
            return false;
        }
    });
    }
}

服务类:

public class DownloadService extends IntentService {

public static final int DOWNLOAD_SUCCESS = 0;
public static final int DOWNLOAD_FAIL = 1;
public static final int DOWNLOAD_CANCELLED = 2;
public static final int SERVER_FAIL = 3;

public static boolean isRunning = false;
private int result;

public DownloadService() {
    super("DownloadService");
}

@Override
public void onCreate() {
    super.onCreate();
    isRunning = true;
}

@Override
protected void onHandleIntent(Intent intent) {
    if (NetworkStateUtils.isInternetConnected(getApplicationContext())) 
        result = downloadFiles(getApplicationContext());

}

@Override
public void onDestroy() {
    super.onDestroy();
    switch (result) {
    case DOWNLOAD_SUCCESS:
        Toast.makeText(getApplicationContext(), R.string.download_finished,
                Toast.LENGTH_SHORT).show();
        break;
    case DOWNLOAD_CANCELLED:
        Toast.makeText(getApplicationContext(), R.string.download_canceled,
                Toast.LENGTH_SHORT).show();
        break;
    case DOWNLOAD_FAIL:
        Toast.makeText(getApplicationContext(), R.string.download_failed,
                Toast.LENGTH_SHORT).show();
        break;
    }
    isRunning = false;
}
}

此服务旨在运行,直到下载完毕。函数 downloadFiles()不使用的AsyncTask 。它节省了的HttpURLConnection 的FileOutputStream 直接

This service is meant to run until download has finished. The function downloadFiles() uses no AsyncTask. It saves the HttpURLConnection with an FileOutputStream directly.

该服务启动正确的,当我点击了preference。现在的问题是,当我点击停止与 stopService()下载触发的onDestroy()立即;然而,根据日志, onHandleIntent()仍在运行becasue我仍然可以继续看到HTTP请求。这是因为服务运行在一个线程本身,还是我做错了什么?我怎样才能确保一切都在 onHandleIntent()立即停止(或者至少能停止),当 stopService()被称为?

The service started correctly when I click the preference. Now the problem is, when I click to stop the service with stopService(), DownloadService triggered onDestroy() immediately; however according to logs, onHandleIntent() is still running becasue I can still see HTTP requests continuously. Is this because Service runs in a thread itself, or am I doing something wrong? How can I ensure that everything in onHandleIntent() stops immediately (or at least able to stop) when stopService() is being called?

推荐答案

最后想出了如何使它发挥作用。

Finally figured out how to make it work.

正如我在我的问题说,不知何故onHandleIntent()将创建一个线程来完成这项工作。因此,即使当服务本身destoryed,线程仍在运行。我加入了一个全局变量来实现我的目标。

As I stated in my question, somehow onHandleIntent() will create a thread to do the job. So even when the service itself is destoryed, the thread is still running. I achieved my goal by adding a global variable

private static boolean isStopped = false;

下载类。

为了取消而不是调用我的服务,

In order to cancel my service, instead of calling

Setting.this.stopService(new Intent(Setting.this, DownloadService.class));

刚刚成立 DownloadService.isStopped = TRUE

最后,虽然处事 onHandleIntent(),定期检查这个布尔,看它是否应该停止下载。如果 isStopped = TRUE ,立即返回,该服务将自行停止。

Finally, while doing things in onHandleIntent(), check this boolean periodically to see if it should stop downloading. If isStopped = true, return immediately and the service will stop itself.

希望这可以帮助别人谁遇到这个问题了。并感谢您的时间阅读了这个问题。

Hope this helps someone who come across this problem too. And thanks for your time reading this question.

这篇关于IntentService onHandleIntent()的onDestroy后仍在运行()发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆