IntentService will be killed after I stop my application

后端 未结 5 1023
栀梦
栀梦 2021-02-05 11:50

I am referring to android design considerations: AsyncTask vs Service (IntentService?)

According to the discussion, AsyncTask does not suit, because it is ti

相关标签:
5条回答
  • 2021-02-05 12:28

    You are using startService(). The Service will run until it's code is done, or until Android decides it should be killed. Look up on bound services. On your Activity.onDestroy() you should call unbindService().

    0 讨论(0)
  • 2021-02-05 12:34

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

    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.

    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".

    0 讨论(0)
  • 2021-02-05 12:35

    Actually there are different kinds of services you can implement. Use a Service instead of IntentService. There you need to look at START_STICKY , START_NOT_STICKY and START_REDELIVER_INTENT you can keep your service running in background even if your activity dies. Android services

    0 讨论(0)
  • 2021-02-05 12:37

    You can invoke setIntentRedelivery(true) in the constructor of the IntentService

    0 讨论(0)
  • 2021-02-05 12:40

    In your IntentService you can override onStartCommand() returning START_REDELIVER_INTENT

    Then if killed, your service will be restarted automatically by the system after some time with the same Intent.

    Be sure to call the super implementation on onStartCommand() like this:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent,flags,startId);
        return START_REDELIVER_INTENT;
    }
    
    0 讨论(0)
提交回复
热议问题