Search code examples
androidserviceconnectionbindserviceonserviceconnected

Android: onServiceConnected not called after bindService


None of the many similar questions I have found have helped to solve my issue.

Here are some questions I've looked at:

Here is part of my android app code:

    //Local service interface
private INetworkQueryService myNetworkQueryService = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    Intent intent = new Intent(this, NetworkQueryService.class);
    startService (intent);
    bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
                            Context.BIND_AUTO_CREATE);
}

//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
    /** Handle the task of binding the local object to the service */
    public void onServiceConnected(ComponentName className, IBinder service) {
        myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
        // as soon as it is bound, run a query.
        loadNetworksList();
    }

    /** Handle the task of cleaning up the local binding */
    public void onServiceDisconnected(ComponentName className) {
        myNetworkQueryService = null;
    }

    public void loadNetworksList() {
        // delegate query request to the service.
        try {
            myNetworkQueryService.startNetworkQuery(myCallback);
        } catch (RemoteException e) {
        }
    }

};  

/**
 * This implementation of INetworkQueryServiceCallback is used to receive
 * callback notifications from the network query service.
 */
private final INetworkQueryServiceCallback myCallback = new INetworkQueryServiceCallback.Stub() {

    @Override
    public void onQueryComplete(List<NetworkInfo> networkInfoArray,
            int status) throws RemoteException {
        displayNetworks(networkInfoArray, status);
    }

    /** place the message on the looper queue upon query completion. */

};

As you can sy, myNetworkQueryServiceConnection should be called from within the bindService method (located in onCreate()). This (in theory) should also run onServiceConnected (I think), but I put a breakpoint in my code and it never stops inside that method. I did some debugging and bindService returns false, so for some reason it's not successfully binding it.

This is my first time ever having to bind services in Android, so I'm sure I've missed something. I'm hoping someone could point out a mistake.

Let me know if you're missing any information--I'm not sure what all you might need, and I really can't post my entire app code here.

EDIT: I keep reading things about registering the service in the manifest but cannot seem to figure out where or how that would be done? Here's the manifest for this project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I certainly don't see anything about the service, but I guess I don't know how to add it.


Solution

  • You need to add a <service> element, as a peer of your <activity> element, as a child of <application>:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tek15.cellemrmonitor"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="21" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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:name="NetworkQueryService" />
    </application>
    

    My example element assumes that NetworkQueryService is really com.tek15.cellemrmonitor.NetworkQueryService — otherwise, substitute in the proper fully-qualified class name.