<?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>websmithing</title>
	<atom:link href="http://www.websmithing.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.websmithing.com</link>
	<description>connecting the world to the internet</description>
	<lastBuildDate>Fri, 08 Jul 2011 19:23:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Sync Up an iphone App with Dropbox and a Couchdb Database in the Cloud</title>
		<link>http://www.websmithing.com/2011/07/08/how-to-sync-up-an-iphone-app-with-dropbox-and-a-couchdb-database-in-the-cloud/</link>
		<comments>http://www.websmithing.com/2011/07/08/how-to-sync-up-an-iphone-app-with-dropbox-and-a-couchdb-database-in-the-cloud/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 19:22:25 +0000</pubDate>
		<dc:creator>Nick Fox</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.websmithing.com/?p=73</guid>
		<description><![CDATA[I was recently working on a project for a client and needed to sync up a catalog app on an iphone with data and images on the internet. The client wanted to be able to update the catalog on the internet and then have the iphone app automatically sync up. I&#8217;ve put the project on [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working on a project for a client and needed to sync up a catalog app on an iphone with data and images on the internet. The client wanted to be able to update the catalog on the internet and then have the iphone app automatically sync up. I&#8217;ve put the project on github and have directions there on how to install and the app.</p>
<p><a href="https://github.com/nickfox/iphoneCloudSync">The full source code for iPhoneCloudSync is here on github.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.websmithing.com/2011/07/08/how-to-sync-up-an-iphone-app-with-dropbox-and-a-couchdb-database-in-the-cloud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Update the UI in an Android Activity Using Data from a Background Service</title>
		<link>http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/</link>
		<comments>http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 06:44:46 +0000</pubDate>
		<dc:creator>Nick Fox</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://www.websmithing.com/?p=52</guid>
		<description><![CDATA[Recently, I needed to update an activity in Android with data I gathered from a background Service. I couldn&#8217;t find any decent examples on the web or StackOverflow, so I decided to put one together. I really like when someone has a complete working sample, so I will provide that as well you can download [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I needed to update an activity in Android with data I gathered from a background Service. I couldn&#8217;t find any decent examples on the web or StackOverflow, so I decided to put one together. I really like when someone has a complete working sample, so I will provide that as well you can download the source code from Github at the end of this article. The Android project is very simple, with just two classes, the activity and the background service. We&#8217;ll take a look first at the background service to see how the data is generated.</p>
<p>The first thing that needs to do is to create a string called BROADCAST_ACTION, this is simply a string that identifies what kind of action is taking place. Most of time, it&#8217;s common to use the package name with the kind of action added to the end, so we&#8217;ll do that here. The next step is to create a handler that will be used to broadcast our data every 5 seconds. It&#8217;s better to use a Handler instead of Timer because a Timer creates a new thread.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BroadcastService  <span style="color: #000000; font-weight: bold;">extends</span> Service <span style="color: #009900;">&#123;</span>
    <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> <span style="color: #003399;">String</span> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BroadcastService&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> BROADCAST_ACTION <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.websmithing.broadcasttest.displayevent&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Handler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Intent intent<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> counter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></pre></div></div>

<p>In onCreate, I&#8217;ve created a Intent and passed the BROADCAST_ACTION to the constructor of the intent. Notice that the Intent was defined as a global variable above. The intent will be called repeatedly in the Handler below and there is no reason to create a new intent every 5 seconds, so I&#8217;ll create it once here.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Override
    <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><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>BROADCAST_ACTION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>In onStart, first we call removeCallbacks to remove any existing callbacks to the handler and make sure we don&#8217;t get more callbacks than we want. Then we call our handler with a one second delay. This will start our runnable object below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span>Intent intent, <span style="color: #000066; font-weight: bold;">int</span> startId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        handler.<span style="color: #006633;">removeCallbacks</span><span style="color: #009900;">&#40;</span>sendUpdatesToUI<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        handler.<span style="color: #006633;">postDelayed</span><span style="color: #009900;">&#40;</span>sendUpdatesToUI, <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 1 second   </span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Our runnable object creates a new thread, performs whatever code is in the run method and then shuts down. In the run method, we do two things call the DisplayLoggingInfo method and then calls  handler.postDelayed again but this time with a 5 second delay. This is how the repeating timer is created. One of the things that is nice about doing it this way, is that you can put a variable in place of 5000 and that way you can externally control the interval between call to the runnable object.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Runnable</span> sendUpdatesToUI <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<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>
    	    DisplayLoggingInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    		
    	    handler.<span style="color: #006633;">postDelayed</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 5 seconds</span>
    	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the following method, we add some data to our intent that was created above. I&#8217;m just adding the date and a simple counter that increments itself. Then we call sendBroadcast with that intent which sends a message and whoever is registered to receive that message will then get it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> DisplayLoggingInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;entered DisplayLoggingInfo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;time&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toLocaleString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	intent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;counter&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>counter<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	sendBroadcast<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>At this point, the Service is completed and now it&#8217;s time to take a look at our Activity and see how we consume and display our data. The first thing we&#8217;ll do is create an intent with the name of the Service class. This will be passed to startService and stopService.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BroadcastTest <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
	<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> <span style="color: #003399;">String</span> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BroadcastTest&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Intent intent<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <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>
        <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>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        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>, BroadcastService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we&#8217;ll create a BroadcastReceiver to receive the message that is going to be broadcast from the Service above. The BroadcastReceiver has one method, onReceive, it gets called and then the BroadcastReceiver object is destroyed. In the onReceive method, we call updateUI passing in our intent which is holding the data to display.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> BroadcastReceiver broadcastReceiver <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BroadcastReceiver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        @Override
        <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><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        	updateUI<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In onResume and onPause, we start and stop our Service. This happens when the screen (Activity) displays and goes away. We also register our BroadcastReceiver by passing in an IntentFilter. Do you see that the IntentFilter is using the same static string, BROADCAST_ACTION, that we created in the Service above. This is how we identify the message that is broadcast. in onPause, we also make sure that we call unregisterReceiver to stop listening for broadcasts.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onResume<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onResume</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		startService<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		registerReceiver<span style="color: #009900;">&#40;</span>broadcastReceiver, <span style="color: #000000; font-weight: bold;">new</span> IntentFilter<span style="color: #009900;">&#40;</span>BroadcastService.<span style="color: #006633;">BROADCAST_ACTION</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onPause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		unregisterReceiver<span style="color: #009900;">&#40;</span>broadcastReceiver<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stopService<span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 		
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This last bit of code is pretty straight forward. We get our data out of the intent and set our two TextViews with the data. They will be updated every 5 seconds.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> updateUI<span style="color: #009900;">&#40;</span>Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #003399;">String</span> counter <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;counter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    	<span style="color: #003399;">String</span> time <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;time&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	TextView txtDateTime <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;">txtDateTime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  	
    	TextView txtCounter <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;">txtCounter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	txtDateTime.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	txtCounter.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ve made the entire project <a href="https://github.com/nickfox/Update-Android-UI-from-a-Service">available for download here on GitHub</a>. You can either clone the project or download it with the Downloads button.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Find your lost Android Cell Phone!</title>
		<link>http://www.websmithing.com/2010/08/20/find-your-lost-android-cell-phone/</link>
		<comments>http://www.websmithing.com/2010/08/20/find-your-lost-android-cell-phone/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 22:55:58 +0000</pubDate>
		<dc:creator>Nick Fox</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Google Map GPS Cell Phone Tracker]]></category>

		<guid isPermaLink="false">http://www.websmithing.com/?p=34</guid>
		<description><![CDATA[I&#8217;ve taken the open source GPS cell phone tracker and rewrote it so that it works with Android cell phones. Since I wrote the original tracker, I&#8217;ve had many people who are not developers ask me how they can track their cell phone. So I created a website that allows people to do that. You [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve taken the open source GPS cell phone tracker and rewrote it so that it works with Android cell phones. Since I wrote the original tracker, I&#8217;ve had many people who are not developers ask me how they can track their cell phone. So I created a website that allows people to do that. You can download the app from the Android Market by searching for MyCellTracker and then going to:</p>
<p><a href="http://www.mycelltracker.com">www.mycelltracker.com</a></p>
<p>You can try the app free for 7 days and then subscribe for either $1.99/month or $19.95/year. Please give it a try and also click on the facebook like button if you would be so kind!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websmithing.com/2010/08/20/find-your-lost-android-cell-phone/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google Map GPS Cell Phone Tracker</title>
		<link>http://www.websmithing.com/2010/02/14/google-map-gps-cell-phone-tracker/</link>
		<comments>http://www.websmithing.com/2010/02/14/google-map-gps-cell-phone-tracker/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 17:41:16 +0000</pubDate>
		<dc:creator>Nick Fox</dc:creator>
				<category><![CDATA[Google Map GPS Cell Phone Tracker]]></category>

		<guid isPermaLink="false">http://www.websmithing.com/?p=1</guid>
		<description><![CDATA[I&#8217;m happy to announce that the test webpage for the Google GPS Cell Phone Tracker is now operational. You can test your cell phone tracker using this app!]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m happy to announce that the test webpage for the <a href="http://www.websmithing.com/gpstracker2/displaymap2.php">Google GPS Cell Phone Tracker</a> is now operational. You can test your cell phone tracker using this app!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.websmithing.com/2010/02/14/google-map-gps-cell-phone-tracker/feed/</wfw:commentRss>
		<slash:comments>141</slash:comments>
		</item>
	</channel>
</rss>

