onServiceConnected never called after bindService method

后端 未结 12 624
醉话见心
醉话见心 2020-11-30 05:48

I have a particular situation: a service started by a broadcast receiver starts an activity. I want to make it possible for this activity to communicate back to the service.

相关标签:
12条回答
  • 2020-11-30 06:19

    There can be another explanation, not a very frequent situation, but I managed to get into it and lose 2 hours to solve it.

    So what I did was that I wanted to reuse the bundle from the intent received by the onBind method. But I went for the lazy version and changed the class of the intent. Apparently this causes the onServiceConnected not to get called. Probably because the system keeps a reference to the intent and uses it when calling onServiceConnected.

    In conclusion don't change the intent you receive in onBind.

    0 讨论(0)
  • 2020-11-30 06:21

    After hours and hours of trying to figure this out, the issue is that the examples that show the creation of the service, don't include the onBind method or they have the following sample code or it generates this for you:

    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    

    This causes the onServiceConnected method to fail or never actually get executed. The fix is VERY simple, which is the following:

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

    Where you could create a simple binder such as the following to return:

    private final IBinder mBinder = new LocalBinder();
    public class LocalBinder extends Binder {
        public ConferenceService getService() {
        return ConferenceService.this;
      }
    }
    
    0 讨论(0)
  • 2020-11-30 06:22

    I just spent a lot of time on this one, and since in my case it was neither of the above answers I'll just lay out another possible scenario:

    This happened to me when I was using a kotlin.coroutines.Deferred object to represent my bound service, and I blocked the thread that onServiceConnected should be called on when I called await() on this object.

    So basically - don't block the main thread waiting for onServiceConnected to be called, or it won't be able to be called...

    0 讨论(0)
  • 2020-11-30 06:26

    I've just experienced another version of this problem, with the same symptom of onServiceConnected(...) not being called. The cause was different in my case.

    You must make sure to have a service declaration in your AndroidManifest.xml within the application tag - this was the root of the problem for me.

    <application android:name=".YourAppTitle" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main" android:label="@string/app_name">
        </activity>
        <service android:name="YourService" />
    </application>
    

    There's an extra complication if you're using a separate Android library within Eclipse - adding this Service tag only seems to fix the issue if the referenced service is in the same package as the manifest; i.e. if your app is in package a.b.c and this is where AndroidManifest.xml resides, then 'YourService' must also be in package a.b.c. (manually copied from another library, if necessary) or else the <service..> tag may/will be ignored and onServiceConnected(...) still won't be called.

    This was the case for my project even though I used a suitable import statement for the Service in my code. Eclipse showed no error, so the import was correctly identifying the class from another library in the Eclipse workspace.

    HTH

    0 讨论(0)
  • 2020-11-30 06:27

    The onServiceConnected event was never being called in my application.

    The problem was that I had the service name defined in my Application Manifest as:

    <service android:name="MyService" android:enabled="true"></service>
    

    Once I changed it to the full class name it worked:

    <service android:name="com.example.MyService" android:enabled="true"></service>
    

    Update: You can also have use a relative class name:

    <service android:name=".MyService" android:enabled="true"></service>
    

    Specify the class name using its full com.example.MyServiceClass instead of just MyServiceClass.

    0 讨论(0)
  • 2020-11-30 06:28

    If someone's still looking for the answer after trying all others then this might help.. onServiceConnected() is not called immediately after bindService(...). it takes few seconds. So if you are receiving some value from the onServiceConnected() and you have to use it somewhere else in onCreate or onStart it will throw a NullPointerException because of the delay. So if you can shift that line of code which needs the value from onCreate or onStart, inside onServiceConnected(){......}, below the line of declaration of the value that you need then it might work.

    0 讨论(0)
提交回复
热议问题