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