In android 8 Service stops and does not reboot when Activity ends

Asked

Viewed 74 times

1

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

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

I am not able to identify if the problem is in the execution from Androidstudio (debug), android 8, or device.

Also I cannot verify if there has been any exception in the execution service because the app instance has been 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);
      }
    }
  }
}

Service startup

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

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, are only applied to applications with android:targetSdkVersion=26 or higher. However, the user can activate these restrictions for any application on the device’s "Settings" screen.

The approach to apply for migration to Android 8 depends on the application and purpose of the service.

Further information can be found in the documentation:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.