androidandroid-activityandroid-serviceandroid-service-binding

Long amount of time elapses before onServiceConnected() is called


I have an activity and a service running in different threads. It is a sound recorder for a night time.

The problem comes when the service crashes for any reason (we can't avoid that crash, as it comes from a external library) in the middle of the recording.

What I do, as a workaround, is to restart the activity when the Activity notices that the connection with the service has been lost. By recreating the activity, the service is recreated too.

The problem is that, it takes a lot of time (even 1 hour) between the call to

    bindService(RecorderService.newIntent(this), this, BIND_AUTO_CREATE);

and the callback

public void onServiceConnected(ComponentName name, IBinder service) {

I think this happens because the phone is in a kind of low-battery mode, as it is not used by the user in the middle of the night.

I call startService once the connection is done, as it is needed that the Activity and the Service is connected.

So the problem is that the record stops for that time, and I would need it to be running without interruptions. I could call startService once the activity is restating, but that would not ensure that the service would be connected at the time of another hypotethical crash.

Any idea about how to make the connction/start of the service faster?

Thanks in advance!


Solution

  • If the problem is only the "low power mode" during the night I think you can aquire a WakeLock (https://developer.android.com/reference/android/os/PowerManager.WakeLock) to wake-up the CPU just before starting the Service or Activity. Don't forget to release the Lock when you've done ;)