<?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>Data SMS &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/tag/data-sms/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Mon, 03 Dec 2012 10:09:36 +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>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>
	</channel>
</rss>
