<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aditya Talpade &#187; Android Java</title>
	<atom:link href="http://www.adityatalpade.com/category/android-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adityatalpade.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Wed, 02 May 2012 09:23:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Mnemonic Major System</title>
		<link>http://www.adityatalpade.com/2011/08/15/mnemonic-major-system/</link>
		<comments>http://www.adityatalpade.com/2011/08/15/mnemonic-major-system/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 06:39:06 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[Android Java]]></category>
		<category><![CDATA[My Apps]]></category>

		<guid isPermaLink="false">http://www.adityatalpade.com/?p=186</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[]]></content:encoded>
			<wfw:commentRss>http://www.adityatalpade.com/2011/08/15/mnemonic-major-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to bind to services from the android source to your app</title>
		<link>http://www.adityatalpade.com/2010/07/31/how-to-bind-to-services-from-the-android-source/</link>
		<comments>http://www.adityatalpade.com/2010/07/31/how-to-bind-to-services-from-the-android-source/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 05:58:14 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[Android Java]]></category>
		<category><![CDATA[andorid]]></category>
		<category><![CDATA[bind to services]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[music player]]></category>

		<guid isPermaLink="false">http://www.adityatalpade.com/?p=110</guid>
		<description><![CDATA[Over the last few days, I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few days, I&#8217;ve been trying to bind my app to the default Android Music Player.</p>
<p>Alex over <a title="Bind to Music Player" href="http://www.alexc.me/android-music-app-service-currently-playing-song/231/" target="_self">here</a> has a great tutorial to get you app to bind to the service.</p>
<p>The code still works and it works well.<br />
<span class="normal-text">NOTE:</span> This only works from the 2.1 music player.<br />
The only issue that arises is that it works only for the default Android Music Player.</p>
<p>The default service is called</p>
<pre class="brush:java">com.android.music and it uses com.android.music.MediaPlaybackService</pre>
<p>Now to go forward, we need the <em>AIDL</em> (Android Interface Definition Language) file from the default music player.<br />
Go <a title="Android Github" href="http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=tree" target="_blank">here</a> and download the IMediaPlaybackService.aidl from the tree src/com/android/music</p>
<blockquote><p>I&#8217;d suggest getting Tortoise SVN (if you are on a PC) and checkout the entire Music git.</p></blockquote>
<p>Once you have that, create a new android project in Eclipse.<br />
Name it whatever you want and create an activity, again using any name you want to.</p>
<p>Once you have done that, go to your Eclipse workspace in explorer (The path to the workspace shows up every time you start Eclipse).</p>
<p>Follow this path<br />
%your workspace%\%you app%\src\create a new folder (if it doesn&#8217;t already exist) called &#8220;com&#8221; without the quotes.<br />
Inside your &#8220;com&#8221; folder, create another folder &#8220;android&#8221; without the quotes. Inside the &#8220;android&#8221; folder create a folder &#8220;music&#8221; without the quotes. Copy the &#8220;IMediaPlaybackService.aidl&#8221; file here.</p>
<p>Go back to Eclipse now and in your main Activity write the following.</p>
<pre class="brush:java">Intent i = new Intent();
i.setClassName("com.android.music", "com.android.music.MediaPlaybackService");

ServiceConnection conn = new MediaPlayerServiceConnection();
this.bindService(i, conn, 0);</pre>
<p>Let me explain the code above.<br />
Whenever we want to do anything to the device or access anything on the device, we create &#8220;Intents&#8221;.<br />
Above using Intent i we set the class for i to &#8220;com.android.music&#8221;, &#8220;com.android.music.MediaPlaybackService&#8221;, ie the process and the source.<br />
Then using a service connection named conn, which we will define later, we connect (bind) to the Music Player service.<br />
Now create another class file under your app package called &#8220;MediaPlayerServiceConnection&#8221;</p>
<p>Inside paste the following code.</p>
<pre class="brush:java">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!");
		}
	}</pre>
<p>Now start playing a song from your music player.</p>
<p>Next in Eclipse, compile and run the code on your Android device/emulator.</p>
<p>If everything went fine, the song you were just playing will pause after the app loads.</p>
<p>If the song pauses, you have successfully bound your app the default music player service.</p>
<p>From here, you can use the service like any other service.</p>
<p><span class="normal-text">NOTE:</span> 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.<br />
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.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.adityatalpade.com%2F2010%2F07%2F31%2Fhow-to-bind-to-services-from-the-android-source%2F&amp;title=How%20to%20bind%20to%20services%20from%20the%20android%20source%20to%20your%20app" id="wpa2a_2"><img src="http://www.adityatalpade.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.adityatalpade.com/2010/07/31/how-to-bind-to-services-from-the-android-source/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

