IBinder casting *sometimes* results in ClassCastEx

2019-05-20 07:31发布

问题:

We bind to our local service (same process) in Application subclass but sometimes we are getting a ClassCastException. I have no idea how to fix it since everyone says the exception occurs if the service process is different than your application. How can this be fixed?

Application subclass:

public class App extends Application implements ServiceConnection {
  private ServiceApi exposedServiceApi;
  private EventBus eventBus;

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

      public void bindToService() {
        if (isConnectedToService()) {
          return;
        }

        boolean successfulBindCall =
            bindService(new Intent(this, MainService.class), this, BIND_IMPORTANT | BIND_AUTO_CREATE);
        if (successfulBindCall) {
          Timber.d("bindService() returned true --> onServiceConnected() will be called soon.");
        } else {
          throw new RuntimeException("bindService() returned false, this is an unrecoverable state.");
        }

  }



      @Override public void onServiceConnected(ComponentName componentName, IBinder service) {
        Timber.d("App has connected to service %s", componentName.toShortString());
        if (componentName.getClassName().equals(MainService.class.getName())) {
          try {
            exposedServiceApi = (ServiceApi) service;
            eventBus.post(new MainServiceConnectedEvent());

          } catch (ClassCastException e) {
            Timber.e(e, "Could not cast IBinder to ServiceApi!");
            exposedServiceApi = null;
          }
        }
      }

      @Override public void onServiceDisconnected(ComponentName componentName) {
        Timber.d("App has disconnected from service %s", componentName.toShortString());
        if (componentName.getClassName().equals(MainService.class.getName())) {
          exposedServiceApi = null;
          eventBus.post(new MainServiceDisconnectedEvent());
        }
      }
}

Service.onBind():

  @Override public IBinder onBind(Intent intent) {
    return new ServiceBinder();
  }

ServiceBinder: (inner class of MainService)

  private class ServiceBinder extends Binder implements ServiceApi {
     // implementation of ServiceApi
  }

Manifest:

<service
android:name=".service.MainService"
android:exported="false"
android:icon="@drawable/ic_launcher"
android:stopWithTask="true"/>

Error:

java.lang.ClassCastException: android.os.BinderProxy cannot be cast to ee.mtakso.driver.service.ServiceApi at ee.mtakso.App.onServiceConnected(App.java:152) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1139) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1156) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5341) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) at dalvik.system.NativeStart.main(NativeStart.java)