IBinder casting *sometimes* results in ClassCastException when binding to service

狂风中的少年 提交于 2019-12-08 03:47:33

问题


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)

来源:https://stackoverflow.com/questions/31181164/ibinder-casting-sometimes-results-in-classcastexception-when-binding-to-servic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!