How to bind to services from the android source to your app
Over the last few days, I’ve been trying to bind my app to the default Android Music Player.
Alex over here has a great tutorial to get you app to bind to the service.
The code still works and it works well.
NOTE: This only works from the 2.1 music player.
The only issue that arises is that it works only for the default Android Music Player.
The default service is called
com.android.music and it uses com.android.music.MediaPlaybackService
Now to go forward, we need the AIDL (Android Interface Definition Language) file from the default music player.
Go here and download the IMediaPlaybackService.aidl from the tree src/com/android/music
I’d suggest getting Tortoise SVN (if you are on a PC) and checkout the entire Music git.
Once you have that, create a new android project in Eclipse.
Name it whatever you want and create an activity, again using any name you want to.
Once you have done that, go to your Eclipse workspace in explorer (The path to the workspace shows up every time you start Eclipse).
Follow this path
%your workspace%\%you app%\src\create a new folder (if it doesn’t already exist) called “com” without the quotes.
Inside your “com” folder, create another folder “android” without the quotes. Inside the “android” folder create a folder “music” without the quotes. Copy the “IMediaPlaybackService.aidl” file here.
Go back to Eclipse now and in your main Activity write the following.
Intent i = new Intent();
i.setClassName("com.android.music", "com.android.music.MediaPlaybackService");
ServiceConnection conn = new MediaPlayerServiceConnection();
this.bindService(i, conn, 0);
Let me explain the code above.
Whenever we want to do anything to the device or access anything on the device, we create “Intents”.
Above using Intent i we set the class for i to “com.android.music”, “com.android.music.MediaPlaybackService”, ie the process and the source.
Then using a service connection named conn, which we will define later, we connect (bind) to the Music Player service.
Now create another class file under your app package called “MediaPlayerServiceConnection”
Inside paste the following code.
public class MediaPlayerServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder boundService) {
Log.i("MediaPlayerServiceConnection", "Connected! Name: " + name.getClassName());
// This is the important line
service = IMediaPlaybackService.Stub.asInterface((IBinder) boundService);
// If all went well, now we can use the interface
try {
Log.i("MediaPlayerServiceConnection", "Playing track: " + service.getTrackName());
//Tell the player to pause the song
service.pause();
//Log.i("MediaPlayerServiceConnection", "By artist: " + service.getArtistName());
/*if (service.isPlaying()) {
Log.i("MediaPlayerServiceConnection", "Music player is playing.");
} else {
Log.i("MediaPlayerServiceConnection", "Music player is not playing.");
}*/
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void onServiceDisconnected(ComponentName name) {
service=null;
Log.i("MediaPlayerServiceConnection", "Disconnected!");
}
}
Now start playing a song from your music player.
Next in Eclipse, compile and run the code on your Android device/emulator.
If everything went fine, the song you were just playing will pause after the app loads.
If the song pauses, you have successfully bound your app the default music player service.
From here, you can use the service like any other service.
NOTE: My code is not perfect and will never be. This code is not for use in all real world examples and is just for enhancing your knowledge.
If you have found any mistakes in my code, please bring it to my attention in the comments below. I would gladly fix them up.



