如何在不同的过程中运行音乐服务

时间:2016-10-22 10:19:51

标签: android

我写了一个流媒体音乐应用程序,我工作正常,直到我尝试在不同的过程中运行音乐服务。在Android Developer的service之后,我已经在Manifest中添加了Service的过程,如下所示:

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

然后在MainActivity中我调用启动服务:

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

bindService将调用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;
        }
    };

然后该应用程序在此行之后停止工作:

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

应用程序显示消息“不幸的是,Mx Dance Music已停止”,堆栈跟踪:“致命异常:主java.lang.ClassCastException:android.os.BinderProxy无法转换为com.xiKy.mxMusic.MyPlayer $ MusicBinder at com.xiKy.mxMusic.MainActivity $ 3.onServiceConnected(MainActivity.java:356)at android.app.LoadedApk $ ServiceDispatcher.doConnected(LoadedApk.java:1097)                                                                     在android.app.LoadedApk $ ServiceDispatcher $ RunConnection.run(LoadedApk.java:1114)                                                                 在android.os.Handler.handleCallback(Handler.java:615)                                                                     在android.os.Handler.dispatchMessage(Handler.java:92)                                                                     在android.os.Looper.loop(Looper.java:137)                                                                     在android.app.ActivityThread.main(ActivityThread.java:4823)                                                                     at java.lang.reflect.Method.invokeNative(Native Method)                                                                     在java.lang.reflect.Method.invoke(Method.java:511)                                                                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)                                                                     at dalvik.system.NativeStart.main(Native Method)     ClassLoader.loadClass:

Thread.getContextClassLoader()返回的类加载器可能会因承载多个应用程序的进程而失败。您应该显式指定上下文类加载器。例如:Thread.setContextClassLoader(getClass()。getClassLoader());

然后如果我删除该行:

bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);

应用程序没有崩溃。如何解决这个错误?我很感激所有人的帮助。

1 个答案:

答案 0 :(得分:1)

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

这仅适用于本地绑定服务。切换到远程服务后:

  • 您需要在AIDL
  • 中定义API
  • 您的服务需要实现其Binder作为从该AIDL生成的.Stub类的子类
  • 客户端需要使用asInterface()IBinder转换为从该AIDL生成的客户端代理

所有这些都包含在the documentation on AIDL中。