How can we restart the service?

I am developing an Android application that has a background launch of a service.

When I replace the application from the "Recent Application List", this will disable the application and terminate the service. (The following method has code for the same.)

    @Override
public void onTaskRemoved(Intent rootIntent)
{
       //code to be executed
       //Stop service
       super.onTaskRemoved(rootIntent);
    }

The service start code is in the Application class (onCreate ()), it will never be executed if the application resumes.

Positive scenario

1) If I restarted the application after the service was successfully completed, a new instance of the application will be and the service will also start.

Negative scenario

1) , , , ( ). , , , . , , .

, , .

?

1) . 2) .

.

+4
3

onStartCommand() Service, return "START_STICKY", , Android ( ).

0

OnResume , Intent

  @Override
    protected void onResume() 
    {
        /*   This will be called when starting the UI and resume from background
         *   If the service is not running, then start the service and bind to the service. 
         *   If the service is already running, then just bind with the service. The status
         *   of the service is determined by the #DetermineServiceStatus function.
         */


      }
0

. Android, .

- START_STICKY onStartCommand() ( ), , , , , Android.

0
source

Source: https://habr.com/ru/post/1529359/


All Articles