Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ClassCast exception when binding to service

Ok, I'm new to android development and am trying to bind to a service so that I can call methods on the service once it's been started. The Activity and Service described below are both part of the same application so there shouldn't be any problems there, but everytime I run my app I get the following error:

java.lang.ClassCastException: android.os.BinderProxy

The line this happens on is:

LocalBinder binder = (LocalBinder) service;

My Activity code (simplified is):

public class Main extends Activity {

    boolean gpsBound = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /** Called whenever the activity is started. */
    @Override
    protected void onStart() {
        super.onStart();
        // Bind to GPSService
        Intent i = new Intent(this, GPSService.class);
    startService(i);
    bindService(i, connection, Context.BIND_AUTO_CREATE);
    }

    /** service binding */
    private ServiceConnection connection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
            // After binding to GPSService get the instance of it returned by IBinder
        LocalBinder binder = (LocalBinder) service;
            gpsBound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
            gpsBound = false;
        }
    };

}

Service:

public class GPSService extends Service {

    @Override
    public void onCreate() {
            super.onCreate();
    }

    @Override
    public IBinder onBind(Intent i) {
    // TODO Auto-generated method stub
    return new LocalBinder<GPSService>(this);
    }


   /**
    * Our implementation of LocationListener that handles updates given to us
    * by the LocationManager.
    */
    public class CustomLocationListener implements LocationListener {

        DBHelper db;

        CustomLocationListener() {
            super();
        }

    // Overridden methods here...

    }

}

And finally my LocalBinder:

/**
 * A generic implementation of Binder to be used for local services
 * @author Geoff Bruckner  12th December 2009
 *
 * @param <S> The type of the service being bound
 */

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


    public LocalBinder(S service){
        mService = new WeakReference<S>(service);
    }


    public S getService() {
        return mService.get();
    }
}

I understand the meaning of the ClassCast Exception but cannot understand what to do! I've followed the example in the google documentation but it's still not working. Can anyone shed any light on what might be causing this?

Thanks in advance!

like image 777
James Herrington Avatar asked Nov 07 '11 17:11

James Herrington


2 Answers

Delete attribute process in your AndroidManifest.xml of your service.

like image 164
Tony Avatar answered Sep 25 '22 20:09

Tony


Had same error. I had added the android:process=":process_description" attribute in the manifest. When you add it, your service is created as separate process and hence you get instance of binderProxy (Hence the class cast exception)

like image 25
MirageProg. Avatar answered Sep 24 '22 20:09

MirageProg.