<?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>Byte Foundry</title>
	<atom:link href="http://www.bytefoundry.co.uk/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.bytefoundry.co.uk/blog</link>
	<description>Obscure internet technobabble...</description>
	<lastBuildDate>Mon, 19 Nov 2012 15:04:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>How to Fix the Firefox Font (Windows)</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=221</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=221#comments</comments>
		<pubDate>Wed, 29 Feb 2012 16:51:34 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Font]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=221</guid>
		<description><![CDATA[If you think the font in Firefox on Windows XP looks horrible it may be because you are using the standard method of edge smoothing for screen fonts (which is the default). There is a setting you can adjust in your &#8220;Display Properties&#8221; to set the smoothing method to &#8220;ClearType&#8221;. Apparently Internet Explorer does this [...]]]></description>
			<content:encoded><![CDATA[<p>If you think the font in Firefox on Windows XP looks horrible it may be because you are using the standard method of edge smoothing for screen fonts (which is the default). There is a setting you can adjust in your &#8220;Display Properties&#8221; to set the smoothing method to &#8220;ClearType&#8221;. Apparently Internet Explorer does this by itself but you need to do the following to enable it in Firefox:</p>
<p>1. Right-click on your Desktop and choose &#8220;Properties&#8221;.<br />
2. Click on the &#8220;Appearance&#8221; tab.<br />
3. Click the &#8220;Effects&#8221; button.<br />
4. Select the checkbox for the &#8220;Use the following method to smooth edges of screen fonts&#8221; option and set the drop-down list to &#8220;ClearType&#8221;.<br />
5. Click OK to exit.</p>
<p>This is illustrated below:</p>
<p><a href="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2012/02/firefox_font.png"><img class="size-full wp-image-222 aligncenter" title="Firefox Font Fix" src="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2012/02/firefox_font.png" alt="Setting &quot;ClearType&quot; smoothing method." width="414" height="309" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=221</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android: Storing a DatePicker and TimePicker in SQLite Database</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=214</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=214#comments</comments>
		<pubDate>Wed, 29 Feb 2012 12:00:37 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[DatePicker]]></category>
		<category><![CDATA[TimePicker]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=214</guid>
		<description><![CDATA[If you are looking for an easy way to store a DatePicker and TimePicker control to an SQLite database, one method is to convert them to a Calendar object and store the combined date and time in milliseconds (a &#8216;long&#8217; value). Take the following method as an an example: private Calendar createAsCalendar&#40;DatePicker dp, TimePicker tp&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>If you are looking for an easy way to store a DatePicker and TimePicker control to an SQLite database, one method is to convert them to a Calendar object and store the combined date and time in milliseconds (a &#8216;long&#8217; value). Take the following method as an an example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> createAsCalendar<span style="color: #009900;">&#40;</span>DatePicker dp, TimePicker tp<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Convert a DatePicker and TimePicker control in to a calendar object. The time is</span>
    <span style="color: #666666; font-style: italic;">// stored in UTC minutes and unaffected by time zone</span>
    <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span>, dp.<span style="color: #006633;">getYear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span>, dp.<span style="color: #006633;">getMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span>, dp.<span style="color: #006633;">getDayOfMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, tp.<span style="color: #006633;">getCurrentHour</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, tp.<span style="color: #006633;">getCurrentMinute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> cal<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-214"></span></p>
<p>You can then call the Calendar&#8217;s <em>getTimeInMillis()</em> method to get a long value to store in your database. When it comes to restoring the value you simply create a new UTC Calendar object as per above and then pass the restored value to the Calendar&#8217;s <em>setTimeInMillis()</em> method.</p>
<p>Below is an example of restoring to a Calendar object and then updating the DatePicker and TimePicker controls:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// m_eventDate is a DatePicker control</span>
<span style="color: #666666; font-style: italic;">// m_eventTime is a TimePicker control</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// m_restoredDateTime is a 'long' value restored from the SQLite database</span>
<span style="color: #666666; font-style: italic;">// acquired by calling getTimeInMillis() from the Calendar in previous code.</span>
&nbsp;
<span style="color: #003399;">Calendar</span> calEvent <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calEvent.<span style="color: #006633;">setTimeInMillis</span><span style="color: #009900;">&#40;</span>m_restoredDateTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
m_eventDate.<span style="color: #006633;">updateDate</span><span style="color: #009900;">&#40;</span>calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span><span style="color: #009900;">&#41;</span>, calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span><span style="color: #009900;">&#41;</span>, calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    		
&nbsp;
m_eventTime.<span style="color: #006633;">setCurrentHour</span><span style="color: #009900;">&#40;</span>calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
m_eventTime.<span style="color: #006633;">setCurrentMinute</span><span style="color: #009900;">&#40;</span>calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=214</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Operations With Java Calendar API</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=196</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=196#comments</comments>
		<pubDate>Sun, 26 Feb 2012 16:06:42 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[Common]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Operations]]></category>
		<category><![CDATA[Time]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=196</guid>
		<description><![CDATA[The date/time API in Java gets a bad reputation but it is not so bad to work with once you get used to it and is fine for doing simple manipulations or calculations without the need to use a third party library. There are various bugs and some performance issue with the native API so [...]]]></description>
			<content:encoded><![CDATA[<p>The date/time API in Java gets a bad reputation but it is not so bad to work with once you get used to it and is fine for doing simple manipulations or calculations without the need to use a third party library. There are various bugs and some performance issue with the native API so if you are doing an application which heavily utilises date/time functionality you might want to look in to using something like <a title="Joda Time" href="http://joda-time.sourceforge.net/">Joda Time</a> instead.</p>
<p>The majority of date/time operations in Java are done using the Calendar object. Below are examples of some common date/time operations using the native Java API:</p>
<p><span id="more-196"></span></p>
<p><strong>Contents:</strong><br />
<a href="#Operation1">1. Create a Calendar Object for &#8220;Now&#8221; (i.e. Current Date/Time and Time Zone)</a><br />
<a href="#Operation2">2. Create a Basic Calendar Object (i.e. Disregard the Time Zone)</a><br />
<a href="#Operation3">3. Setting the Date and Time In a Calendar Object</a><br />
<a href="#Operation4">4. Getting Fields From Calendar Object (Year, Month, Day, Hour, Minute, Second)</a><br />
<a href="#Operation5">5. Date Arithmetic (Add/Subtract Values to a Calendar Object)</a><br />
<a href="#Operation6">6. Create a Calendar Object in a Specific Time Zone</a><br />
<a href="#Operation7">7. Convert Between Time Zones</a><br />
<a href="#Operation8">8. How to Store the Date and Time in an SQLite Database?</a><br />
<a href="#Operation9">9. Get the Time Between Two Dates</a></p>
<p><a name="Operation1"></a><br />
<strong>1. Create a Calendar Object for &#8220;Now&#8221; (i.e. Current Date/Time and Time Zone)</strong></p>
<p>The following code will create an instance of the Calendar object. The date, time and time zone are set to that of the system at the time of instantiation.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Create a Calendar object in my current time zone and set </span>
<span style="color: #666666; font-style: italic;">// with the current date and time.</span>
<span style="color: #003399;">Calendar</span> calNow <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a name="Operation2"></a><br />
<strong>2. Create a Basic Calendar Object (i.e. Disregard the Time Zone)</strong></p>
<p>To create a basic Calendar object that is not augmented by time zone you would create it specifically with the &#8220;UTC&#8221; time zone which is effectively GMT+0:00. You would use this as a base for most date/time calculations and time zone conversions and also if you want to store the date/time in a database.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get a time zone independent Calendar instance</span>
<span style="color: #003399;">Calendar</span> calBase <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a name="Operation3"></a><br />
<strong>3. Setting the Date and Time In a Calendar Object</strong></p>
<p>To create a Calendar object representing &#8220;<strong>December 25, 2012 at 10am</strong>&#8221; which is independent of time zone you would do the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get a time zone independent Calendar instance</span>
<span style="color: #003399;">Calendar</span> calXmas <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the date December 25, 2012</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span>, <span style="color: #cc66cc;">2012</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span>, <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span>, <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the time 10am</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em><strong>Things to Note:</strong></em></p>
<ul>
<li>The <em>Calendar.MONTH</em> field is zero-based meaning January is &#8220;0&#8243; and December is &#8220;11&#8243;. This is not a mistake, just awkward if you&#8217;re not aware of it. You wonder why people hate this?
<li>The <em>Calendar.HOUR_OF_DAY</em> field is in 24 hours so the hour for 10am is &#8220;10&#8243; and the hour for 10pm is &#8220;22&#8243;.
</ul>
<p><a name="Operation4"></a><br />
<strong>4. Getting Fields From Calendar Object (Year, Month, Day, Hour, Minute, Second)</strong></p>
<p>If you have a Calendar object configured, you would access the various fields as follows:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Pre-configured Calendar object</span>
<span style="color: #003399;">Calendar</span> calEvent<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> year  <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> month <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Important! Zero-based!</span>
<span style="color: #000066; font-weight: bold;">int</span> day   <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> hour  <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> min   <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> sec   <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a name="Operation5"></a><br />
<strong>5. Date Arithmetic (Add/Subtract Values to a Calendar Object)</strong></p>
<p>You can easily add or subtracy values to a Calendar object if required. It is quite simple to do using the Calendar&#8217;s Add() method. You can use any of the above Calendar fields (e.g. YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE and SECOND). If you wish to subtract a value simply use a negative number.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// A pre-configured Calendar event</span>
<span style="color: #003399;">Calendar</span> calMyEvent<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add five days to the event</span>
calMyEvent.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span>, <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Subtract two hours from the event</span>
calMyEvent.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a name="Operation6"></a><br />
<strong>6. Create a Calendar Object in a Specific Time Zone</strong></p>
<p>You would need to do this if you want to convert between time zones. To create a Calendar object in a specific time zone you must know the time zone ID. This is typically the Olson name in the form <em>Area/Location</em>. For example to create a Calendar object representing the time in Los Angeles, America you would do the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Create a Calendar object in Los Angeles time</span>
<span style="color: #003399;">Calendar</span> calLosAngeles <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;America/Los_Angeles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a name="Operation7"></a><br />
<strong>7. Convert Between Time Zones</strong></p>
<p>This is a common and often frustrating situation. If you have an event in one time zone, how do you convert it to another one? It is surprisingly quite easy to do, you just need to remember to set the time zones on the Calendar object that you are converting from and to.</p>
<p>For example, if you want to find out what time Christmas in Australia is in UK time you would do the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Create a Calendar object of the event in Australia</span>
<span style="color: #003399;">Calendar</span> calXmasAUS <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Australia/Brisbane&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmasAUS.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span>, <span style="color: #cc66cc;">2012</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmasAUS.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span>, <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmasAUS.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span>, <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmasAUS.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmasAUS.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmasAUS.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a Calendar object in the UK time zone</span>
<span style="color: #003399;">Calendar</span> calUKTime <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Europe/London&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Convert the event from Australia to UK time</span>
calUKTime.<span style="color: #006633;">setTimeInMillis</span><span style="color: #009900;">&#40;</span>calXmasAUS.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above Calendar object for the event in Australia (calXmasAUS) would contain December 25, 2012 12am and the time zone would be GMT+10:00 Australia/Brisbane. Once you do the conversion to UK time, the object (calUKTime) contains December 24, 2012 2pm and the time zone is GMT+0:00 Europe/London.</p>
<p><a name="Operation8"></a><br />
<strong>8. How to Store the Date and Time in an SQLite Database?</strong></p>
<p>The easiest way to store the date and time in an SQLite database is to store the millisecond value of the Calendar object using the getTimeInMillis() method. This require a field on your database table to store a long value. If the time zone is important, I would have a second field in the database for storing the Olson name of the time zone (i.e. the time zone ID). You can then use both fields together when it comes to restoring the date and time in a Calendar object.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Create a Calendar object for December 25, 2012 10am (time zone independent)</span>
<span style="color: #003399;">Calendar</span> calXmas <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the date December 25, 2012</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span>, <span style="color: #cc66cc;">2012</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span>, <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_MONTH</span>, <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the time 10am</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
calXmas.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get a value for the date/time to write to the database</span>
<span style="color: #000066; font-weight: bold;">long</span> dbDateTimeValue <span style="color: #339933;">=</span> calXmas.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Store a time zone ID (i.e. Olson name) in the database also (if required)</span>
<span style="color: #003399;">String</span> dbTimeZoneValue <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Australia/Brisbane&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// -- Write Above Two Values To Database --</span></pre></td></tr></table></div>

<p>To restore the Calendar object once you have read the values from the database you would do the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">long</span> dbRestoredDateTime<span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> dbRestoredTimeZone<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// --Above Values Restored From SQLite Database --</span>
&nbsp;
<span style="color: #003399;">Calendar</span> calRestored<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set up the time zone on the Calendar object we are restoring to</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dbRestoredTimeZone <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    calRestored <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>dbRestoredTimeZone<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    calRestored <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Time Zone Independent</span>
    <span style="color: #666666; font-style: italic;">// calRestored = Calendar.getInstance(); // Current System Time Zone</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Restore the date and time</span>
calRestored.<span style="color: #006633;">setTimeInMillis</span><span style="color: #009900;">&#40;</span>dbRestoredDateTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Make sure that when you restore the date/time value from the SQLite database that you read it as a long value and not an int value as it will come out incorrectly.</p>
<p><a name="Operation9"></a><br />
<strong>9. Get the Time Between Two Dates</strong></p>
<p>You can work out the amount of time between two dates by converting both Calendar objects to millisecond values and subtracting them. You then need to convert the remaining millisecond value back to something legible as in the following example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Two pre-configured Calendar objects</span>
<span style="color: #003399;">Calendar</span> calEvent<span style="color: #339933;">;</span>
<span style="color: #003399;">Calendar</span> calNow<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the time between the dates in milliseconds</span>
<span style="color: #000066; font-weight: bold;">long</span> timeBetween <span style="color: #339933;">=</span> calEvent.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> calNow.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>timeBetween <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Event is in the past</span>
    <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Convert the millisecond result in to Days, Hours, Minutes and Seconds</span>
<span style="color: #000066; font-weight: bold;">long</span> timeConv <span style="color: #339933;">=</span> timeBetween <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">long</span> seconds <span style="color: #339933;">=</span> timeConv <span style="color: #339933;">%</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>
timeConv <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> minutes <span style="color: #339933;">=</span> timeConv <span style="color: #339933;">%</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>
timeConv <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> hours <span style="color: #339933;">=</span> timeConv <span style="color: #339933;">%</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
timeConv <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> days <span style="color: #339933;">=</span> timeConv<span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=196</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android: How to Create a Boot Receiver</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=187</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=187#comments</comments>
		<pubDate>Fri, 24 Feb 2012 15:24:24 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Boot]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Receiver]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=187</guid>
		<description><![CDATA[Sample Code: Click Here A boot receiver class is useful when you wish to run a piece of code or start a service as soon as the device has finished booting up. This is very simple to implement requiring only a class that derives from BroadcastReceiver and some configuration in the AndroidManifest.xml file. There is [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Sample Code: Click <a href="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2012/02/Boot_Receiver.zip">Here</a></strong></p>
<p>A boot receiver class is useful when you wish to run a piece of code or start a service as soon as the device has finished booting up. This is very simple to implement requiring only a class that derives from BroadcastReceiver and some configuration in the <em>AndroidManifest.xml</em> file.</p>
<p>There is a link to a sample Eclipse project at the top of this article. Below I examine the main elements of creating a boot receiver class. Lets start by looking at the <em>AndroidManifest.xml</em> file:</p>
<p><strong>AndroidManifest.xml</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;com.bytefoundry.BootReceiver&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:versionCode</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:versionName</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-sdk</span> <span style="color: #000066;">android:minSdkVersion</span>=<span style="color: #ff0000;">&quot;7&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/ic_launcher&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.BootReceiver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.action.BOOT_COMPLETED&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.Boot_ReceiverActivity&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.action.MAIN&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.category.LAUNCHER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This is where the majority of the configuration for a boot receiver takes place. The first thing to note is near the top of the file. You can see that you must declare using the &#8220;<em>android.permission.RECEIVE_BOOT_COMPLETED</em>&#8221; permission. Without this your application will not be notified on boot up and may crash or otherwise fail.</p>
<p><span id="more-187"></span></p>
<p>The second, important thing to note is the &lt;receiver&gt; element. This points to the class that derives from BroadcastReceiver (in this case I have called the class BootReceiver). There is also the inclusion of the BOOT_COMPLETED action in the corresponding intent filter. This is the standard Android broadcast action which we need to detect in order to execute code after boot up.</p>
<p><strong>BootReceiver.java (Received the BOOT_COMPLETED broadcast)</strong></p>

<div class="wp_syntax"><table><tr><td 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> BootReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Boot intent action name</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> BOOT_ACTION_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;android.intent.action.BOOT_COMPLETED&quot;</span><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> 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>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>BOOT_ACTION_NAME.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// ToDo: Whatever I need at immediately after bootstrap</span>
            Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span>context, <span style="color: #0000ff;">&quot;This message received on boot!&quot;</span>, Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is the class which derives from BroadcastReceiver and receives the BOOT_COMPLETED system broadcast. You can see that it simply overrides the onReceive() method and checks the broadcast intent action name it has received. If the action is equal to &#8220;android.intent.action.BOOT_COMPLETED&#8221;, it then executes whatever code you want. In this case I simply display a temporary message on the screen using Toast.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=187</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Android: How to Use the Alarm Manager Service</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=140</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=140#comments</comments>
		<pubDate>Mon, 20 Feb 2012 13:37:12 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[AlarmManager]]></category>
		<category><![CDATA[Dialog]]></category>
		<category><![CDATA[Popup]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=140</guid>
		<description><![CDATA[Sample Code: Click Here The AlarmManager is a useful system service that allows you to schedule when you want to have your application code run at a certain time. To use this service you need to set an alarm in the form of a PendingIntent and the time (in milliseconds) when you want it to go [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Sample Code: Click <a href="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2012/02/Alarm_Manager.zip">Here</a></strong></p>
<p>The <a title="AlarmManager" href="http://developer.android.com/reference/android/app/AlarmManager.html" target="_blank">AlarmManager</a> is a useful system service that allows you to schedule when you want to have your application code run at a certain time. To use this service you need to set an alarm in the form of a <a title="PendingIntent" href="http://developer.android.com/reference/android/app/PendingIntent.html" target="_blank">PendingIntent</a> and the time (in milliseconds) when you want it to go off. You also have the ability to set up a repeating alarm by specifying the interval in milliseconds of which the alarm should repeat.</p>
<p>When you set alarms using the AlarmManager it must be an intent broadcast. This means you will need a class that derives from <a title="BroadcastReceiver" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" target="_blank">BroadcastReceiver</a> in order to handle the messages. This is defined in the <em>AndroidManifest.xml</em> file using the &lt;receiver&gt; tag. Inside this tag you must also define a custom action which will be used when instantiating the broadcast intent. This is used to distinguish your alert from all the other alerts flying around your device.</p>
<p><span id="more-140"></span></p>
<p>I have written an example below that demonstrates how to use the AlarmManager service. When you push the button in the main activity an alarm is scheduled for 10 seconds in the future. Once this time is reached a floating, popup dialog box appears on the screen informing you of the alarm ID that has been fired. The floating popup is an activity which implements the <a title="AlertDialog" href="http://developer.android.com/guide/topics/ui/dialogs.html" target="_blank">AlertDialog</a> functionality. There is a link to a sample Eclipse project at the top of this article.</p>
<p><strong>AndroidManifest.xml</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;com.bytefoundry.AlarmManager&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:versionCode</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:versionName</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-sdk</span> <span style="color: #000066;">android:minSdkVersion</span>=<span style="color: #ff0000;">&quot;7&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/ic_launcher&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.AlarmReceiver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.bytefoundry.broadcast.ALARM&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.BFAlarmMgr&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.action.MAIN&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.category.LAUNCHER&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.AlarmPopUp&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:theme</span>=<span style="color: #ff0000;">&quot;@style/Theme_Transparent&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:launchMode</span>=<span style="color: #ff0000;">&quot;singleInstance&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:taskAffinity</span>=<span style="color: #ff0000;">&quot;&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:excludeFromRecents</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Above is the manifest for the test application. Note the &lt;receiver&gt; element which determines the class that handles our alarm broadcast messages. This is the class which is derived from BroadcastReceiver. Within the &lt;receiver&gt; element is also the definition of a custom action for our alarm alerts. This is used when creating the broadcast intents. It is a good idea to name your custom actions with a fully qualified name.</p>
<p>Finally, note the definition of the AlarmPopUp activity. This is how to define a popup dialog box that is disconnected from your application.</p>
<p><strong>BFAlarmMgr.java (Primary Activity &#8211; Setting the Alarms)</strong></p>

<div class="wp_syntax"><table><tr><td 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> BFAlarmMgr <span style="color: #000000; font-weight: bold;">extends</span> Activity
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// The alarm action defined in AndroidManifest.xml</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> ALARM_ACTION_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.bytefoundry.broadcast.ALARM&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Handle for the AlarmManager service</span>
    <span style="color: #000000; font-weight: bold;">private</span> AlarmManager m_alarmMgr<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// The UI text field</span>
    <span style="color: #000000; font-weight: bold;">private</span> TextView m_tvAlarmText<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// An ID for the alarm (used for the PendingIntent request code)</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> m_alarmId<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>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Set the main content view</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;
        <span style="color: #666666; font-style: italic;">// Set the default for the alarm ID</span>
        m_alarmId <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Get a handle to the AlarmManager service</span>
        m_alarmMgr <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><span style="color: #003399;">Context</span>.<span style="color: #006633;">ALARM_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Get the UI text view control</span>
        m_tvAlarmText <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;">tvAlarmText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Hook up an event for the button to set the alarm</span>
        <span style="color: #003399;">Button</span> alarmButton <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">btnSetAlarm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        alarmButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">View</span>.<span style="color: #006633;">OnClickListener</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> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Create an alarm for 10 seconds in the future</span>
                createAlarm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// Update the text field</span>
                m_tvAlarmText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Alarm with ID &quot;</span> <span style="color: #339933;">+</span> m_alarmId <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; has been set.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// Increment the alarm ID</span>
                m_alarmId<span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> createAlarm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Create an alarm intent</span>
        Intent alarmIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>ALARM_ACTION_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Set the Alarm ID as extra data to be displayed in the popup</span>
        alarmIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AlarmID&quot;</span>, m_alarmId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Create the corresponding PendingIntent object</span>
        PendingIntent alarmPI <span style="color: #339933;">=</span> PendingIntent.<span style="color: #006633;">getBroadcast</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, m_alarmId, alarmIntent, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Use a Calendar object to create the alarm time 10 seconds in the future</span>
        <span style="color: #003399;">Calendar</span> calAlarm <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        calAlarm.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Register the alarm with the alarm manager</span>
        m_alarmMgr.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>AlarmManager.<span style="color: #006633;">RTC</span>, calAlarm.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, alarmPI<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is the main activity of the test application. It is fairly straight-forward. When you press the button an alarm is registered with the AlarmManager service. In the onCreate() method we get a handle to the AlarmManager service and register a callback event for the button.  When the button is pressed the createAlarm() method is called which contains everything you need to do to register an alarm with the AlarmManager service.</p>
<p><strong>AlarmReceiver.java (Broadcast Receiver &#8211; Handles the Alarm Messages)</strong></p>

<div class="wp_syntax"><table><tr><td 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> AlarmReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// The alarm action defined in AndroidManifest.xml</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> ALARM_ACTION_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.bytefoundry.broadcast.ALARM&quot;</span><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> 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>
        <span style="color: #666666; font-style: italic;">// Handle the alarm broadcast</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ALARM_ACTION_NAME.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Launch the alarm popup dialog</span>
            Intent alarmIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;android.intent.action.MAIN&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            alarmIntent.<span style="color: #006633;">setClass</span><span style="color: #009900;">&#40;</span>context, AlarmPopUp.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            alarmIntent.<span style="color: #006633;">setFlags</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">FLAG_ACTIVITY_NEW_TASK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Pass on the alarm ID as extra data</span>
            alarmIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AlarmID&quot;</span>, intent.<span style="color: #006633;">getIntExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AlarmID&quot;</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Start the popup activity</span>
            context.<span style="color: #006633;">startActivity</span><span style="color: #009900;">&#40;</span>alarmIntent<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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is the class which is defined in the manifest as a receiver and is derived from the BroadcastReceiver class. You can see it implements the onReceive() method and checks the broadcast intent to see if the action is equal to our custom alarm action (also defined in the manifest). If the received message is one of our alarms then an instance of the popup activity is started.</p>
<p><strong>AlarmPopUp.java (Popup Activity &#8211; Fired When Alarm Goes Off)</strong></p>

<div class="wp_syntax"><table><tr><td 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> AlarmPopUp <span style="color: #000000; font-weight: bold;">extends</span> Activity
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// An ID of the alarm dialog</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: #000066; font-weight: bold;">int</span> DIALOG_ALARM <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// The alarm ID</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> m_alarmId<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>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Get the alarm ID from the intent extra data</span>
        Intent intent <span style="color: #339933;">=</span> getIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Bundle extras <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>extras <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>
            m_alarmId <span style="color: #339933;">=</span> extras.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AlarmID&quot;</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            m_alarmId <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Show the popup dialog</span>
        showDialog<span style="color: #009900;">&#40;</span>DIALOG_ALARM<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;">protected</span> <span style="color: #003399;">Dialog</span> onCreateDialog<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<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;">onCreateDialog</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Build the dialog</span>
        AlertDialog.<span style="color: #006633;">Builder</span> alert <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AlertDialog.<span style="color: #006633;">Builder</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        alert.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ALARM MANAGER&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        alert.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Its time for the alarm with ID: &quot;</span> <span style="color: #339933;">+</span> m_alarmId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        alert.<span style="color: #006633;">setCancelable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        alert.<span style="color: #006633;">setPositiveButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dismiss&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> DialogInterface.<span style="color: #006633;">OnClickListener</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> onClick<span style="color: #009900;">&#40;</span>DialogInterface dialog, <span style="color: #000066; font-weight: bold;">int</span> whichButton<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                AlarmPopUp.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">finish</span><span style="color: #009900;">&#40;</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Create and return the dialog</span>
        AlertDialog dlg <span style="color: #339933;">=</span> alert.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> dlg<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is a simple popup activity class. It simply checks the intent extras for the alarm ID and then builds and displays an AlertDialog box using the ID in it&#8217;s message. This popup should appear when an alarm activates as long as there is not another instance of the popup already active.</p>
<p>Finally, please note that alarms set in the AlarmManager service are volatile and they will disappear if the device is restarted. To combat this you would need to define a boot receiver class that gets informed when the device boots up. From within here you would need to re-register your alarms. A future article on defining a boot receiver is forthcoming (found <a href="http://www.bytefoundry.co.uk/blog/?p=187" alt="How to Create a Boot Receiver">here</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=140</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Alfresco 4: Restoring From a Cold Backup (Windows)</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=114</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=114#comments</comments>
		<pubDate>Fri, 20 Jan 2012 16:24:32 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Alfresco]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Cold]]></category>
		<category><![CDATA[Restore]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=114</guid>
		<description><![CDATA[For the sake of this article, assume Alfresco 4 has been installed to C:\Alfresco (the application directory) and that the dir.root directory is C:\Alfresco\alf_data. The name of the database being used is simply &#8220;alfresco&#8220;. This article assumes you have been following the Alfresco cold backup procedure documented in the following Alfresco Wiki page. Note that [...]]]></description>
			<content:encoded><![CDATA[<p>For the sake of this article, assume Alfresco 4 has been installed to <strong>C:\Alfresco</strong> (the application directory) and that the <em>dir.root</em> directory is <strong>C:\Alfresco\alf_data</strong>. The name of the database being used is simply &#8220;<strong>alfresco</strong>&#8220;.</p>
<p>This article assumes you have been following the Alfresco cold backup procedure documented in the following Alfresco Wiki page. Note that it is essential that you are keeping a dump of the Postgresql database along with a copy of the <em>dir.root </em>data directory from the point in time of the database dump.</p>
<p><a href="http://wiki.alfresco.com/wiki/Backup_and_Restore">http://wiki.alfresco.com/wiki/Backup_and_Restore</a></p>
<p>There are a couple of changes to the restoration procedure on that Wiki page when it comes to Alfresco 4. This is mostly down to the fact that it uses the Postgresql database platform. You may have noticed that the database files are stored in <em>&#8216;dir.root/postgresql&#8217;</em>  directory which complicates things a little? OK, the restoration procedure is as follows:</p>
<p><span id="more-114"></span></p>
<p><strong>1.  Stop the Alfresco services</strong></p>
<ul>
<li>This is easily done through the Start menu (START &#8211;&gt; Programs &#8211;&gt; Alfresco Community &#8211;&gt; Service &#8211;&gt; Stop).</li>
<li>You can also stop both services using the &#8220;services.msc&#8221; snapin (<em>alfrescoPostgreSQL</em> and <em>alfrescoTomcat</em>) .</li>
</ul>
<p><strong>2.  Start the Postgresql Database On It&#8217;s Own</strong></p>
<ul>
<li>You need to start the Postgresql database platform on its own. This is harder to do than you might think and running the <em>&#8216;postgresql/scripts/servicerun.bat&#8217; </em>file from the application directory does nothing.</li>
<li>Open a command prompt (START &#8211;&gt; Run &#8211;&gt; cmd)</li>
<li>Change in to the Postgresql/bin folder in the application directory (e.g. <strong>cd C:\Alfresco\postgresql\bin</strong>).</li>
<li>Issue the command: <em>pg_ctl start -D [Database Data] </em>in this case <strong>pg_ctl start -D C:\Alfresco\alf_data\postgresql </strong>to start the Postgresql database server on its own.</li>
</ul>
<p><strong>3.  Drop the existing database</strong></p>
<ul>
<li>You need to drop the existing copy of the database. You can do so by issuing the following command at the command prompt:  <em>dropdb -U [DBUser] [DBName]</em>  in this case <strong>dropdb -U alfresco alfresco</strong></li>
</ul>
<p><strong>4.  Restore the Database</strong></p>
<ul>
<li>Create and restore the Postgresql database at the same time by issuing the following command at the command prompt:  <em>pg_restore -U postgres -C -d postgres [Path Dump File] </em>in this case <strong>pg_restore -U postgres -C -d postgres C:\backup\19-01-2012\alfresco.bak.</strong></li>
<li>Don&#8217;t worry about the fact that the database (-d) is postgres. This just needs to be a pre-existing database name that exists in the cluster. When you are restoring using the -C parameter, the data is restore to the database name that exists in the dump file (<a href="http://www.postgresql.org/docs/8.3/static/app-pgrestore.html">http://www.postgresql.org/docs/8.3/static/app-pgrestore.html</a>).</li>
<li>The user needs to have administrative rights to create databases. That is why the postgres account is being used (-U postgres). You will need to know the password you set on this account.</li>
</ul>
<p><strong>5.  Stop the Postgresql Database Again</strong></p>
<ul>
<li>Issue the command: <em>pg_ctl stop -D [Database Data] </em>in this case <strong>pg_ctl stop -D C:\Alfresco\alf_data\postgresql </strong>to stop the Postgresql database server.</li>
</ul>
<p><strong>6.  Restore the Data in <em>dir.root</em></strong></p>
<ul>
<li>Finally, you need to restore the data in the <em>dir.root</em> directory. While the Wiki article says to delete everything in <em>dir.root</em>, you must in fact leave the &#8216;postgresql&#8217; directory for Alfresco 4. This is because it contains the database files which you just restored.</li>
<li>As per the image below, delete the highlighted folders and leave the &#8216;postgresql&#8217; directory. These are the exact same folders that you should restore from your backup.</li>
<li style="text-align: left;">Just to emphasise the point, do not overwrite the <em>dir.root/postgresql</em> directory with the one from your backup. Just restore the other folders instead.</li>
</ul>
<div id="attachment_117" class="wp-caption aligncenter" style="width: 526px"><a href="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2012/01/dirRoot.jpg"><img class=" wp-image-117" title="Alfresco Restore Folders" src="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2012/01/dirRoot.jpg" alt="Alfresco Restore Folders" width="516" height="340" /></a><p class="wp-caption-text">Fig 1: Folders to delete / retore.</p></div>
<p style="text-align: left;"> <strong>7.  Start the Alfresco Services</strong></p>
<ul>
<li>As per before, just restart the Alfresco services through the Start menu.</li>
</ul>
<p><strong>Note :- Broken Searches/Dashlets</strong></p>
<p>It has been brought to my attention that if you restore your Alfresco 4 installation to a different root you will likely experience problems with broken search functionality and errors with dashlets (e.g. a 500 error on the user dashboard). This appears to be because of a hard reference in the Solr config files to the installation directory.</p>
<p>If you restore to a different root you will need to edit the following files and update the paths accordingly:</p>
<p>\alf_data\solr\workspace-SpacesStore\conf\solrcore.properties<br />
\alf_data\solr\archive-SpacesStore\conf\solrcore.properties</p>
<p>Thank you for your input, Chris!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=114</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Alfresco: Change Company Home Name</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=108</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=108#comments</comments>
		<pubDate>Thu, 01 Dec 2011 11:52:23 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Alfresco]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=108</guid>
		<description><![CDATA[To change the &#8220;Company Home&#8221; name in Alfresco Explorer you must perform the following configuration: Stop the Alfresco service. Find the appropriate webclient properties file for your language. For example, to change this setting for English (US) you would open and edit the following file in a text editor:  &#8220;tomcat/webapps/alfresco/WEB-INF/classes/alfresco/messages/webclient_en_US.properties&#8221; You will see this file [...]]]></description>
			<content:encoded><![CDATA[<p>To change the &#8220;Company Home&#8221; name in Alfresco Explorer you must perform the following configuration:</p>
<ul>
<li>Stop the Alfresco service.</li>
<li>Find the appropriate webclient properties file for your language. For example, to change this setting for English (US) you would open and edit the following file in a text editor: 
<p>&#8220;<em>tomcat/webapps/alfresco/WEB-INF/classes/alfresco/messages/webclient_en_US.properties&#8221;<br />
</em></li>
<li>You will see this file containts a list of key/value pairs. Do a search for &#8220;Company home&#8221; and edit the value accordingly.</li>
<li>Restart Alfresco.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=108</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enable Versioning For All Documents In Alfresco 4</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=102</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=102#comments</comments>
		<pubDate>Thu, 01 Dec 2011 11:18:33 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Alfresco]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=102</guid>
		<description><![CDATA[Version of Alfresco used:  4.0b The following steps show how to enable version control for all documents in Alfresco 4. In a nutshell, you are essentially adding the &#8220;Versioning&#8221; aspect as a mandatory part of the cm:content property (of which all content is derived). Step 1:  Edit &#8220;contentModel.xml&#8221; File Open the &#8220;contentModel.xml&#8221; configuration file in [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Version of Alfresco used:  4.0b</strong></p>
<p>The following steps show how to enable version control for all documents in Alfresco 4. In a nutshell, you are essentially adding the &#8220;Versioning&#8221; aspect as a mandatory part of the <em>cm:content</em> property (of which all content is derived).</p>
<p><strong>Step 1:  Edit &#8220;contentModel.xml&#8221; File</strong></p>
<ul>
<li>Open the &#8220;contentModel.xml&#8221; configuration file in a text editor. This is found in &#8220;<em>tomcat/webapps/alfresco/WEB-INF/classes/alfresco/model&#8221;.</em></li>
<li>Note that in previous versions of Alfresco this file was called &#8220;<em>content.xml</em>&#8220;.</li>
</ul>
<p><strong>Step 2:  Add in the Versionable Aspect</strong></p>
<ul>
<li>Find the &#8220;<em>cm:content&#8221;</em> type. This starts on line 83. In Windows, do CTRL+F and search for &#8220;cm:content&#8221;.</li>
<li>After the closing &#8220;<em>&lt;/properties&gt;&#8221; </em>tag, add in the following text:</li>
</ul>
<blockquote><p>&lt;mandatory-aspects&gt;<br />
&lt;aspect&gt;cm:versionable&lt;/aspect&gt;<br />
&lt;/mandatory-aspects&gt;</p></blockquote>
<ul>
<li>Restart the Alfresco service.</li>
</ul>
<p>To verify this has worked, add a new document to the system and check its properties. Its &#8220;Version History&#8221; block at the bottom of the properties screen should already be enabled.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=102</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Espionage Development Video 02</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=94</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=94#comments</comments>
		<pubDate>Tue, 01 Nov 2011 14:51:07 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Espionage]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=94</guid>
		<description><![CDATA[Since the first video I have implemented more goal behaviours (specifically Fight Opponent and Escape Complex) for the AI. Once an agent has the four items it will make its was to the exit and complete the game. It can get the items from the containers or from another agent if he beats it when [...]]]></description>
			<content:encoded><![CDATA[<p>Since the first video I have implemented more goal behaviours (specifically Fight Opponent and Escape Complex) for the AI. Once an agent has the four items it will make its was to the exit and complete the game. It can get the items from the containers or from another agent if he beats it when fighting.</p>
<p>This short video demonstrated a four room complex with two AI agents.</p>
<p>My next objective is to implement the player object and HUD so that the game can actually be played and interacted with rather than watching the AI entites fight it out. I also intend to tweak the goal evaluation/selection behaviour of the AI entities.</p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/gLM1IdbJhIk" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=94</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working With Fonts In Unity3D</title>
		<link>http://www.bytefoundry.co.uk/blog/?p=70</link>
		<comments>http://www.bytefoundry.co.uk/blog/?p=70#comments</comments>
		<pubDate>Wed, 26 Oct 2011 21:17:08 +0000</pubDate>
		<dc:creator>Raymond</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Color]]></category>
		<category><![CDATA[Font Size]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[GUIStyle]]></category>
		<category><![CDATA[GUIText]]></category>
		<category><![CDATA[Style]]></category>

		<guid isPermaLink="false">http://www.bytefoundry.co.uk/blog/?p=70</guid>
		<description><![CDATA[OK, so you&#8217;re sick of the default font in Unity3D and want to use something different in your game? How do you go about using fonts in a script with C#? How do you change the font colour? How do you make it bold and change the font size? I recently came across this issue when trying to output scrolling [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so you&#8217;re sick of the default font in Unity3D and want to use something different in your game? How do you go about using fonts in a script with C#? How do you change the font colour? How do you make it bold and change the font size? I recently came across this issue when trying to output scrolling text above the head of my game&#8217;s protagonist.</p>
<p>I was hoping for a simple way of creating a font, something like:</p>
<p><em>new Font(&#8220;Arial&#8221;, 16, FontStyle.Bold, Color.Red);</em></p>
<p>It turned out to be a surprisingly convoluted process involving importing a True-Type Font file (*.ttf) in to the editor and loading it as a resource before assigning it to a GUIText component.</p>
<p><strong>1. Importing a True Type Font in to Unity3D</strong></p>
<p>This is the easy bit. Simply create a sub-folder in &#8220;Resources&#8221; for storing your fonts (i.e. &#8220;Resources/Fonts&#8221;). You then drag a TTF file from your computer in to this folder and Unity will automatically import it for you. You can get free TTF files online and also from your PC (e.g. C:\Windows\Fonts).</p>
<p>Say you click and drag the &#8220;Broadway.ttf&#8221; file in to the &#8220;Fonts&#8221; folder you created in Unity. Once you have a font file imported in to Unity you can load it in a C# script as follows:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">Font BroadwayFont <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Font<span style="color: #008000;">&#41;</span>Resources<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Fonts/Broadway&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>2. Using the Font with GameObject and GUIText</strong></p>
<p>The first thing to realise here is that although you can now associate your font with a GUIText object, you must associate the GUIText object with a GameObject (or derived subclass). This makes sense when you think about it. Everything in Unity is derived from a GameObject. A GameObject contains the transformation information etc. GUIText is actually a component of GameObject and is instantiated as in the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Create an instance of GameObject for the text</span>
GameObject TextObject <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GameObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Add a GUIText component to the above GameObject (cast in to GUIText</span>
<span style="color: #008080; font-style: italic;">// reference)</span>
GUIText GText <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>GUIText<span style="color: #008000;">&#41;</span>TextObject<span style="color: #008000;">.</span><span style="color: #0000FF;">AddComponent</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>GUIText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>You can then associate the font and set other parameters of the GUIText accordingly:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Set text, font and default properties of the text item</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Test Text!&quot;</span><span style="color: #008000;">;</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">font</span> <span style="color: #008000;">=</span> BroadwayFont<span style="color: #008000;">;</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">material</span><span style="color: #008000;">.</span><span style="color: #0000FF;">color</span> <span style="color: #008000;">=</span> Color<span style="color: #008000;">.</span><span style="color: #0000FF;">white</span><span style="color: #008000;">;</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">=</span> Vector3<span style="color: #008000;">.</span><span style="color: #0000FF;">zero</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>3. Changing the Font Size, Color and Style</strong></p>
<p>Assuming your GUIText component is set up as above, you can then change the font size, color and style (i.e. bold, italic or both) through a C# script too. It is simply a case of changing the properties in the GUIText component:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Change the font size to 48 points</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">fontSize</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">48</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Make the font bold</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">fontStyle</span> <span style="color: #008000;">=</span> FontStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">Bold</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Change the font color to Green</span>
GText<span style="color: #008000;">.</span><span style="color: #0000FF;">material</span><span style="color: #008000;">.</span><span style="color: #0000FF;">color</span> <span style="color: #008000;">=</span> Color<span style="color: #008000;">.</span><span style="color: #0000FF;">Green</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>4. Setting the Position of the Text</strong></p>
<p>Finally the position of the text would usually be set in screen space (i.e. on the X and Y axis). Unity uses normalised screen space coordinates where the top-left is (0,0) and the bottom-right is (1,1). Say your screen resolution is 800&#215;600 and you want to put some text starting at the centre of the screen (i.e. at position (400,300))? The positions would be calculated as follows:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">XPos <span style="color: #008000;">=</span> 400<span style="color: #008000;">.</span>0f <span style="color: #008000;">/</span> Screen<span style="color: #008000;">.</span><span style="color: #0000FF;">width</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">// 0.5f</span>
YPos <span style="color: #008000;">=</span> 300<span style="color: #008000;">.</span>0f <span style="color: #008000;">/</span> Screen<span style="color: #008000;">.</span><span style="color: #0000FF;">height</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Also 0.5f</span></pre></td></tr></table></div>

<p>And you set the position as follows:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">GText<span style="color: #008000;">.</span><span style="color: #0000FF;">transform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">position</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Vector3<span style="color: #008000;">&#40;</span>XPos, YPos, 0<span style="color: #008000;">.</span>0f<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>5. Calculating the Text Width and Height</strong></p>
<p>How can you calculate the text width and height using your font? GUIStyle comes to the rescue here with it&#8217;s CalcSize() method. You have to instantiate a GUIStyle object, set the appropriate Font, Size and FontStyle parameters and then call CalcSize() as per below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">GUIStyle GStyle <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GUIStyle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Set the relevant style parameters for calculating size</span>
GStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">font</span> <span style="color: #008000;">=</span> BroadwayFont<span style="color: #008000;">;</span>
GStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">fontSize</span> <span style="color: #008000;">=</span> GText<span style="color: #008000;">.</span><span style="color: #0000FF;">fontSize</span><span style="color: #008000;">;</span>
GStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">fontStyle</span> <span style="color: #008000;">=</span> GText<span style="color: #008000;">.</span><span style="color: #0000FF;">fontStyle</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Calculate the text width and height</span>
<span style="color: #6666cc; font-weight: bold;">float</span> TextWidth <span style="color: #008000;">=</span> GStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">CalcSize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> GUIContent<span style="color: #008000;">&#40;</span>GText<span style="color: #008000;">.</span><span style="color: #0000FF;">text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">x</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">float</span> TextHeight <span style="color: #008000;">=</span> GStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">CalcSize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> GUIContent<span style="color: #008000;">&#40;</span>GText<span style="color: #008000;">.</span><span style="color: #0000FF;">text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">y</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Linked below is a class I wrote called CTextItem. It is a wrapper class around a GameObject with GUIText component and has methods for adjusting text, color, font size, style and position. The position can be set in screen space or world space (for example if you wish to make text appear above an object&#8217;s head in your game world). It assumes you know how it import TTF files in to your project and load a font resource as per above. I hope you find it useful.</p>
<p><a href="http://www.bytefoundry.co.uk/blog/wp-content/uploads/2011/10/CTextItem.zip">CTextItem</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bytefoundry.co.uk/blog/?feed=rss2&#038;p=70</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
