<?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>AndroidDev &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/tag/androiddev/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Wed, 08 Apr 2015 17:50:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.1</generator>
	<item>
		<title>The Hitchhiker&#8217;s guide to Android Testing, part 2: Espresso</title>
		<link>http://wiebe-elsinga.com/blog/the-hitchhikers-guide-to-android-testing-part-2-espresso/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 03 Apr 2015 02:04:02 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1711</guid>

					<description><![CDATA[As mentioned in the first article Ali Derbane and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called Espresso. So now you know a bit more about Espresso. If you want to know more, you may want to visit: Espresso website Github repo corresponding to this article General Espresso Github samples by Google Updated (08-04) When running the tests with gradle you can just use gradle connectedAndroidTest]]></description>
										<content:encoded><![CDATA[<p>As mentioned in the <a href="http://www.technotalkative.com/part-10-the-hitchhikers-guide-to-android-testing/">first article</a> <a href="https://plus.google.com/+AliDerbane">Ali Derbane</a> and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called <a href="https://code.google.com/p/android-test-kit/">Espresso</a>.<br />
<span id="more-1711"></span><br />
<img src="http://i.giphy.com/VtFd68Pr19fYk.gif" width=100%" /></p>
<h3>Introduction</h3>
<p>Introduced at the GTAC in 2013, Espresso is designed to be used in environments where the developers write their own tests, and makes it possible to write concise, beautiful, and reliable Android UI tests quickly.<br />
Espresso has several general components:</p>
<ul>
<li>The <code class="codecolorer bash default"><span class="bash">Espresso</span></code> class offers the <code class="codecolorer bash default"><span class="bash">onView</span></code> and <code class="codecolorer bash default"><span class="bash">onData</span></code> methods which, alone, can be used for a good numbers of possible tests on a given interface.</li>
<li><code class="codecolorer bash default"><span class="bash">ViewMatchers</span></code> contains a collection of objects that implements the interface <code class="codecolorer bash default"><span class="bash">Matcher <span style="color: #000000; font-weight: bold;">&lt;</span>? super View<span style="color: #000000; font-weight: bold;">&gt;</span></span></code>. Using this Class you can collect and check View elements. For example, getting a View element (Button) with text &#8220;7&#8221;.</li>
<li><code class="codecolorer bash default"><span class="bash">ViewActions</span></code> contains a collection of <code class="codecolorer bash default"><span class="bash">viewAction</span></code> objects to perform actions on a view. These actions are passed to the method <code class="codecolorer bash default"><span class="bash">ViewInteraction.perform</span></code> and may contain multiple actions. For example, clicking on a View element (Button).</li>
<li><code class="codecolorer bash default"><span class="bash">ViewAssertions</span></code> contains a collection of <code class="codecolorer bash default"><span class="bash">ViewAssertion</span></code> to conduct checks on views. </li>
</ul>
<p>
To illustrate these components a test can look like this:</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; Espresso.<span style="color: #006633;">onView</span><span style="color: #009900;">&#40;</span>ViewMatchers.<span style="color: #006633;">withText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>ViewActions.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Espresso.<span style="color: #006633;">onView</span><span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>ViewAssertions.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span>ViewMatchers.<span style="color: #006633;">withText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;42&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And the good news, as of last year Google has introduced a <a href="https://developer.android.com/tools/support-library/index.html">Testing Support Library</a> containing Espresso. So lets start by implementing Espresso.</p>
<div style="background: lightgray; padding: 12pt;">To illustrate, we are going to write some tests that tests agains a <a href="https://github.com/welsinga/sample_espresso/app">Android calculator application</a>. The common test scenario we will be implementing is testing if <em>‘6’ x ‘7’ equals ‘42’</em>.</div>
<p>&nbsp;&nbsp;</p>
<h3>Define the test runner</h3>
<p>To use Espresso we first need to define who is running the tests. Espresso uses a new runner named <code class="codecolorer bash default"><span class="bash">AndroidJUnitRunner</span></code>. This runner, based on <code class="codecolorer bash default"><span class="bash">InstrumentationTestRunner</span></code> and <code class="codecolorer bash default"><span class="bash">GoogleInstrumentationTestRunner</span></code>, runs <em>JUnit3</em> and <em>JUnit4</em> tests against your Android application.</p>
<p>First add the dependencies to your <code class="codecolorer bash blackboard"><span class="bash">build.gradle</span></code>, assuming you have installed the <a href="https://developer.android.com/tools/support-library/index.html">Testing Support Library</a>.</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">dependencies <span style="color: #66cc66;">&#123;</span><br />
&nbsp; androidTestCompile <span style="color: #ff0000;">'com.android.support.test:testing-support-lib:0.1'</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Then add the runner in your build.gradle<code class="codecolorer groovy default"><span class="groovy">android.<span style="color: #006600;">defaultConfig</span></span></code> configuration:</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">defaultConfig <span style="color: #66cc66;">&#123;</span><br />
&nbsp; ...<br />
&nbsp; <span style="color: #006600;">testInstrumentationRunner</span> <span style="color: #ff0000;">&quot;android.support.test.runner.AndroidJUnitRunner&quot;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>&nbsp;&nbsp;</p>
<h3>Writing the test</h3>
<p>As you may know test classes must be in <code class="codecolorer bash default"><span class="bash">src\androidTest\com.example.package.tests</span></code>, com.example.package being the package specified in the package attribute of the manifest element in the <code class="codecolorer bash default"><span class="bash">AndroidManifest</span></code> file.&nbsp;<br />
Also each test class must extend the abstract class <code class="codecolorer bash default"><span class="bash">ActivityInstrumentationTestCase2</span></code> and supply the Test Activity as generic type that will be used by default for testing.<br />
It must also be passed to the superclass via the <code class="codecolorer bash default"><span class="bash">super<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></span></code>. To make the Test Activity being called by the test framework, simply define a setup which calls the synchronous method <code class="codecolorer bash default"><span class="bash">getActivity<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.</span></code></p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionalInstrumentationTest <span style="color: #000000; font-weight: bold;">extends</span> ActivityInstrumentationTestCase2<span style="color: #339933;">&lt;</span>ActivityToTest<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> FunctionalInstrumentationTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>ActivityToTest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>As mentioned we want to check if <em>‘6’ x ‘7’ equals ‘42’</em>.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAnswer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;×&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">resText</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;42&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>You may have noticed, this example is using static imports. This is solely done to make the code more readable.</p>
<p>Other actions you want to use are:</p>
<ul>
<li><code class="codecolorer java default"><span class="java">pressBack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to simulate the use of the &#8220;back&#8221; button,</li>
<li><code class="codecolorer java default"><span class="java">isDisplayed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to check if an element is being shown and </li>
<li><code class="codecolorer java default"><span class="java">scrollTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to scroll to an element.</li>
</ul>
<p>&nbsp;&nbsp;</p>
<h3>Running the test</h3>
<p>Now lets do the fun part, lets run the test. This can be done from the command line with he command line with <code class="codecolorer bash blackboard"><span class="bash">gradle connectedAndroidTest</span></code> or using Android Studio by:</p>
<ol>
<li><em>Open Run menu | Edit Configurations</em></li>
<li><em>Add a new Android Tests configuration</em></li>
<li><em>Choose the module you are testing</em></li>
<li><em>Define our test runner: <code class="codecolorer bash default"><span class="bash">android.support.test.runner.AndroidJUnitRunner</span></code></em></li>
</ol>
<p>	<br />
<img src="http://i.giphy.com/10OjZwNlstPj9e.gif" width=100%"/><br />
<br />	<br />
So now you know a bit more about Espresso. If you want to know more, you may want to visit:</p>
<ul>
<li><a href="https://code.google.com/p/android-test-kit/">Espresso website</a></li>
<li><a href="https://github.com/welsinga/sample_espresso">Github repo corresponding to this article</a></li>
<li><a href="https://github.com/googlesamples/android-testing">General Espresso Github samples by Google</a></li>
</ul>
<p>
<strong>Updated</strong> (08-04) When running the tests with gradle you can just use <code class="codecolorer bash blackboard"><span class="bash">gradle connectedAndroidTest</span></code></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>AndroidDev (Pro) tip: Do not forget screenSize</title>
		<link>http://wiebe-elsinga.com/blog/androiddev-pro-tip-dont-reload-when-orientation-changes/</link>
					<comments>http://wiebe-elsinga.com/blog/androiddev-pro-tip-dont-reload-when-orientation-changes/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 01 Feb 2013 08:34:52 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1424</guid>

					<description><![CDATA[So you are stuck using screenOrientation in your android:configChanges list within your AndroidManifest. Your activity will not be recreated, but will receive a callback instead. You may not know that this is insufficient on API level of 13 or above. In API level 13 or above, the screen size changes when the orientation changes, so this cause the activity to be destroyed. If you still want to keep the state and use onConfigurationChanged() you will also need to use screenSize.]]></description>
										<content:encoded><![CDATA[<p>So you are stuck using screenOrientation in your <em>android:configChanges</em> list within your <em>AndroidManifest</em>. Your activity will not be recreated, but will receive a callback instead.<br />
You may not know that this is insufficient on API level of 13 or above.</p>
<p>In API level 13 or above, the screen size changes when the orientation changes, so this cause the activity to be destroyed. If you still want to keep the state and use <em>onConfigurationChanged()</em> you will also need to use <em>screenSize</em>.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/androiddev-pro-tip-dont-reload-when-orientation-changes/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>AndroidDev (Pro) tip: Fragment meets container</title>
		<link>http://wiebe-elsinga.com/blog/androiddev-pro-tip-fragment-meets-container/</link>
					<comments>http://wiebe-elsinga.com/blog/androiddev-pro-tip-fragment-meets-container/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 01 Feb 2013 08:04:58 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[Container]]></category>
		<category><![CDATA[Fragment]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1415</guid>

					<description><![CDATA[Today a tip from my colleague Coen Houtman, a tip about Fragments and the container to put them in. What all of you Fragment users know, is that you must have a container (i.e. ViewGroup) to put your fragments in. Which ViewGroup is the best to use? Normally, with any other layout I want to create, I start with a LinearLayout. It&#8217;s simple, easy and lightweight; something that you will experience in more complex layouts. If you use LinearLayout as the container for your Fragments, you will probably run into some issues. In the image below, the content doesn&#8217;t even take up half the screen. Even though the width was set to match it&#8217;s parent. The solution to the problem above: use FrameLayout! I&#8217;m not claiming that FrameLayout is always the way to go. In all our projects though, we use FrameLayout as the container for our Fragments. At the moment I don&#8217;t have the answer of why FrameLayout works better than LinearLayout. But when I do, you&#8217;ll read it here!]]></description>
										<content:encoded><![CDATA[<p>Today a tip from my colleague Coen Houtman, a tip about Fragments and the container to put them in. What all of you Fragment users know, is that you must have a container (i.e. ViewGroup) to put your fragments in.</p>
<h4>Which ViewGroup is the best to use?</h4>
<p>Normally, with any other layout I want to create, I start with a LinearLayout. It&#8217;s simple, easy and lightweight; something that you will experience in more complex layouts. If you use LinearLayout as the container for your Fragments, you will probably run into some issues. In the image below, the content doesn&#8217;t even take up half the screen. Even though the width was set to match it&#8217;s parent.</p>
<p style="text-align: justify;"><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146.png" rel="lightbox[1415]" title="AndroidDev (Pro) tip: Fragment meets container"><img loading="lazy" class="aligncenter size-full wp-image-1417" alt="device-2012-12-14-111146" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146.png" width="187" height="300" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146.png 800w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146-187x300.png 187w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146-640x1024.png 640w" sizes="(max-width: 187px) 100vw, 187px" /></a></p>
<p>The solution to the problem above: use FrameLayout! I&#8217;m not claiming that FrameLayout is always the way to go. In all our projects though, we use FrameLayout as the container for our Fragments. At the moment I don&#8217;t have the answer of why FrameLayout works better than LinearLayout. But when I do, you&#8217;ll read it here!</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/androiddev-pro-tip-fragment-meets-container/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sending and Receiving Data SMS messages with Android</title>
		<link>http://wiebe-elsinga.com/blog/sending-and-receiving-data-sms-messages-with-android/</link>
					<comments>http://wiebe-elsinga.com/blog/sending-and-receiving-data-sms-messages-with-android/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Mon, 03 Dec 2012 03:01:40 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[Data SMS]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1295</guid>

					<description><![CDATA[The humble short message service (SMS) celebrates its 20th birthday today. Engineer Neil Papworth sent the first SMS to an Orbitel 901 handset via Vodafone UK&#8217;s network in 1992: &#8220;Merry Christmas.&#8221; It would be safe to say that nearly every mobile phone sold in the past decade has SMS messaging capabilities. So making use of SMS with your Android app can be a great way to handle information when no Data network (3G or WIFI) is available. In this article, we will take a look at how you can programmatically send and receive SMS messages in your Android applications. Sending SMS Data messages So lets start with the sender AndroidManifest.xml. I recommend you open up the sample code, as I will be highlighting the key parts on this page. &#160;&#60;uses-permission android:name=&#34;android.permission.SEND_SMS&#34; /&#62; The uses-permission tag will allow the application to send SMS messages. Sending SMS message with the application will potentially incur additional cost on the user&#8217;s end. Indicating the SMS permissions in the AndroidManifest.xml file will let the user decide whether to allow the application to install or not. Next, we will create an activity called SendActivity that will handle sending SMS messages. private void sendSMS&#40;&#41; &#160; &#123; &#160; &#160; PendingIntent sent = this.createPendingResult&#40;SENT, new Intent&#40;&#41;, PendingIntent.FLAG_UPDATE_CURRENT&#41;; &#160; &#160; String messageText = _message.getText&#40;&#41;.toString&#40;&#41;; &#160; &#160; SmsManager smsManager = SmsManager.getDefault&#40;&#41;; &#160; &#160; smsManager.sendDataMessage&#40;_phoneNumber.getText&#40;&#41;.toString&#40;&#41;, null, SMS_PORT, messageText.getBytes&#40;&#41;, sent, null&#41;; &#160; &#125; To send an SMS message, you use the SmsManager class. Unlike other classes, you do not directly instantiate this class; instead you will call the getDefault() static method to obtain an SmsManager object. The sendDataMessage() method sends the SMS message with a PendingIntent. The PendingIntent object is used to identify a target to invoke at a later time. For example, after sending the message, you can use a PendingIntent object to update the current activity, and onActivityResult will be triggered. &#160; @Override &#160; protected void onActivityResult&#40;int requestCode, int resultCode, Intent data&#41; &#160; &#123; &#160; &#160; String toast = null; &#160; &#160; switch &#40;requestCode&#41; &#160; &#160; &#123; &#160; &#160; &#160; case SENT : &#160; &#160; &#160; &#160; switch &#40;resultCode&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; case RESULT_OK : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;OK&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#160; case SmsManager.RESULT_ERROR_GENERIC_FAILURE : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;Generic Failure&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#160; case SmsManager.RESULT_ERROR_RADIO_OFF : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;Radio Off&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#160; case SmsManager.RESULT_ERROR_NULL_PDU : &#160; &#160; &#160; &#160; &#160; &#160; toast = &#34;Null Pdu&#34;; &#160; &#160; &#160; &#160; &#160; &#160; break; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; break; &#160; &#160; &#125; &#160; &#160; if &#40;toast != null&#41; &#160; &#160; &#123; &#160; &#160; &#160; Toast.makeText&#40;this, toast, Toast.LENGTH_LONG&#41;.show&#40;&#41;; &#160; &#160; &#125; &#125; Receiving SMS Data messages Besides sending SMS messages, you can also intercept incoming SMS messages using a BroadcastReceiver object. To see how to receive SMS messages from within your Android application, lets look at the AndroidManifest.xml file within our receiver sample code. &#160;&#60;uses-permission android:name=&#34;android.permission.RECEIVE_SMS&#34; /&#62; &#160;&#60;uses-permission android:name=&#34;android.permission.BROADCAST_SMS&#34; /&#62; &#160;&#60;uses-permission android:name=&#34;android.permission.READ_SMS&#34; /&#62; The uses-permission tags will allow the application to receive and read SMS messages. Lets create a class that extends the BroadcastReceiver class and override the onReceive() method: &#160;@Override &#160; public void onReceive&#40;Context context, Intent intent&#41; &#160; &#123; &#160; &#160; Bundle bundle = intent.getExtras&#40;&#41;; &#160; &#160; String recMsgString = &#34;&#34;; &#160; &#160; String fromAddress = &#34;&#34;; &#160; &#160; SmsMessage recMsg = null; &#160; &#160; byte&#91;&#93; data = null; &#160; &#160; if &#40;bundle != null&#41; &#160; &#160; &#123; &#160; &#160; &#160; //---retrieve the SMS message received--- &#160; &#160; &#160; Object&#91;&#93; pdus = &#40;Object&#91;&#93;&#41; bundle.get&#40;&#34;pdus&#34;&#41;; &#160; &#160; &#160; for &#40;int i = 0; i &#60; pdus.length; i++&#41; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; recMsg = SmsMessage.createFromPdu&#40;&#40;byte&#91;&#93;&#41; pdus&#91;i&#93;&#41;; &#160; &#160; &#160; &#160; try &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; data = recMsg.getUserData&#40;&#41;; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; catch &#40;Exception e&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; if &#40;data != null&#41; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; for &#40;int index = 0; index &#60; data.length; ++index&#41; &#160; &#160; &#160; &#160; &#160; &#123; &#160; &#160; &#160; &#160; &#160; &#160; recMsgString += Character.toString&#40;&#40;char&#41; data&#91;index&#93;&#41;; &#160; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; fromAddress = recMsg.getOriginatingAddress&#40;&#41;; &#160; &#160; &#160; &#125; &#160; &#160; &#125; &#160; &#160; Intent result = new Intent&#40;context, ReceiveActivity.class&#41;; &#160; &#160; result.putExtra&#40;ReceiveActivity.MESSAGE, &#34;Received SMS from &#34; + fromAddress + &#34;: &#34; + recMsgString&#41;; &#160; &#160; result.addFlags&#40;Intent.FLAG_ACTIVITY_NEW_TASK&#41;; &#160; &#160; context.startActivity&#40;result&#41;; &#160; &#125; When SMS messages are received, the onReceive() method will be invoked. The SMS message is contained and attached to the Intent object via a Bundle object. The messages are stored in an Object array in the PDU format. To extract each message, you use the static createFromPdu() method from the SmsMessage class. The SMS message is then passed to an Activity that shows the message. That&#8217;s it. We&#8217;ve managed to send and received a Data SMS within an Android application. Till next time.]]></description>
										<content:encoded><![CDATA[<p>The humble short message service (SMS) celebrates its 20th birthday today. Engineer Neil Papworth sent the first SMS to an Orbitel 901 handset via Vodafone UK&#8217;s network in 1992: &#8220;Merry Christmas.&#8221;</p>
<p>It would be safe to say that nearly every mobile phone sold in the past decade has SMS messaging capabilities. So making use of SMS with your Android app can be a great way to handle information when no Data network (3G or WIFI) is available.<br />
In this article, we will take a look at how you can programmatically send and receive SMS messages in your Android applications.</p>
<h2>Sending SMS Data messages</h2>
<p>So lets start with the sender <i>AndroidManifest.xml</i>. I recommend you open up the <a href="https://github.com/welsinga/sample_sms" title="Sample project" target="_blank">sample code</a>, as I will be highlighting the key parts on this page.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&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.SEND_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>The <b>uses-permission</b> tag will allow the application to send SMS messages. Sending SMS message with the application will potentially incur additional cost on the user&#8217;s end.<br />
Indicating the SMS permissions in the <i>AndroidManifest.xml</i> file will let the user decide whether to allow the application to install or not. </p>
<p>Next, we will create an activity called <i>SendActivity</i> that will handle sending SMS messages.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> sendSMS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; PendingIntent sent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">createPendingResult</span><span style="color: #009900;">&#40;</span>SENT, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, PendingIntent.<span style="color: #006633;">FLAG_UPDATE_CURRENT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> messageText <span style="color: #339933;">=</span> _message.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; SmsManager smsManager <span style="color: #339933;">=</span> SmsManager.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; smsManager.<span style="color: #006633;">sendDataMessage</span><span style="color: #009900;">&#40;</span>_phoneNumber.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span>, SMS_PORT, messageText.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, sent, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>To send an SMS message, you use the <b>SmsManager</b> class. Unlike other classes, you do not directly instantiate this class; instead you will call the <b>getDefault()</b> static method to obtain an <b>SmsManager</b> object. The <b>sendDataMessage()</b> method sends the SMS message with a PendingIntent. The <b>PendingIntent</b> object is used to identify a target to invoke at a later time. For example, after sending the message, you can use a PendingIntent object to update the current activity, and <b>onActivityResult</b> will be triggered.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onActivityResult<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> requestCode, <span style="color: #000066; font-weight: bold;">int</span> resultCode, Intent data<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> toast <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>requestCode<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SENT <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>resultCode<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> RESULT_OK <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;OK&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SmsManager.<span style="color: #006633;">RESULT_ERROR_GENERIC_FAILURE</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Generic Failure&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SmsManager.<span style="color: #006633;">RESULT_ERROR_RADIO_OFF</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Radio Off&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> SmsManager.<span style="color: #006633;">RESULT_ERROR_NULL_PDU</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Null Pdu&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>toast <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, toast, 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><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></p>
<h2>Receiving SMS Data messages</h2>
<p>Besides sending SMS messages, you can also intercept incoming SMS messages using a <b>BroadcastReceiver</b> object.</p>
<p>To see how to receive SMS messages from within your Android application, lets look at the <i>AndroidManifest.xml</i> file within our receiver <a href="https://github.com/welsinga/sample_sms" title="Sample project" target="_blank">sample code</a>.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&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_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&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.BROADCAST_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&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.READ_SMS&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<p>The <b>uses-permission</b> tags will allow the application to receive and read SMS messages. Lets create a class that extends the <b>BroadcastReceiver</b> class and override the onReceive() method:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp;@Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> context, Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Bundle bundle <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><br />
<br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> recMsgString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a> fromAddress <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; SmsMessage recMsg <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bundle <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//---retrieve the SMS message received---</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+object"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> pdus <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+object"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> bundle.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pdus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> pdus.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; recMsg <span style="color: #339933;">=</span> SmsMessage.<span style="color: #006633;">createFromPdu</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> pdus<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data <span style="color: #339933;">=</span> recMsg.<span style="color: #006633;">getUserData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> index <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>index<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recMsgString <span style="color: #339933;">+=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+character"><span style="color: #003399;">Character</span></a>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> data<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; fromAddress <span style="color: #339933;">=</span> recMsg.<span style="color: #006633;">getOriginatingAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; Intent result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>context, ReceiveActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; result.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span>ReceiveActivity.<span style="color: #006633;">MESSAGE</span>, <span style="color: #0000ff;">&quot;Received SMS from &quot;</span> <span style="color: #339933;">+</span> fromAddress <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> recMsgString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; result.<span style="color: #006633;">addFlags</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><br />
&nbsp; &nbsp; context.<span style="color: #006633;">startActivity</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>When SMS messages are received, the <b>onReceive()</b> method will be invoked. The SMS message is contained and attached to the Intent object via a <b>Bundle</b> object.<br />
The messages are stored in an Object array in the <b>PDU</b> format. To extract each message, you use the static <b>createFromPdu()</b> method from the <b>SmsMessage</b> class.<br />
The SMS message is then passed to an Activity that shows the message.</p>
<p>That&#8217;s it. We&#8217;ve managed to send and received a Data SMS within an Android application. Till next time.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/sending-and-receiving-data-sms-messages-with-android/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Android 4.2 minnor bug</title>
		<link>http://wiebe-elsinga.com/blog/1245/</link>
					<comments>http://wiebe-elsinga.com/blog/1245/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Sun, 18 Nov 2012 08:02:14 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Android4.2]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1245</guid>

					<description><![CDATA[Looks like Android 4.2. has a &#8220;minnor&#8221; Bug. They aren&#8217;t celebrating Christmas at Google because Android 4.2 lacks the month of December.]]></description>
										<content:encoded><![CDATA[<p>Looks like Android 4.2. has a &#8220;minnor&#8221; Bug. They aren&#8217;t celebrating Christmas at Google because Android 4.2 lacks the month of December.</p>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2012/11/Untitled.png" rel="lightbox[1245]" title="Untitled"><img loading="lazy" class="aligncenter size-medium wp-image-1246" title="Untitled" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2012/11/Untitled-300x196.png" alt="" width="300" height="196" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2012/11/Untitled-300x196.png 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2012/11/Untitled.png 761w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/1245/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
