0

I write a Streaming Music app, I work ok until I'm try to run the music Service in the different process. following the service of Android Developer, I've added the process of Service in the Manifest like here:

<service android:name=".MyPlayer"
            android:process=":myPlayer"
            android:enabled="true"/>

Then in the MainActivity I call the start Service:

if(_playIntent == null) {
            _playIntent = new Intent(getApplicationContext(), MyPlayer.class);
            startService(_playIntent);
            bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        }

The bindService will call to ServiceConnection:

MyPlayer _player;
private ServiceConnection musicConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service; 
            _player = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isConnection = false;
        }
    };

Then the app stop working after this line:

MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service;

The app show the message "Unfortunately, Mx Dance Music has stopped", Stack trace: "FATAL EXCEPTION: main java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.xiKy.mxMusic.MyPlayer$MusicBinder at com.xiKy.mxMusic.MainActivity$3.onServiceConnected(MainActivity.java:356) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1097) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1114) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4823) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method) ClassLoader.loadClass:

The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());

And then if I remove the line:

bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);

The application isn't crash. How to fix this bug ? I'm appreciate any help from everybody.

Xiky
  • 17
  • 4
  • "Find in the logcat I see the error" -- I am not aware that this is an error. "Then the app stop working after this line" -- please explain what "stop working" means. If your app is crashing, there will be a Java stack trace in LogCat: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand the stack trace, edit your question and post the entire stack trace. – CommonsWare Oct 22 '16 at 11:10
  • Edited, thank you alot CommonsWare – Xiky Oct 22 '16 at 11:27

1 Answers1

1
MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service;

This works for local bound services only. Once you switch to a remote service:

  • You need to define your API in AIDL
  • Your service needs to implement its Binder as a subclass of the .Stub class generated from that AIDL
  • The client needs to use asInterface() to convert the IBinder into a client-side proxy generated from that AIDL

All of this is covered in the documentation on AIDL.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491