onServiceConnected() not called

余生颓废 提交于 2019-12-08 23:25:57

问题


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


回答1:


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(...)




回答2:


You need to add:

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

in the Android manifest file.




回答3:


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.




回答4:


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;
}



回答5:


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();
}



回答6:


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.




回答7:


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();


来源:https://stackoverflow.com/questions/7875131/onserviceconnected-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!