-3

I am facing a weird issue .On launch of my app I am connecting Activity to service.Service connection is getting established successfully and works fine in Samsung galaxy S5 and other devices too.But in other devices like Moto E , Moto G etc it works only 2 or 3 time after 3 or 4 launch it dose not works at all. communication between service and Activity is achieved using massenger. I observed that when my app stops working onServiceConnected is not called but even in this case bindservice returns true. Similar issue Is mentioned here but no answer

This is how I bind the service to an Activity

    startService(new Intent(this,MyService.class));
    boolean bound = bindService(new Intent(this,MyService.class), serviceConnection, Context.BIND_AUTO_CREATE);//bound is true everytime 

Below is onBind Of MyService

public IBinder onBind(Intent intent) {

    return messenger.getBinder();
}

Here messenger is an instance of Messenger ,to communicate between activity and service.

Onik
  • 19,396
  • 14
  • 68
  • 91
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64

1 Answers1

1

You should do unbind when your activity is paused or destroyed. Take a look here.

Edit: If you want your service to continue running, start it with startService and make it a foreground one. Than you can bind and unbind your activity when resuming and pausing. I used this approach when was doing my thesis that was a media player for android :)

Kiril Aleksandrov
  • 2,601
  • 20
  • 27