我想在框架级别使用 MediaPlayback 服务(在音乐应用程序中实现),步骤是:
将这些代码添加到基于 /frameworks/base/core/java/com/android 的 java 文件中(也在同一目录中添加 IMediaPlaybackService.aidl):
private IMediaPlaybackService mMediaPlaybackService = null;
private boolean isServiceConn = false;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mMediaPlaybackService = IMediaPlaybackService.Stub.asInterface(service);
isServiceConn = true;
}
public void onServiceDisconnected(ComponentName className) {
}
};
public void funcXXX() {
...
if(isServiceConn == false) {
mContext.bindService(new intent("com.android.music.MediaPlaybackService"), mConnection, Context.BIND_AUTO_CREATE);
}
...
}
public void onClick(View v) {
try {
mMediaPlaybackService.prev();// play previous track
} catch(RemoteException ex) {
}
}
服务绑定成功并且mMediaPlaybackService的值被更正了,但是当我点击一个按钮并执行语句“mMediaPlaybackService.prev()”时,程序会崩溃,logcat转储是:
E/AndroidRuntime( 259): java.lang.SecurityException: Binder invocation to an incorrect interface
E/AndroidRuntime( 259): at android.os.Parcel.readException(Parcel.java:1327)
E/AndroidRuntime( 259): at android.os.Parcel.readException(Parcel.java:1281)
E/AndroidRuntime( 259): at com.android.internal.widget.IMediaPlaybackService$Stub$Proxy.prev(IMediaPlaybackService.java:457.
请提出这里可能出现的问题,我们将不胜感激任何帮助。如果您简要说明如何使用 bindService 或 IPC,也会很有帮助。