I am not certain how to resolve this, but in one Activity I call startService, and then immediately call to start the next Activity.
This works, the service starts, and begins to process the data as expected.
I go to the next Activity, and in onResume I call the AsyncTask to bind the service.
So, the basic flow is that I call the AsyncTask, the bindService returns false, so mConnection is never called.
So, the problem is why is bindService returning false?
The reason I put the binding in an AsyncTask is that I had the thread sleep for 10 seconds before binding to see if the service needed to start up first.
I also started the service inside this activity, first, in the onCreate method, and so waited 10 seconds, but bindService still returns false.
private class BindServiceTask extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
return bindService(
new Intent(IMyCallback.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
}
protected void onPostExecute(Boolean b) {
if (b) {
Log.i(TAG, "onResume - binding succeeded");
Toast.makeText(mContext, "Bound to service succeeded",
Toast.LENGTH_LONG).show();
} else {
Log.i(TAG, "onResume - binding failed");
Toast.makeText(mContext, "Bound to service failed",
Toast.LENGTH_LONG).show();
}
}
}
private IMyCallback mCallback = new IMyCallback.Stub() {
@Override
public void dataChanged(double[] info) throws RemoteException {
mHandler.sendMessage(mHandler.obtainMessage(LOCATION_MSG, info));
}
};
IMyService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.i(TAG, "onServiceConnected");
mIRemoteService = IMyService.Stub.asInterface(service);
try {
Log.i(TAG, "registering callback");
mIRemoteService.registerCallback(mCallback);
} catch (RemoteException e) {
Log.e(TAG, e.toString());
}
}
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "Service has unexpectedly disconnected");
mIRemoteService = null;
}
};
When you call
bindService()it won’t necessarily return your service connection or API to access the service. It happens asynchronously. You need a callback like this:Your call to do the bind should look like this:
Make sure in your service you are implementing the API Stub and returning an instance of it in your
onBind()method:Lastly, to complete the example, the AIDL file for my example looks like this: