On android 8 the Service stops and does not restart when Activity quits

1

I am testing a service but when the activity is terminated the service stops temporarily, then the service tries to restart, the onCreate method is called but onDestroy is called in sequence and the service is definitely called.

I'm running on two physical devices, the problem occurs on android 8 on android 6 the service can reboot.

I can not identify if the problem is running from AndroidStudio (debug), android 8 or device.

I also can not check if an exception has occurred in the execution of the service because the app instance has closed in the IDE.

minSdkVersion 16
targetSdkVersion 28

AndroidManifest.xml

 <service
            android:name="app.service.CServiceMain"
            android:exported="false"
            android:enabled="true">
        </service>

CServiceMain.java

public class CServiceMain extends Service
{

  private CCounterWorker worker;

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

    CMessage.debug(getClass(),"onCreate");

    worker = new CCounterWorker();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId)
  {
    CMessage.debug(getClass(),"onStartCommand");

    new Thread(worker).start();

    return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent)
  {
    return null;
  }

  @Override
  public void onDestroy()
  {
    CMessage.debug(getClass(),"onDestroy");

    worker.stop = true;
  }

  //-----------------------------------

  public class CCounterWorker implements Runnable
  {
    public boolean stop;

    @Override
    public void run()
    {
      int i = 0;
      while (!stop) {
        i++;
        CMessage.debug(getClass(),"count " + i);
        CCommon.sleep(1000);
      }
    }
  }
}

Initializing the service

startService(new Intent("contexto",CServiceMain.class)); 

Logcat

>>>> Serviço iniciado a partir da activity 

            2018-12-06 17:08:25.902 14321-14321/com.neoporto.elegal CServiceMain: onCreate 
            2018-12-06 17:08:25.904 14321-14321/com.neoporto.elegal CServiceMain: onStartCommand
            2018-12-06 17:08:25.906 14321-14533/com.neoporto.elegal CCounterWorker: count 1
            2018-12-06 17:08:26.906 14321-14533/com.neoporto.elegal CCounterWorker: count 2
            2018-12-06 17:08:27.907 14321-14533/com.neoporto.elegal CCounterWorker: count 3
            2018-12-06 17:08:28.908 14321-14533/com.neoporto.elegal CCounterWorker: count 4
            2018-12-06 17:08:29.909 14321-14533/com.neoporto.elegal CCounterWorker: count 5

>>>> Aqui a activity é encerrada e o serviço tenta restartar

            2018-12-06 17:08:33.582 14546-14546/com.neoporto.elegal CServiceMain: onCreate 
            2018-12-06 17:08:33.583 14546-14546/com.neoporto.elegal CServiceMain: onDestroy
    
asked by anonymous 06.12.2018 / 20:23

1 answer

1

This has to do with the restrictions that Android 8 imposes, among others, on the use of services, when the application is in background .

Restrictions aim to decrease battery consumption and improve overall device performance.

These restrictions, by default, only apply to applications with android:targetSdkVersion=26 or higher. However, you can enable these restrictions for any application on the device's Settings screen.

The approach to migrating to Android 8 depends on the application and purpose of the service.

For more information consult the documentation:

06.12.2018 / 22:19