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.