0

I want to use MediaPlayback service (achieved in music app) in framework level, steps is:

added these codes in a java file (also add IMediaPlaybackService.aidl in the same directory) based in /frameworks/base/core/java/com/android:

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) {
    } 
}

the service binded successfully and the value of mMediaPlaybackService is corrected, but when i click a button and execute the statement "mMediaPlaybackService.prev()", the progrem will crash, the logcat dump is:

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.

Please suggest what could be going wrong here, and any help will be highly appreciated. It will be also helpful if you briefly explain how to use bindService or IPC.

Sam
  • 86,580
  • 20
  • 181
  • 179
SimpleSu
  • 31
  • 4

1 Answers1

0

I fixed this problem myself. the key is copy aidl to correct place. i want to add client code on the package com.android.internal.widget(frameworks/base/core/java/com/android/internal/widget), and aidl's package is com.android.music, so we need copy IMediaPlaybackService.aidl to frameworks/base/core/java/com/android/music/IMediaPlaybackService.aidl(make sure it will generate the same java file), and then add aidl path to LOCAL_SRC_FILE in frameworks/base/Android.mk(make sure this file will be compiled).

SimpleSu
  • 31
  • 4