我停止应用程序后,IntentService将被终止 [英] IntentService will be killed after I stop my application

查看:178
本文介绍了我停止应用程序后,IntentService将被终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是 android设计注意事项:AsyncTask与Service(IntentService?)

根据讨论, AsyncTask不适合,因为它与您的活动紧密绑定"

According to the discussion, AsyncTask does not suit, because it is tightly "bound" to your Activity

因此,我启动了 Thread (我假设AsyncTask和Thread属于同一类别),其中包含一个无限运行循环并进行了以下测试.

So, I launch a Thread (I assume AsyncTask and Thread belong to same category), have an infinity running loop in it and did the following testing.

  • 退出我的应用程序,方法是不断按返回软键,直到看到主屏幕.线程仍在运行.
  • 通过 管理应用->应用->强制停止
  • 杀死我的应用.线程已停止.
  • I quit my application, by keeping pressing on back soft key, till I saw home screen. Thread is still running.
  • I kill my application, by going to Manage apps -> App -> Force stop. Thread is stopped.

因此,我希望在从 Thread 更改为 Service 后,即使我退出,我的 Service 仍将保持活动状态杀死.

So, I expect after I change from Thread to Service, my Service will keep alive even after I quit or kill my app.

Intent intent = new Intent(this, SyncWithCloudService.class);
startService(intent);


public class SyncWithCloudService extends IntentService {
    public SyncWithCloudService() {
        super("SyncWithCloudService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        int i = 0;
        while (true) {
            Log.i("CHEOK", "Service i is " + (i++));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Log.i("CHEOK", "", ex);
            }
        }
    }
}


    // Doesn't matter whether I use "android:process" or not.
    <service 
        android:name="com.xxx.xml.SyncWithCloudService" 
        android:process=".my_process" >
    </service>


但是,我的发现是


However, my finding is that,

  • 退出我的应用程序,方法是不断按返回软键,直到看到主屏幕.服务仍在运行.
  • 通过 管理应用->应用->强制停止
  • 杀死我的应用.服务已停止.
  • I quit my application, by keeping pressing on back soft key, till I saw home screen. Service is still running.
  • I kill my application, by going to Manage apps -> App -> Force stop. Service is stopped.

Service Thread 的行为似乎是相同的.那么,为什么我应该使用 Service 而不是 Thread ?我有什么错过的吗?我以为我的 Service 应该可以继续运行,即使我杀死了我的应用程序也是如此?

It seems that the behaviour of Service and Thread are the same. So, why I should use Service instead of Thread? Is there anything I had missed out? I thought my Service suppose to keep running, even after I kill my app?

推荐答案

不是.当您终止应用程序时,服务将停止运行.当您杀死应用程序时,它的所有组件(活动,服务等)都会被杀死.

Nope. Service will stop running when you kill your application. When you kill your application all components of it are killed (activities, services, etc.).

通常,线程和服务的行为类似.但是,如果您从活动中启动线程,然后关闭活动(即:退出应用程序),则最终Android将注意到您的进程中没有活动组件(因为Android无法将您的线程识别为活动组件)它将杀死您的进程,从而杀死线程.

In general the behaviour of Thread and Service are similar. However, If you start a Thread from an Activity and then shutdown the activity (ie: quit your application), eventually Android will notice that your process has no active components in it (since Android doesn't recognize your Thread as an active component) and it will just kill your process, thereby killing the thread.

但是,如果您正在运行服务,那么Android会注意到您正在运行服务,而不会立即终止该服务.但是,如果未使用" Android,仍然有可能终止您的服务进程.

However, if you have a Service running, then Android will notice that you have a service running and not kill it so readily. However, it is still possible that Android will kill your service process if it isn't "in use".

这篇关于我停止应用程序后,IntentService将被终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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