0

I've followed steps made by radiolondra57 from Intent Service not working in doze mode to create service running on separate process. I've got a problem that when after startService the onCreate of this service is never called. This is my setup:

Manifest:

    <service
        android:name=".service.Smart113MainService"
        android:enabled="true"
        android:exported="true"
        android:process=":antiDozeProcessName"
        android:label="LocationService">
    </service>

In the onCreate of the first Activity this method is called : (yes, the version of the test phone is equal to "O" :) )

 private void startServices() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startService(new Intent(this, Smart113MainService.class));
    } 
 }

Service:

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(Smart113NotificationFactory.locationSharingNotificationId, notification.logIntoAppNotification(this).build());
    doingMoreStuff();
    return START_STICKY;
}

Is this a problem with me not setting action to the Intent that is being send ? That is actually the only difference i can see in our codes..

Karzel
  • 928
  • 9
  • 24

1 Answers1

0
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  startService(new Intent(this, Smart113MainService.class));
}
else {
  startForegroundService(new Intent(this, Smart113MainService.class));
}

Move startForeground code to onCreate()

@Override
public void onCreate() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(NOTIFICATION_ID, notification);
    }
    ...
}

to remove notification onDestroy()

@Override
public void onDestroy() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          stopForeground(true); //true will remove notification
    }
    ...
}

Update

You are not getting debug point in different process because you are selecting main process from attach debugger. Select right process to debug.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • I've placed breakpoints on my startService (after checking OS) its being called. I've placed breakpoints on both onCreate and onStartCommand's first lines of Services. Non of them is ever called. – Karzel Aug 10 '18 at 06:16
  • If you do `startService()`, It automatically trigger `onCreate()` & `onStartCommand()`. If it does not, try removing `enabled` and `exported` – Khemraj Sharma Aug 10 '18 at 06:30
  • Tried that, debugger still stops on breakpoint on `startService()` and does not on the other breakpoints inside Service. – Karzel Aug 10 '18 at 06:56
  • Okay, and your service runs without specifying `:process`? – Khemraj Sharma Aug 10 '18 at 06:58
  • Just checked, it does. – Karzel Aug 10 '18 at 07:00
  • Weird behaviour!, Can you make a last try with `enabled true` & with `android:process=":antiDozeProcessName"`. – Khemraj Sharma Aug 10 '18 at 07:06
  • I was also wondering why he starts the service with `startService()` and not `startForegroundService()`. I've changed that. Then I've noticed that i had startForeground() in both `onCreate()` and `onStartCommand()` already. But anyway, the strangest things is that application is showing the Notification. That means that this `startForeground()` function has to run ! But it doesn't stop on it with breakpoint. What is going on. – Karzel Aug 10 '18 at 07:36
  • So finally you got your service running. Did latest edited answer helped? – Khemraj Sharma Aug 10 '18 at 07:43
  • I guess so, thanks. But i still don't know why breakpoints are not triggering inside the Service. – Karzel Aug 10 '18 at 07:46
  • You are right !! How could I not think about it! Thank you so much ! Now I'm wondering if my initial version actually worked, and adding simple `Log()` would show me that everything is just fine. Thanks again! – Karzel Aug 10 '18 at 08:03
  • Yes, that's why I first said you to add log() – Khemraj Sharma Aug 10 '18 at 08:06