1

I am trying to bind to a service, but the bind service method always returns false. I believe the problem is in my connection. When I import the location of the LocalBinder class (in the service) I dont get any compile errors but the bindService call is still false.

Service information:

//binder for client
private final IBinder mBinder = new LocalBinder();

//client class binder
public class LocalBinder extends Binder {
    ServiceA getService() {
        // Return this instance of LocalService so clients can call public methods
        return ServiceA.this;
        }
}

@Override
public IBinder onBind(Intent arg0) {
    return mBinder;
}

//called when service is created
public void onCreate(){
    super.onCreate();
    Toast.makeText(this,"ServiceA is started",Toast.LENGTH_SHORT).show();
}

Connection:

 private ServiceConnection connect2A = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder newBinder = (LocalBinder) service;
        mainService = newBinder.getService();
        bound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        bound = false;
    }
};

Initial calls:

    @Override
protected void onStart(){
    super.onStart();
    Intent intent = new Intent(this, ServiceA.class);
    startService(intent);
    boolean check = bindService(intent,connect2A,0);
    Toast.makeText(this, "Return from check is: " + check, Toast.LENGTH_LONG).show();
    //program does get to this point, but binding fails
}

the AndroidManifest.XML file:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ServiceTest2Activity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<service android:enabled="true" android:name=".serviceA" />
</application>

user966005
  • 13
  • 4
  • Do you have the Service declared in your AndroidManifest.xml? – dymmeh Oct 24 '11 at 18:51
  • Yes, it is set up as: – user966005 Oct 24 '11 at 18:58
  • Why are you calling startService prior to calling bindService on the same intent? This seems redundant. Have you tried eliminating this call? – dymmeh Oct 24 '11 at 19:03
  • Yes, the startService call STILL returns FALSE – user966005 Oct 24 '11 at 19:07
  • I assume the import com.ajc2990.ServiceA.LocalBinder; is required as the LocalBinder is in the Service class. When this line is removed, Eclipse gives an error. – user966005 Oct 24 '11 at 19:12
  • Yes, you need that. Have you tried debugging to see if onBind(Intent arg0) is even getting called? – dymmeh Oct 24 '11 at 19:20
  • Got it, the XML file had a discrepancy in the name of the service. I assume I need to add the same line for each additional service I add? – user966005 Oct 24 '11 at 19:24
  • Yes. For each service you have you must declare it in the manifest. I suggest up-voting @DanJ's comment on the other answer since he was correct in assuming that the spelling of your service name was wrong. – dymmeh Oct 24 '11 at 19:27
  • Check the context which you are using to bind. See http://stackoverflow.com/questions/2914921/android-context-bindservice-always-returns-false-and-serviceconnection-object-is – codinguser Apr 12 '12 at 07:15

1 Answers1

0

Some ideas:

-What class is the onStart() method in? I bind to my services in onCreate().

-Do you perhaps need to pass Context.BIND_AUTO_CREATE to the bindService call? e.g.

boolean check = bindService(intent, connect2A, Context.BIND_AUTO_CREATE);

-As the other commenter suggests, you should post the relevant snippets from your AndroidManifest.xml.

-You should put some trace/toast in the onServiceConnected method so you know if it is getting called.

Dan J
  • 25,433
  • 17
  • 100
  • 173