<?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>Digital Craig &#187; Projects</title>
	<atom:link href="http://www.digitalcraig.me/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.digitalcraig.me</link>
	<description>Surviving in the digital wasteland</description>
	<lastBuildDate>Thu, 20 Oct 2011 23:44:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Be My Valentine (or my first real Android app)</title>
		<link>http://www.digitalcraig.me/2011/02/be-my-valentine-or-my-first-real-android-app/</link>
		<comments>http://www.digitalcraig.me/2011/02/be-my-valentine-or-my-first-real-android-app/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 03:07:10 +0000</pubDate>
		<dc:creator>Craig</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.digitalcraig.me/?p=9442</guid>
		<description><![CDATA[It all started with trying to find a way to top the most memorable Valentine&#8217;s Day with my wife. She always talks about the first one after we were married in which I hired a barbershop quartet to serenade her at the front door of our apartment. She claims they sang a song that was a [...]]]></description>
			<content:encoded><![CDATA[<p>It all started with trying to find a way to top the most memorable Valentine&#8217;s Day with my wife. She always talks about the first one after we were married in which I hired a barbershop quartet to serenade her at the front door of our apartment. She claims they sang a song that was a key fixture in our wedding itself, a Victorian song that is also popular with barbershop quartets around Valentine&#8217;s Day. I consider it a lucky break. <img src='http://www.digitalcraig.me/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>I managed to (not so subtly) pry the name of the song from her a few weeks ago and found (what I hope) is an mp3 recording of a barbershop quartet performing it. Of course, I can&#8217;t verify this because I can&#8217;t let her listen to it without tipping my hand. However, even if it&#8217;s not the right one, it&#8217;s at least a romantic song sung by a barbershop quartet with the same name.</p>
<p>So, what to do with it, then? I finally came up with the idea to write an Android app using the song and a few other elements to create a customized Valentine&#8217;s Day greeting card app.</p>
<p><span id="more-9442"></span></p>
<p>The kids got new Nintendo DSi portable gaming devices for Christmas with an app called <a title="Flipnote" href="http://www.nintendodsi.com/flipnotestudio.jsp">Flipnote</a> which allows them to create animations and share them with their friends. This has been the biggest fun for them and I found out that you can export the frames of the animations as individual GIF files to an SD card. It just so happens that Android supports the <a title="AnimationDrawable" href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html">AnimationDrawable</a> which is defined in XML like so:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;animation-list</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">android:oneshot</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/b1001&quot;</span> <span style="color: #000066;">android:duration</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/b1002&quot;</span> <span style="color: #000066;">android:duration</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/b1003&quot;</span> <span style="color: #000066;">android:duration</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; .<br />
&nbsp; &nbsp; .<br />
&nbsp; &nbsp; .<br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">android:drawable</span>=<span style="color: #ff0000;">&quot;@drawable/b1068&quot;</span> <span style="color: #000066;">android:duration</span>=<span style="color: #ff0000;">&quot;20000&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/animation-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p><span style="font-family: monospace;"><br />
</span> The &#8220;@drawable/b1004&#8243; strings will resolve the the individual GIF files in the res/drawables directory in the android package. In this case they are called b1001.gif, b1002.gif, b1003.gif, etc. Also, the animation defined in the XML can be addressed as a @drawable itself and can be controlled in code like so:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Beginning onCreate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Calling super&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Allocating ImageView animation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;animationImage <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ImageView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">imageView1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;animationImage.<span style="color: #006633;">setBackgroundResource</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">animation</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Animation <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>AnimationDrawable<span style="color: #009900;">&#41;</span> animationImage.<span style="color: #006633;">getBackground</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Allocating AnimationDrawable Animation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Ending onCreate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onTouchEvent<span style="color: #009900;">&#40;</span>MotionEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Starting onTouchEvent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> MotionEvent.<span style="color: #006633;">ACTION_DOWN</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Start animation</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Stopping Animation playback using touch event&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Animation.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Starting Animation playback using touch event&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Animation.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Calling Super&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Ending onTouchEvent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onTouchEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The next resource I need is the actual song itself to play. For this, I placed the mp3 file into the res/raw folder and now I can reference it using the generated R class (in this case R.raw.songname in place of res/raw/songname.mp3).</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> playAudio<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Starting playAudio&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Allocating MedaPlayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mp <span style="color: #339933;">=</span> MediaPlayer.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplication</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, R.<span style="color: #006633;">raw</span>.<span style="color: #006633;">song</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Starting playback&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mp.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Ending playAudio&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span></div></div>
<p>The playAudio() function is called anytime in the application lifecycle that the the music should start playing. It&#8217;s important to note here that at this point in the development I started to run into numerous OutOfMemory exceptions and that&#8217;s the reason there are so many calls to Log.d which outputs to the debug console in Eclipse. At first I thought the cause of these was the mediaplayer, but it turned out to be the animation.</p>
<p>The garbage collector cannot reclaim memory unless all references to an object are removed and with a 16MB heap, it looks like all of those GIF images was using up a pretty big chunk. There wasn&#8217;t much room for a reference to be left behind. </p>
<p>As I soon discovered through a lot reading of SDK documentation and forums, the ImageView and the animation itself requires a little bit of extra handling in onPause() and onDestroy(). The ImageView itself retains a reference to the animation resource in it&#8217;s callback and so the callbacks must be set to null and garbage collection initiated:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Beginning onDestroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Calling Super&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDestroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mp <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Releasing mediaplayer resource&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp.<span style="color: #006633;">release</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>animationImage <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Release animationImage resource&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; animationImage.<span style="color: #006633;">setBackgroundDrawable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; animationImage <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Animation <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Stopping and freeing up Animation resource&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Animation.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Animation.<span style="color: #006633;">setCallback</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Animation <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Initiating Garbage Collection&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">gc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;ending onDestroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>The final resource I need to manage is an HTML-escaped string displayed in a ScrollView which contains a heartfelt message to the love of my life.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Allocating TextView&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextView notes <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">textView1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Getting String Resources&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resources res <span style="color: #339933;">=</span> getResources<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Formatting note with HTML&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> text <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">note</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CharSequence styledText <span style="color: #339933;">=</span> Html.<span style="color: #006633;">fromHtml</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Assigning formatted text to TextView&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; notes.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>styledText<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>At this point, the application is completely functional. I can install it on a device using a debug key and launch it from the icon in the application manager. I can tap the animation and it plays, the music plays when the application is on-screen, and the text is scrollable. The application also functions in both orientations. However, keep in mind, that I developed the app on my Motorola Droid X for my wife&#8217;s Motorola Droid and the app is not for general release so I probably made a lot of assumptions about device capabilities.</p>
<p>There is one final feature that I really wanted to add to make it fully functional. At a given date and time (February 14, 2011 7:00am), I wanted a notification to appear with a heart icon and the text &#8220;Be My Valentine&#8221;. When she sees that and selects the notification, I want the app to start and do its thing. When the day is over, I want the notification to no longer show up. As it turns out, implementing this one feature was nearly as much work as all the entire app itself.</p>
<p>There are several pieces to this: an alarm, a service, a receiver, and a binder.</p>
<p>First in the UI class&#8217;s onCreate(), I need to start the service I created to handle notifications using an alarm at a particular time:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create an IntentSender that will launch our service, to be scheduled</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// with the alarm manager.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Creating AlarmSender&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mAlarmSender <span style="color: #339933;">=</span> PendingIntent.<span style="color: #006633;">getService</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>BeMyValentine.<span style="color: #000000; font-weight: bold;">this</span>, BeMyValentineService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Setting time for alarm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a> calendar <span style="color: #339933;">=</span> &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Agregoriancalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">GregorianCalendar</span></a>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; calendar.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">YEAR</span>,<span style="color: #cc66cc;">2011</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; calendar.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">MONTH</span>,<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">FEBRUARY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; calendar.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">DAY_OF_MONTH</span>,<span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; calendar.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">HOUR_OF_DAY</span>,<span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; calendar.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">MINUTE</span>,00<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,calendar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Schedule the alarm!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Scheduling alarm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Schedule the alarm!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; AlarmManager am <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>AlarmManager<span style="color: #009900;">&#41;</span>getSystemService<span style="color: #009900;">&#40;</span>ALARM_SERVICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; am.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>AlarmManager.<span style="color: #006633;">RTC_WAKEUP</span>, calendar.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, mAlarmSender<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>I also need a broadcast receiver to handle broadcasts sent to my application. Specifically, the broadcast generated by the alarm which will start the service.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BeMyValentineReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BeMyValentine&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Context</span></a> context, Intent intent<span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Receiver: starting onReceive&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// we start notification service on start up</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Receiver: creating intent for service&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Intent startActivityIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>context, BeMyValentineService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Receiver: starting service&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">startService</span><span style="color: #009900;">&#40;</span>startActivityIntent <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Receiver: ending onReceive&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now for the real workhorse of this feature, the BeMyValentineService. In the service&#8217;s onCreate(), I call a function to display the notification by creating an intent to launch the app and calling the system notification service to display it (this will quickly disappear if the date not February 14, 2011):</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Show a notification while this service is running.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> showNotification<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; <br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set up Notification</span><br />
&nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Service: Creating intent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>,BeMyValentine.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NotificationManager<span style="color: #009900;">&#41;</span> getSystemService<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Context</span></a>.<span style="color: #006633;">NOTIFICATION_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Service: Creating notification&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Notification notification <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Notification<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">icon</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;Notify&quot;</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; notification.<span style="color: #006633;">setLatestEventInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;Be My Valentine&quot;</span>,<span style="color: #0000ff;">&quot;Select for a special message&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PendingIntent.<span style="color: #006633;">getActivity</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getBaseContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span>, intent,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PendingIntent.<span style="color: #006633;">FLAG_CANCEL_CURRENT</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Service: Sending notification notification to manager&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mManager.<span style="color: #006633;">notify</span><span style="color: #009900;">&#40;</span>APP_ID, notification<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Service: ending onCreate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>Next, I create and start a thread whose job it is to wait until a specific time and tell the service to shut itself down:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* The function that runs in our worker thread<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arunnable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Runnable</span></a> mTask <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arunnable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Runnable</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Setting time for alarm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a> cal <span style="color: #339933;">=</span> &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Agregoriancalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">GregorianCalendar</span></a>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">YEAR</span>,<span style="color: #cc66cc;">2011</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">MONTH</span>,<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">FEBRUARY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">DAY_OF_MONTH</span>,<span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">HOUR_OF_DAY</span>,<span style="color: #cc66cc;">23</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">MINUTE</span>,<span style="color: #cc66cc;">59</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,cal.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> endTime <span style="color: #339933;">=</span> cal.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> endTime<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span>mBinder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mBinder.<span style="color: #006633;">wait</span><span style="color: #009900;">&#40;</span>endTime <span style="color: #339933;">-</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Done with our work... &nbsp;stop the service!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BeMyValentineService.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">stopSelf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Of course, part of the service shutting down is to cancel the notification:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Service: starting onDestroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mManager.<span style="color: #006633;">cancel</span><span style="color: #009900;">&#40;</span>APP_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Service: calling Super&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDestroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Service: ending onDestroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>Finally, the glue that holds it all together is the binder object which synchronizes the work of the thread (it&#8217;s returned in the service&#8217;s onBind() function).</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> IBinder mBinder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Binder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">boolean</span> onTransact<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> code, Parcel data, Parcel reply,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> flags<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aremoteexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">RemoteException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onTransact</span><span style="color: #009900;">&#40;</span>code, data, reply, flags<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>One final note. I&#8217;ve only posted sections of code use to highlight particular challenges I had to solve. There are a lot of smaller pieces that are used to put it all together and I relied heavily on the SDK documentation, the SDK sample code, <a href="http://www.stackoverflow.com">StackOverflow</a>, and other forums.</p>
<p>Feel free to contact me if you want more information or help with solving a similar problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.digitalcraig.me/2011/02/be-my-valentine-or-my-first-real-android-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Online Genealogy project</title>
		<link>http://www.digitalcraig.me/2010/01/online-genealogy/</link>
		<comments>http://www.digitalcraig.me/2010/01/online-genealogy/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 00:35:20 +0000</pubDate>
		<dc:creator>Craig</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.digitalcraig.me/?p=165</guid>
		<description><![CDATA[The past few weeks while I&#8217;ve had quite a bit of vacation time, I&#8217;ve worked on putting some of our family&#8217;s genealogical information online. I installed PHPGedview and ask my mom, who is the keeper of the official genealogy for the family, to provide me with a GEDCOM file to try it out with. It took [...]]]></description>
			<content:encoded><![CDATA[<p>The past few weeks while I&#8217;ve had quite a bit of vacation time, I&#8217;ve worked on putting some of our family&#8217;s genealogical information online. I installed <a href="http://www.phpgedview.net" target="_blank">PHPGedview</a> and ask my mom, who is the keeper of the official genealogy for the family, to provide me with a GEDCOM file to try it out with.</p>
<p>It took quite a bit of effort to get the initial file imported. I think it&#8217;s because of the size of the file and the limitations of the shared server I&#8217;m hosting it on. It kept running out of memory, but once I figured out how to increase the limit,  it seemed to go okay. After some initial testing, we opened the site up to the rest of the family to review their records and make corrections. It was all going okay.</p>
<p>Once people started making changes, the question came up of how do we take changes people have made and get it back into my mom&#8217;s official record in Family Tree Maker. I decided to let her approve all changes and use the change report to select just those records that had changed, create a new GEDCOM file which could be re-imported. The only problem was that the change report seemed to be broken in the version I was using.</p>
<p>I decided to upgrade to the lastest version, but it has a bug in it which doesn&#8217;t allow you login with Internet Explorer. I filed a bug report, but so far nothing. Meanwhile, things kept trucking along.</p>
<p>The major problem came in when one of our relatives sent an entirely new GEDCOM that contained all of the information for that branch of the family. My dad imported the file into Family Tree Maker and tried to do an entirely new export of the entire database to overwrite the information in PHPGedview so that it would include the new data. That&#8217;s where things went wrong. I&#8217;m not able to import the new file and I&#8217;m no longer able to import the original file that worked before.</p>
<p>I have no idea how to proceed at this point. No matter which file I try to import or how I do it (fiddling with the memory limits), the import fails with a 500 server error. I even tried creating a virtual machine with VirtualBox and a LAMP appliance to do the import and so far it&#8217;s not worked either. It&#8217;s weird that it gets to 16% or so and then the progress meter goes back to 2% and starts counting up again.</p>
<p>Maybe I should give up on PHPGedview and try something else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.digitalcraig.me/2010/01/online-genealogy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

