ClassCast class exception when binding to a service

Ok, I'm new to Android development and trying to bind to a service so that I can call methods on the service after it starts. The actions and services described below are part of the same application, so there should not be any problems, but every time I run my application, I get the following error:

java.lang.ClassCastException: android.os.BinderProxy

The line in which this occurs is as follows:

LocalBinder binder = (LocalBinder) service; 

My opcode (simplified):

 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; } }; } 

Services:

 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 I can’t figure out what to do! I followed this example in the google documentation, but it still doesn't work. Can anyone shed light on what might be causing this?

Thanks in advance!

+6
source share
4 answers

LocalBinder passed to onServiceConnected has a generic type argument, while your local LocalBinder binder variable LocalBinder binder not have one.

Allow this anyway, either by removing the generic type from the LocalBinder definition, or by adding it to the declaration of your local variable in onServiceConnected

 class MyBoundService extends Service{ private final IBinder mBinder = new MyBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } public class MyBinder extends Binder{ public void doStuff(){ //Stuff } //More Binder Methods } } class MyActivity extends Activity{ private MyBinder mBinder; @Override protected void onStart(){ Intent intent = new Intent(this, MyBoundService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onStop(){ unbindService(mConnection); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { mBinder = (TaskBinder) service; mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }; private void doStuff(){ if (mBound) mBinder.doStuff(); } } 

No real need to mess around with weak recommendations and something else. just be sure to untie (I'm not in the sample)

If you want to call ASAP service methods, just put the calls in onServiceConnected after installing mBinder. otherwise, it is simply called from other callbacks (onClick events and much more).

+3
source

Remove the process attribute in your AndroidManifest.xml of your service.

+32
source

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

+4
source

If you are trying to bind to a local service than to yes, you can just drop it. However, if you are trying to bind to a remote (separate process) service, you must use the AIDL method, as described in this article.

http://developer.android.com/guide/components/aidl.html

+3
source

Source: https://habr.com/ru/post/900957/


All Articles