`
kobe学java
  • 浏览: 258382 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Android之BroadcastReceiver讲义并通过广播机制实现短信信息

 
阅读更多
<p>
</p>
<div class="article_title" style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; font-family: 'Microsoft YaHei'; font-size: 20px; line-height: 30px; text-align: left;">
<h3 style="display: inline; font-size: 20px; font-weight: normal; vertical-align: middle; padding: 0px; margin: 0px;"><span class="link_title"><a style="color: #000000; text-decoration: none;" href="http://blog.csdn.net/jjaze3344/article/details/7259272">Android之BroadcastReceiver讲义并通过广播机制实现短信</a></span></h3>
</div>
<div class="article_manage" style="padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; color: #999999; font-family: Arial; line-height: 24px; text-align: right;">
<span class="link_categories" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; float: left;">分类: <a style="color: #336699; text-decoration: none;" href="http://blog.csdn.net/jjaze3344/article/category/945121">Android之路</a></span><span class="link_postdate" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px;">2012-02-14 20:33</span> <span class="link_view" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; background-color: initial; background-position: 0% 50%;" title="阅读次数">315人阅读</span> <span class="link_comments" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 14px; background-color: initial; background-position: 0% 50%;" title="评论次数"><a style="color: #336699; text-decoration: none;" href="http://blog.csdn.net/jjaze3344/article/details/7259272#comments">评论</a>(0)</span> <span class="link_collect" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px;"><a style="color: #336699; text-decoration: none;" title="收藏" href="http://blog.csdn.net/jjaze3344/article/details/7259272">收藏</a></span> <span class="link_report" style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 5px;"><a style="color: #336699; text-decoration: none;" title="举报" href="http://blog.csdn.net/jjaze3344/article/details/7259272#report">举报</a></span>
</div>
<div id="article_content" class="article_content" style="margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Arial; font-size: 14px; line-height: 26px; color: #333333; text-align: left;">
<p>本文来源于雷哥(郭老师)。</p>
<p> </p>
<h1 style="padding: 0px; margin: 0px;"><span style="font-size: 16px;">BroadcastReceiver</span></h1>
<h2 style="padding: 0px; margin: 0px;"><span style="font-size: 16px;">BroadcastReceiver概述:</span></h2>
<p> </p>
<p>BroadcastReceiver是接收从sendBroadcast()发出的intent的基类。你可以通过Context.registerReceiver()方法在代码中动态的注册一个BroadcastReceiver的实例,也可以通过再AndroidManifest.xml文件中用&lt;receiver&gt;标签来静态声明。</p>
<p>注意:如果你实在Activity.onResume()方法中注册的一个receiver,那么你必须在Activity.onPause()方法中进行注销。(当一个activity处于暂停状态是不会接收intents的,并且这样做也可以减小系统不必要的开销)。不要在Activity.onSaveInstanceState()方法中注销receiver,因为activity从栈中恢复的时候并不会调用这个方法了。</p>
<p>订阅广播的两种方法:</p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"MySMSListener"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">   <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">       <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">action</span><span style="background-color: inherit; padding: 0px; margin: 0px;">   </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.provider.Telephony.SMS_RECEIVED"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">   <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
</ol>
</div>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span style="background-color: inherit; padding: 0px; margin: 0px;">IntentFilter </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">filter</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> IntentFilter("android.provider.Telephony.SMS_RECEIVED");  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">IncomingSMSReceiver <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">receiver</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> IncomingSMSReceiver();  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">registerReceiver(receiver, filter);  </span></li>
</ol>
</div>
<br><p><strong>可以接收的broadcast主要分为两种类型</strong>:<br>       <strong>普通的broadcasts(</strong>通过Context.sendBroadcast发送)是完全异步的。这个broadcast的receiver以无序的状态运行,经常是在同一时刻运行。这种做法是十分高效的,但是也意味着receiver不能够利用相互处理的结果或者是调用退出的API来退出(因为不知道哪个receiver先接收到intent)。</p>
<p>       <strong>有序的broadcasts</strong>(通过Context.sendOrderedBroadcast发送)一次只发送给一个receiver。每一个receiver是有序的处理这个intent的,前面的receiver可以传递结果给下一个receiver,或者任意一个receiver都可以完全的退出,这样intent就不会传递给其他的receivers.receiver的执行顺序可以通过匹配的intent-filter中的android:priority属性来控制;如果有多个receivers处于同一个优先级,那么这几个receivers将会以任意的顺序来执行。</p>
<p>       即使是在广播普通的broadcasts的情况下,系统也有可能在某些情况下转换为一次发送一个broadcast给一个receriver。特别是当receivers需要创建进程时,在同一时刻仅仅一个receiver可以运行,避免系统因为这些新建的进程而过载。</p>
<p> </p>
<p>注意:</p>
<p>尽管Intent类是用来发送和接受这些broadcasts,这里的Intentbroadcast机制和那些通过Context.startActivity()方法来启动activity的intent是完全独立的。一个BroadcastReceiver是没办法观察和捕获一个用于启动activity的intent的;同样的,当你通过intent来发出broadcast时,你也不可能(通过这个intent)找到或者启动一个activity的。这两种操作是完全不同的:通过一个intent来启动一个activity是一个前台操作,会改变用户当前交互的对象;而通过intent来发出broadcast是一个后台操作,用户经常是察觉不到的。</p>
BroadcastReceiver类(通过一个manifest的&lt;receiver&gt;标签作为一个组件启动)是应用程序全局声明周期重要的一部分。<br><br><p><strong>讨论的主题</strong><br>   1、Receiver的生命周期<br>   2、权限<br>   3、进程的生命周期<br></p>
<p><strong>Receiver的生命周期</strong></p>
<p>一个BroadcastReceiver的对象仅仅在调用onReceiver(COntext,Intent)的时间中有效。一旦你的代码从这个函数中返回,那么系统就认为这个对象应该结束了,不能再被激活。你在onReceive(Context, Intent)中的实现有着非常重要的影响:任何对于异步操作的请求都是不允许的,因为你可能需要从这个函数中返回去处理异步的操作,但是在那种情况下,BroadcastReceiver将不会再被激活,因此系统就会再异步操作之前杀死这个进程。</p>
<p>特别是,你不应该再一个BroadcastReceiver中显示一个对话框或者绑定一个服务。对于前者(显示一个对话框),你应该用NotificationManagerAPI来替代,对于后者(绑定一个服务),你可以使用Context.startService()发送一个命令给那个服务来实现绑定效果。</p>
<p> </p>
<p><strong>权限</strong></p>
<p>存取的权限可以通过在发送方的Intent或者接收方的Intent中强制指定。</p>
<p>       在发送一个broadcast时强制指定权限,就必须提供一个非空的peemission参数给sendBroadcast(Intent,String)或者是sendOrderedBroadcast(Intent, String,BroadcastReceiver, android.os.Handel, int, String, Bundle)。只有那些拥有这些权限(通过在ANdroidManifest.xml文件中相应的声明&lt;uses-permission&gt;标签)的receiver能够接收这些broadcast。</p>
<p>在接收一个broadcast时强制指定权限,就必须在注册receiver时提供一个非空的permission参数--无论是在调用registerReceiver(BroadcastReceiver, IntentFilter,String, android.os.Handler)或者是通过再AndroidManifest.xml文件中通过&lt;receiver&gt;静态标签来声明。只有那些拥有这些权限(通过在相应的AndroidManifest.xml文件中查询&lt;uses-permission&gt;标签来获知)的发送方将能够给这个receiver发送Intent。</p>
<p>对于安全和权限的详细内容请查看Security and Permission文档。</p>
<p> </p>
<p><strong>进程的生命周期</strong></p>
<p>一个正在执行BroadcastReceiver(也就是,正在执行onReceive(COntext, Intent)方法)的进程被认为是一个前台的进程,将会一直运行,除非系统处于内存极度低的情况下。</p>
<p>一旦从OnReceive()方法中返回,这个BroadcastReceiver将不会再被激活,此时它的主进程就和任何其他运行于此应用程序中的组件拥有相同的优先级。这一点非常重要,如果进程仅仅只是拥有BroadReceiver(一个普遍的情况是用户从不或者是最近没有和它进行交互),因此一旦它从onReceive()方法中返回时,系统就会认为进程是空的并且主动的杀死它,以便这些资源可以被其他重要的进程利用。</p>
<p>这意味着对于耗时的操作,可以采用将Service和BroadcastReceiver结合使用以确保执行这个操作的进程在整个执行过程中都保持激活状态。</p>
<br><h3 style="padding: 0px; margin: 0px;"><span style="font-size: 18px;">短信器</span></h3>
<div>
<p><span style="font-size: 10px;">服务器(videoweb):</span></p>
<p><span style="font-size: 10px;">l  修改formbean:VideoForm中增加短信的时间、内容和发送者属性</span></p>
<p><span style="font-size: 10px;">l  VideoManageAction中增加方法getSMS来获取器发送的短消息</span></p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span style="background-color: inherit; padding: 0px; margin: 0px;">public ActionForward getSMS(ActionMapping mapping, ActionForm form,  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            HttpServletRequest request, HttpServletResponse response)  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            throws Exception {  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    VideoForm <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">formbean</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = (VideoForm)form;  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    System.out.println("发送时间:"+ formbean.getTime());  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    System.out.println("谁给她发的短信:"+ formbean.getSender());  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    System.out.println("内容:"+ formbean.getContent());  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    return mapping.findForward("result");  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">}  </span></li>
</ol>
</div>
<br><p>客户端SMS_Listener</p>
<p>1、 清单文件中订阅广播</p>
<p> </p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">".MySMSListener"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">      <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">action</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.provider.Telephony.SMS_RECEIVED"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"> <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
</ol>
</div>
<br><p> </p>
<p>添加短信接收权限,访问网络权限</p>
<p align="left"> </p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">uses-permission</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.permission.INTERNET"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">uses-permission</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.permission.RECEIVE_SMS"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
</ol>
</div>
<br><p> </p>
<p>2、 客户端MySMSListener.java</p>
<p>功能:收取短信广播,接收并解析短信然后发送至服务器端进行后台打印。</p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span style="background-color: inherit; padding: 0px; margin: 0px;">package cn.class3g.smslistener;  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">import java.text.SimpleDateFormat;  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">import java.util.Date;  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">import java.util.HashMap;  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">import java.util.Map;  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">import cn.class3g.utils.SocketHttpRequester;  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">…  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">public class MySMSListener extends BroadcastReceiver {  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    public void onReceive(Context context, Intent intent) {  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        Object[] <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">pdus</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = (Object[]) intent.getExtras().get("pdus");  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        if (pdus != null &amp;&amp; pdus.length <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> 0) {  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            SmsMessage[] <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">messages</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> SmsMessage[pdus.length];  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            for (int <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">i</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">0</span><span style="background-color: inherit; padding: 0px; margin: 0px;">; i </span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">messages.length</span><span style="background-color: inherit; padding: 0px; margin: 0px;">; i++) {  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                byte[] <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">pdu</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = (byte[]) pdus[i];  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                messages[i] = SmsMessage.createFromPdu(pdu);  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            }  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            for (SmsMessage msg : messages) {  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                String <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">content</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">msg</span><span style="background-color: inherit; padding: 0px; margin: 0px;">.getMessageBody();  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                String <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">sender</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">msg</span><span style="background-color: inherit; padding: 0px; margin: 0px;">.getOriginatingAddress();  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                Date <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">date</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> Date(msg.getTimestampMillis());  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                SimpleDateFormat <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">sdf</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> SimpleDateFormat(  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                        "yyyy-MM-dd HH:mm:ss");  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                String <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">sendTime</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">sdf</span><span style="background-color: inherit; padding: 0px; margin: 0px;">.format(date);  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                Map<span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">String</span><span style="background-color: inherit; padding: 0px; margin: 0px;">,String</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">param</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> HashMap</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">String</span><span style="background-color: inherit; padding: 0px; margin: 0px;">,String</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">();  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                param.put("method", "getSMS");  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                param.put("sender", sender);  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                param.put("content", content);  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                param.put("time", sendTime);  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                String <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">path</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> =   </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"http://192.168.1.100:8080/videoweb/video/manage.do"</span><span style="background-color: inherit; padding: 0px; margin: 0px;">;  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                try {  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                    SocketHttpRequester.post(path, param, "UTF-8");  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                } catch (Exception e) {  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                    Log.e("TAG",e.toString());  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                }  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">            }  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        }  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    }  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">}  </span></li>
</ol>
</div>
<br><p><strong>SmsMessage</strong></p>
<p>public static SmsMessage createFromPdu(byte[] pdu)</p>
<p>从原始的PDU(protocol description units)创建一个SmsMessage。这个方法很重要,在我们编写短信接收程序要用到,它从我们接收到的广播意图中获取的字节创建SmsMessage。</p>
<p> </p>
<p>public String getOriginatingAddress ()<br>以String返回SMS信息的来电地址,或不可用时为null。</p>
<p> </p>
<p>public String getMessageBody ()<br>以一个String返回消息的主体,如果它存在且是基于文本的。</p>
<p> </p>
<p><strong>短信管理器 : SmsManager</strong></p>
<p>1). 在 Android 2.0 以前 应该使用android.telephony.gsm.SmsManager</p>
<p>   之后应该用 android.telephony.SmsManager;</p>
<p>2). 获取系统默认的短信管理器 </p>
<p>SmsManager smsManager =SmsManager.getDefault();</p>
<p>3). 按照每条短信最大字数来拆分短信</p>
<p>   List&lt;String&gt;divideContents = smsManager.divideMessage(content);</p>
<p>4). 发送短信</p>
<p>       smsManager.sendTextMessage(destinationAddress,scAddress, text, sentIntent, deliveryIntent)</p>
<p>             --destinationAddress:目标电话号码</p>
<p>             --scAddress:短信中心号码,测试可以不填</p>
<p>             --text: 短信内容</p>
<p>             --sentIntent:发送 --&gt;中国移动 --&gt; 中国移动发送失败 --&gt; 返回发送成功或失败信号 --&gt; 后续处理   即,这个意图包装了短信发送状态的信息</p>
<p>             --deliveryIntent: 发送 --&gt;中国移动 --&gt; 中国移动发送成功 --&gt; 返回对方是否收到这个信息 --&gt; 后续处理  即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。</p>
<p> </p>
<p>5). 声明短信发送权限</p>
<p>     *AndroidManifest.xml</p>
<p>      &lt;uses-permissionandroid:name="android.permission.SEND_SMS"/&gt;</p>
<p> </p>
<p>1、 进一步添加客户端功能:将监听到的指定的短信进行拦截并且自动进行回复</p>
<p> </p>
<p>添加短信发送权限:</p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">uses-permission</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.permission.SEND_SMS"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
</ol>
</div>
<br><p>MySMSListener的onReceive中添加代码如下</p>
<p> </p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span style="background-color: inherit; padding: 0px; margin: 0px;">String </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">sendContent</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">sdf</span><span style="background-color: inherit; padding: 0px; margin: 0px;">.format(date) + ":" + sender + "--"  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                        + content;  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">                  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">Log.i("TAG",sendContent);  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">if(sender!= null &amp;&amp; sender.endsWith("5556")){//5556".equals(sender)){  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    SmsManager <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">smsManager</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">SmsManager</span><span style="background-color: inherit; padding: 0px; margin: 0px;">.getDefault();  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    smsManager.sendTextMessage("5556",null,"game over",null,null);  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    this.abortBroadcast(); //终止广播  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">}  </span></li>
</ol>
</div>
<br><p> </p>
<p>测试:</p>
<p>启动另一个模拟器,向部署客户端的模拟器发送短信,查看服务器端后台输出,并且观察客户端运行模拟器是否有短信接收。</p>
<p> </p>
<h2 style="padding: 0px; margin: 0px;">补充:</h2>
<p>       除了短信到来广播Intent ,Android 还有很多广播 Intent ,如:开机启动、电池电量变化、时间已经改变等广播 Intent 。</p>
<p> </p>
<p>l 接收电池电量变化广播 Intent ,在AndroidManifest.xml 文件中的 &lt;application&gt;</p>
<p>节点里订阅此 Intent:</p>
<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">".IncomingSMSReceiver"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">   <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">action</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.intent.action.BATTERY_CHANGED"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
</ol>
</div>
<p>l 接收开机启动广播 Intent , 在AndroidManifest.xml 文件中的 &lt;application&gt; 节点里</p>
<p>订阅此 Intent:</p>
<br><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">receiver</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">".IncomingSMSReceiver"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">  <span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">action</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=”android.intent.action.BOOT_COMPLETED” </span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;/</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">intent-filter</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">&lt;</span><span class="tag-name" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">uses-permission</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> </span><span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">android:name</span><span style="background-color: inherit; padding: 0px; margin: 0px;">=</span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">"android.permission.RECEIVE_BOOT_COMPLETED"</span><span class="tag" style="color: #993300; background-color: inherit; font-weight: bold; padding: 0px; margin: 0px;">/&gt;</span><span style="background-color: inherit; padding: 0px; margin: 0px;">  </span></span></li>
</ol>
</div>
<p> </p>
<p>通常一个 BroadcastReceiver 对象的生命周期不超过5 秒 , 所以在BroadcastReceiver里不能做一些比较耗时的操作 , 如果需要完成一项比较耗时的工作 , 可以通过发送 Intent给 Activity 或Service ,由 Activity 或 Service 来完成。</p>
<br><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span style="background-color: inherit; padding: 0px; margin: 0px;">public class IncomingSMSReceiver extends BroadcastReceiver {  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    public void onReceive(Context context, Intent intent) {  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        // 发送 Intent 启动服务,由服务来完成比较耗时的操作  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        Intent <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">service</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> Intent(context, XxxService.class);  </span></span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        context.startService(service);  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        // 发送 Intent 启动 Activity ,由 Activity 来完成比较耗时的操作  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        Intent <span class="attribute" style="color: red; background-color: inherit; padding: 0px; margin: 0px;">newIntent</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> = </span><span class="attribute-value" style="color: blue; background-color: inherit; padding: 0px; margin: 0px;">new</span><span style="background-color: inherit; padding: 0px; margin: 0px;"> Intent(context, XxxActivity.class);  </span></span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">        context.startActivity(newIntent);  </span></li>
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">    }  </span></li>
<li style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;">}  </span></li>
</ol>
</div>
<br>当然 , 实现了 BroadcastReceiver , 有时你可能会觉得不需要它 , 那么你可以将已经注册好的 BroadcastReceiver 进行注销 :<br><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 687px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px;">
<div class="bar" style="padding-left: 45px;">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;">
<strong>[html]</strong> <a class="ViewSource" style="" title="view plain" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">view plain</a><a class="CopyToClipboard" style="" title="copy" href="http://blog.csdn.net/jjaze3344/article/details/7259272#">copy</a>
<div style="width: 18px; height: 18px;"><embed quality="best" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="18" height="18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" align="middle" wmode="transparent"></embed></div>
</div>
</div>
<ol class="dp-xml" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; background-color: #ffffff; color: #5c5c5c; padding: 0px;">
<li class="alt" style="padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: solid; border-color: initial; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin: 0px !important;"><span style="color: black; background-color: inherit; padding: 0px; margin: 0px;"><span style="background-color: inherit; padding: 0px; margin: 0px;">unregisterReceiver( BroadcastReceiver receiver) ;  </span></span></li>
</ol>
</div>
<br><p>思考:</p>
<p>如何在有序的广播中实现intent的修改,比如修改短信内容。</p>
</div>
</div>
分享到:
评论

相关推荐

    高焕堂android课堂讲义

    《高焕堂Android课堂讲义》是一份深入浅出的Android技术学习资料,由知名IT讲师高焕堂精心编撰。这份讲义以其独特的视角和深入的架构解析,深受Android初学者的喜爱。以下将对其中的主要知识点进行详细的阐述。 ...

    疯狂Android讲义(第2版)完整清晰版.pdf

    "服务与广播"部分讲述了后台服务的实现,包括IntentService和BoundService的区别与使用场景,以及BroadcastReceiver的注册与接收机制,帮助开发者实现无用户交互背景任务的执行。 最后,"性能优化"章节提供了关于...

    疯狂android讲义第七章

    Intent是Android中用于组件间通信的重要机制,它可以启动活动、服务或广播。Intent分为显式Intent和隐式Intent,前者指定要启动的目标组件,后者通过IntentFilter匹配合适的接收者。理解Intent的构造和使用,可以...

    疯狂的Android讲义光盘源码

    《疯狂的Android讲义》是一本深受Android开发者喜爱的书籍,其随书附带的光盘源码提供了丰富的实例和项目代码,旨在帮助读者深入理解Android应用开发的各个方面。这个压缩包包含了书中所讲解的各种实战案例,对于...

    疯狂Android讲义源码

     10.1.3 绑定本地Service并与之  通信 377  10.1.4 Service的生命周期 381  10.2 跨进程调用Service  (AIDL服务) 382  10.2.1 AIDL服务简介 382  10.2.2 创建AIDL文件 383  10.2.3 将接口暴露给客户端 383...

    疯狂Android讲义_第二版_源码

    3. **Intent机制**:Intent是Android中用于启动服务、活动或广播的重要工具,理解Intent的作用和使用方式,能让你更好地实现应用间的通信。 4. **Service**:Service是后台运行的组件,常用于执行长时间任务。学习...

    android_ 3ppt (吉林大学android课程英文讲义)

    根据提供的文件信息,我们可以提炼出以下关于Android应用生命周期的知识点,这些知识点源于吉林大学Android课程英文讲义,并且参考了《Unlocking Android》一书。 Android应用生命周期是指Android应用中各个组件...

    疯狂Android讲义 第3版.pdf

    《疯狂Android讲义 第3版》是一本专为Android初学者设计的教程,全面而深入地介绍了Android开发的各项核心技术...通过阅读并实践书中的例子,你将能够逐步掌握Android开发技能,走向成为一名专业的Android开发者之路。

    疯狂android讲义第四章源代码

    首先,Android应用程序的基本架构主要包括四大组件:活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver)和内容提供者(ContentProvider)。活动是用户界面的主要入口,是用户与应用交互的窗口;...

    疯狂Android讲义(第2版)完整.zip

    通过阅读《疯狂Android讲义(第2版)》完整版,开发者将能深入理解上述知识点,并逐步掌握Android开发的各个环节。这本书还可能涵盖更多实践案例和最新技术,帮助读者提升实际开发技能,成为专业的Android开发者。

    Android-疯狂讲义源码

    【Android 疯狂讲义源码】是一个...通过对"Android-疯狂讲义源码"的学习和研究,开发者可以深入理解上述各个知识点,并能将它们应用到实际项目中,提升Android开发技能。这个源码集是实践和巩固理论知识的理想平台。

    android同济大学讲义

    一个典型的Android应用由多个组件组成,如Activity(界面)、Service(后台服务)、BroadcastReceiver(广播接收者)、ContentProvider(数据提供者)和Intent(意图)。Intent用于组件间的通信,是Android的灵魂...

    疯狂Android讲义--随书光盘

    《疯狂Android讲义》是一本深入浅出的Android开发教程,其随书光盘包含了丰富的学习资源,旨在帮助读者全面掌握Android应用开发的核心技术。这本书的知识点覆盖了Android开发的各个方面,从基础到高级,从理论到实践...

    《疯狂Android讲义》03章读书笔记(含源码)

    开发者可以通过实现或继承特定的接口,如OnClickListener、OnTouchListener,来注册事件监听器,并在对应的回调方法中编写处理逻辑。 4. **数据存储**:Android提供了多种数据存储方式,包括Shared Preferences...

    Android 开发完全讲义第二版源代码

    这份源代码是李宁所著《Android开发完全讲义》第二版的配套材料,旨在帮助读者更好地理解书中的概念,并通过实践来提升Android编程技能。 在Android开发中,源代码是理解和学习平台工作原理的关键。这份资料提供了...

    台湾高人高焕堂android 完整讲义

    这份讲义旨在帮助开发者全面理解Android系统的运作机制,提升在Android应用开发中的实践能力。 一、Android基础知识 Android作为全球最流行的移动操作系统之一,其基础知识是所有Android开发者必须掌握的。这包括:...

    疯狂Android讲义--随书光盘.7z

    从文件名称列表来看,02、04、08、09、10、11、12、14、16这些编号可能代表不同的章节或主题,涵盖了Android的基础概念,包括Android系统架构、Activity生命周期、Intent机制、BroadcastReceiver广播接收器、Service...

    疯狂android讲义第二版配套源代码 第12章

    广播接收者(BroadcastReceiver)是Android四大组件之一,它用于监听并响应系统或应用发布的广播意图。通过注册广播接收者,我们可以对特定的广播事件做出反应,如系统开机、网络状态改变等。开发者可以通过动态注册...

    疯狂android讲义第十三章

    3. **BroadcastReceiver的注册与使用**:BroadcastReceiver用于接收系统或应用广播事件,实现对特定事件的响应。这里会介绍动态和静态注册的区别,以及如何编写自定义的BroadcastReceiver。 4. **ContentProvider的...

    疯狂Android讲义(第2版)源码

    《疯狂Android讲义(第2版)》是Android开发领域一本...通过《疯狂Android讲义(第2版)》源码的学习,读者可以亲手实践上述知识点,从而提升Android开发能力。书中涵盖的实例和源码将帮助开发者将理论知识转化为实际技能。

Global site tag (gtag.js) - Google Analytics