Essentially my problem is this, I open my Activity. It binds to a
local service that "appears" to be connected. I get a force close when
I press the back button

"IllegalArgumentException: Service not registered" when unbind is
called in my onPause method...

public void onPause() {
                super.onPause();
                if (isFinishing()) {
                        if (mConnection != null){
                        Log.i(TAG,"onPause, mConnection" + 
mConnection.toString());
                        mDbS.unbindService(mConnection);
                        }else {
                                Log.i(TAG,"mConnection is null");
                        }
The service is bound in onCreate

// bind to our sdCard database using our Service Connection
                mServiceIntent = new Intent(this,DatabaseService.class);
                if(bindService(mServiceIntent, mConnection,
Context.BIND_AUTO_CREATE)){
                        Log.e(TAG, "service bound");
                }else{
                        Log.e(TAG, "service not bound");
                }

bindService is returning true.

My Service connection is as follows. onServiceConnected is called and
goes to completion. onService Disconnected is never called (presumably
because my service is in same process)

private ServiceConnection mConnection = new ServiceConnection() {
                public final String TAG = "LoadView.ServiceConnection"; //$NON-
NLS-1$

                public void onServiceConnected(ComponentName className, IBinder
service) {
                        mDbS = ((LocalBinder<DatabaseService>) 
service).getService();
                  .....
                        stuff not relevant
                   ....
                        Log.i(TAG, "finished onServiceConnected"); //$NON-NLS-1$
                }
                public void onServiceDisconnected(ComponentName className) {
                        Log.e(TAG, "onServiceDisconnectedCalled"); //$NON-NLS-1$
                }
        };

The only thing I do a little different is that I don't use an inner
class in my service for the Binder object - I use a separate class.
This is mostly because I have several databases and wanted code I
could reuse and set up simply rather than worry about all the ins and
outs and memory leak issues.

import android.os.Binder;
import android.util.Log;

public class LocalBinder<S> extends Binder {
        private String TAG = "LocalBinder";
        private  S mService;

        @SuppressWarnings("unused")
        private LocalBinder() {
        }

        public LocalBinder(S service){
                mService = service;
        }

        public S getService() {
                return mService;
        }

        public void finalize(){
                mService = null;
                try {
                        //TODO reinstate this after Issue 8046 is resolved
                        //super.finalize();
                } catch (Throwable e) {
                        Log.e(TAG,"Unable to finalize Binder");
                }

        }
}

Any ideas?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to