2

I have 2 separate Apps A and B.

Application B has a service inside it and at one point, Application A communicates with App B by binding to B's service and exchanging some messages (using a Messenger).

On almost all devices, this process works completely fine. However, on a OnePlus 5 device, whenever A tries to bind the B's service, it always fails unless B has been opened and is sitting in the background. And if you swipe B from the task drawer (killing it), the service binding fails again.

When I say service binding fails, I mean context.bindService() returns false. And so far, I've only noticed this behaviour on a OnePlus 5 device.

Does this indicate something wrong on the OnePlus 5 device alone? Or is there something else that could possibly be causing this issue.

If it helps, this is how I bind to the service:

Intent intent = new Intent();
intent.setComponent(new ComponentName(packageNameOfB, classNameOfBService));
MyServiceConnection connection = new MyServiceConnection();
context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
George V.M.
  • 757
  • 7
  • 16
  • also discussed earlier. https://github.com/cafebazaar/TrivialDrive/issues/13 – Qandil Tariq Jul 20 '18 at 10:46
  • https://stackoverflow.com/questions/10591136/onserviceconnected-sometimes-not-called-after-bindservice-on-some-devices – Qandil Tariq Jul 20 '18 at 10:49
  • @QandilTariq The github issue seems similar to mine. Sucks that there's no answer to why it happens. However, the SO question you linked seems different from my case. In that person's question they mention that service doesn't bind even though `bindService` returns `true`. But in my case, `bindService` always returns `false` on this device. – George V.M. Jul 20 '18 at 12:29

1 Answers1

2

This behavior is caused by a Battery optimization feature in OnePlus devices. This can be solved by disabling this feature. The feature is present in "Settings -> Battery -> Battery Optimization -> Advanced Optimization in overflow menu (three dots menu) -> Disable Advanced Optimization". This will solve the issue.

MediumOne
  • 804
  • 3
  • 11
  • 28