23

I have a problem with using a background service.
I am using the service from 2 activities.

The first activity starts the Service with startService(intent) and the binds to it with bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

This works perfectly and I can send a Message in onServiceConnected().

The second activity only binds to the Service (since it's already started), again using bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

Now here is my problem:
In the second activity I have a Button which should use the service when I press it.
However, the onServiceConnected() is never called in this activity, so I can not get the binder to create a Messenger to send a Message to the server.

Does anyone know when onServiceConnected() is called exactly, or what it is possibly waiting for?
Or maybe something else to fix this problem?

EDIT: bindService returns false, what could cause this?

Thanks in advance

Leon van Noord
  • 868
  • 1
  • 7
  • 24
  • `bindService` should return `false` if you want `onRebind` to be called after the first binding. – omerfarukdogan Jun 18 '15 at 11:54
  • Possible duplicate of [onServiceConnected never called after bindService method](https://stackoverflow.com/questions/2486692/onserviceconnected-never-called-after-bindservice-method) – TT-- Jul 12 '19 at 22:42

8 Answers8

37

You need to add:

<service android:name="package.path.to.your.service.class" android:enabled="true"></service>

in the Android manifest file.

Erdinc Ay
  • 3,224
  • 4
  • 27
  • 42
30

After some more research, I discovered this is a known issue in Android.

The second activity I was talking about was an activity which is used as content within a TabActivity.

The way to fix this was to call bindService(...) on the application context, instead of on the activity context using getApplicationContext().bindService(...)

Leon van Noord
  • 868
  • 1
  • 7
  • 24
  • 2
    Hi there, I have the same problem. Could you explain the remedy a little bit more? If possible could you look at my code here and tell me what should be changed? Thank you so much. This error is driving me insane for a day. My code: http://ideone.com/cV5BJa – Max Mar 16 '13 at 22:28
  • Could you explain me why we need using getApplicationContext().bindService not bindService? bindService works in my one activity but not working in my another activity (getApplicationContext().bindService works). It's very strange. – Thinsky May 21 '15 at 02:23
  • Thank you so much!! This is exactly what I was facing and it makes a lot of sense to use application context rather than activity context. It solved my issue! Thanks man!! – Sjd Jun 21 '17 at 12:24
9

Add bind in manifest..

 <service
            android:name=".MyAccessibilityService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

</service>

MyAccessibilityService your service name.

David Hackro
  • 3,652
  • 6
  • 41
  • 61
febaisi
  • 644
  • 6
  • 15
8

It happened to me just now.
Don't forget to instantiate your binder class before the onbind return. Silly mistake, but it happens. hopefully this will help someone in the future.

private YourBinder thisBinder;

oncreate
{
  thisBinder = new YourBinder(); //don't forget this.
}


public class YourBinder extends Binder
{

}

public IBinder onBind(Intent intent) 
{
  return thisBinder;
}
Thelouras
  • 852
  • 1
  • 10
  • 30
hepizoj
  • 243
  • 4
  • 9
3

This is what worked for me

Instead of

public IBinder onBind(Intent intent) {
    return null;
}

I used

public IBinder onBind(Intent intent) {
    return mMessenger.getBinder();
}
Barrie Galitzky
  • 1,166
  • 12
  • 14
0

I was xmarian and had the same problem and later found that i have not given proper name to that service . So the service name should be given name attribute. Which I suppose the xmarian compiler does the same thing make that entry in Android XML.

0

In my case, it was because I had forgotten to instantiate the binder inside the service and so onBind returned null. I had this:

    private ServiceBinder mServiceBinder;

instead of this:

    private ServiceBinder mServiceBinder = new ServiceBinder();
0
<service
            android:name="com.app.triad.tseacro.services.LocationUpdatesService"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
            android:foregroundServiceType="location"
            >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
    </service>
sarjeet singh
  • 461
  • 4
  • 10