<?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>Gaunt Face &#124; Matthew Gaunt &#187; Proximity Alerts</title>
	<atom:link href="http://www.gauntface.co.uk/blog/tag/proximity-alerts/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gauntface.co.uk/blog</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 31 Jan 2012 23:21:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Proximity Alerts in Android</title>
		<link>http://www.gauntface.co.uk/blog/2010/01/04/proximity-alerts-in-android/</link>
		<comments>http://www.gauntface.co.uk/blog/2010/01/04/proximity-alerts-in-android/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 23:24:07 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Proximity Alerts]]></category>

		<guid isPermaLink="false">http://www.gauntface.co.uk/blog/?p=387</guid>
		<description><![CDATA[I&#8217;ve spent probably the best part of a couple of days attempting to get Proximity Alerts working in Android. There are a large number of Google Group threads, all leading to essentially someone saying it still doesn&#8217;t work. There was one or two that had working solutions, or some who managed to get it working [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">
<p style="text-align: center;"><a href="http://www.gauntface.co.uk/blog/wp-content/uploads/2009/08/androidLogo.png"><img class="aligncenter size-full wp-image-209" title="androidLogo" src="http://www.gauntface.co.uk/blog/wp-content/uploads/2009/08/androidLogo.png" alt="" width="342" height="190" /></a></p>
<p>I&#8217;ve spent probably the best part of a couple of days attempting to get Proximity Alerts working in Android. There are a large number of Google Group threads, all leading to essentially someone saying it still doesn&#8217;t work. There was one or two that had working solutions, or some who managed to get it working to a certain degree.</p>
<p>I managed to get a working implementation of proximity alerts by combining the information from a number of threads, this is just a guide through what I learnt and should get you up and running with your own alerts.</p>
<p>Just for the record, I need to learn a great deal more about Intent Filters and Broadcast Receivers as my knowledge of them is fairly vague, so please correct me in the comments if I say anything wrong, or should be implementing things differently.</p>
<p><span id="more-387"></span></p>
<p>Lets start of with the basic intent action:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public class EventsMapping extends MapActivity<br />
{<br />
// Proximity Alerts<br />
private String proximityIntentAction = new         String(&quot;co.uk.gauntface.android.wheresmycontacts.PROXIMITY_ALERT&quot;);</div></div>
<p>This is simply a string I have made up to signify the action of an intent is a proximity alert action (i.e. this identifies the intent as a proximity alert). The usual convention for naming the intent actions is com.some.package.action.ACTION_NAME, notice the action before the action name. This then needs to be applied to an intent filter, which is then applied to a broadcast receiver. This can be done in the AndroidManifest.xml file, however I didn&#8217;t have any luck this way (told you I needed to do some learning) so for now I can only show how to achieve this in code.</p>
<p>In your Activity:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IntentFilter intentFilter = new IntentFilter(proximityIntentAction);<br />
registerReceiver(new ProximityAlert(), intentFilter);</div></div>
<p>ProximityAlert Class:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public class ProximityAlert extends BroadcastReceiver<br />
{<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; public void onReceive(Context context, Intent intent)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.v(&quot;SomeTag&quot;,&quot;Proximity Alert Intent Received&quot;);<br />
&nbsp; &nbsp; }<br />
}</div></div>
<p>It&#8217;s worth noting at this point that you only need one broadcast receiver for a number of proximity alerts, since one receiver will pick up any number of intents fired off defined by the intent filter. We&#8217;ve got the broadcast receiver with the intent filter applied to it. Now lets define out proximity alerts function.</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">private void setProximityAlert(double lat, double lon, final long eventID, int requestCode)<br />
{<br />
// 100 meter radius<br />
float radius = 100f;<br />
<br />
// Expiration is 10 Minutes<br />
long expiration = 600000;<br />
<br />
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);<br />
<br />
Intent intent = new Intent(proximityIntentAction);<br />
<br />
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);<br />
<br />
locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);<br />
<br />
}</div></div>
<p>Breaking this down, you obviously set radius and expiration to anything you wish, expiration = -1 will mean the alert will never expire. We get the LocationManager as shown, the intent is just an intent using your action string I previously mentioned. In my example I added an extra item, now in the Google Groups there was quite a bit of talk about needing to apply a different extra to the intent, this isn&#8217;t necessary, however the VERY IMPORTANT THING to getting multiple proximity alerts working is in the request code in the PendingIntent.getBroadcast. In the java docs for pending intents, it says &#8220;Private request code for the sender (currently not used).&#8221; So one would assume it doesn&#8217;t matter, Wrong, it&#8217;s extremely important. You need to send a different request code for each proximity alert so the location manager can identify each of the pending intents as different, otherwise (I think) it caches them and treats them the same, meaning all proximity alerts will have the same extra data (or at least that&#8217;s what I found).</p>
<p>So I hope that helps someone, if I have missed anything out let me know, I know my naming for these classes is poor, but I was working on some coursework and refactoring was bottom of the list I&#8217;m afraid.</p>
<p>Do as I say, not as I do.</p>
<p>UPDATE: I&#8217;ve just made a full example of the code here &#8211; <a href="http://www.gauntface.co.uk/blog/2010/12/28/how-to-multiple-proximity-alerts-in-android/">http://www.gauntface.co.uk/blog/2010/12/28/how-to-multiple-proximity-alerts-in-android/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gauntface.co.uk/blog/2010/01/04/proximity-alerts-in-android/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

