﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-life02-随笔分类-android组件学习</title><link>http://www.cppblog.com/life02/category/18322.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 09 Apr 2012 15:10:19 GMT</lastBuildDate><pubDate>Mon, 09 Apr 2012 15:10:19 GMT</pubDate><ttl>60</ttl><item><title>PackageInstaller 原理简述（转）</title><link>http://www.cppblog.com/life02/archive/2012/04/08/170511.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Sun, 08 Apr 2012 14:29:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2012/04/08/170511.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/170511.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2012/04/08/170511.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/170511.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/170511.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2012/04/08/170511.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/170511.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2012-04-08 22:29 <a href="http://www.cppblog.com/life02/archive/2012/04/08/170511.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android开发之道（10）Handler本质简析与使用实例 （转载）</title><link>http://www.cppblog.com/life02/archive/2012/03/20/168391.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Tue, 20 Mar 2012 06:04:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2012/03/20/168391.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/168391.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2012/03/20/168391.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/168391.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/168391.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2012/03/20/168391.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/168391.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2012-03-20 14:04 <a href="http://www.cppblog.com/life02/archive/2012/03/20/168391.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android Service 服务（三）—— bindService与remoteService （转载）</title><link>http://www.cppblog.com/life02/archive/2012/03/20/168390.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Tue, 20 Mar 2012 06:02:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2012/03/20/168390.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/168390.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2012/03/20/168390.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/168390.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/168390.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2012/03/20/168390.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/168390.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2012-03-20 14:02 <a href="http://www.cppblog.com/life02/archive/2012/03/20/168390.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>android 中 涉及到context的时候this和this.getApplictionContext的区别 (转载)</title><link>http://www.cppblog.com/life02/archive/2012/03/20/168389.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Tue, 20 Mar 2012 05:56:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2012/03/20/168389.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/168389.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2012/03/20/168389.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/168389.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/168389.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2012/03/20/168389.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/168389.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2012-03-20 13:56 <a href="http://www.cppblog.com/life02/archive/2012/03/20/168389.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android Service学习之AIDL, Parcelable和远程服务 (转载)</title><link>http://www.cppblog.com/life02/archive/2012/03/06/167234.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Tue, 06 Mar 2012 03:49:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2012/03/06/167234.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/167234.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2012/03/06/167234.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/167234.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/167234.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2012/03/06/167234.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/167234.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2012-03-06 11:49 <a href="http://www.cppblog.com/life02/archive/2012/03/06/167234.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Activity的跳转与传值 （转载）</title><link>http://www.cppblog.com/life02/archive/2012/02/05/165013.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Sun, 05 Feb 2012 14:09:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2012/02/05/165013.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/165013.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2012/02/05/165013.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/165013.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/165013.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2012/02/05/165013.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/165013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2012-02-05 22:09 <a href="http://www.cppblog.com/life02/archive/2012/02/05/165013.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>android学习视频网站</title><link>http://www.cppblog.com/life02/archive/2011/12/15/162192.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Thu, 15 Dec 2011 15:50:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/15/162192.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/162192.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/15/162192.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/162192.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/162192.html</trackback:ping><description><![CDATA[<div>http://www.mars-droid.com/  </div><div>http://www.verycd.com/topics/2900036/</div><br /><div><span style="color: #000000; ">http:</span><span style="color: #008000; ">//</span><span style="color: #008000; ">hi-android.info/src/com/android/phone/&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #000000; "><br />&nbsp;http:</span><span style="color: #008000; ">//</span><span style="color: #008000; ">gitorious.org/0xdroid/packages_apps_phone/trees/5f6f01ecda4336dfb47108e67ff909a65f14b820/src/com/android/phone</span><span style="color: #008000; "><br /></span></div><br /> <div><embed id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" height="0" hidden="true" width="0"></div><img src ="http://www.cppblog.com/life02/aggbug/162192.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-15 23:50 <a href="http://www.cppblog.com/life02/archive/2011/12/15/162192.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android学习笔记（A-10）_记录与出错（LogCat的使用）（转载）</title><link>http://www.cppblog.com/life02/archive/2011/12/15/162187.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Thu, 15 Dec 2011 14:28:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/15/162187.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/162187.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/15/162187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/162187.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/162187.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2011/12/15/162187.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/162187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-15 22:28 <a href="http://www.cppblog.com/life02/archive/2011/12/15/162187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Thread、Looper、Handler和Message 解开迷惑 (转)</title><link>http://www.cppblog.com/life02/archive/2011/12/13/161993.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Mon, 12 Dec 2011 16:33:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/13/161993.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161993.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/13/161993.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161993.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161993.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2011/12/13/161993.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/161993.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-13 00:33 <a href="http://www.cppblog.com/life02/archive/2011/12/13/161993.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android 核心分析 之七------Service深入分析（转）</title><link>http://www.cppblog.com/life02/archive/2011/12/08/161747.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Thu, 08 Dec 2011 03:38:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/08/161747.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161747.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/08/161747.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161747.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161747.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tbody>
<tr>
<td bgcolor="#ffffff">
<div class="arttitle" align="center">Android核心分析<br /></div></td></tr>
<tr>
<td bgcolor="#ffffff" height="17">&nbsp;</td></tr>
<tr>
<td bgcolor="#ffffff">
<div class="artformtitle" align="center">
<p>发布于2011-09-28</p><a href="http://www.uml.org.cn/mobiledev/201109284.asp#7">http://www.uml.org.cn/mobiledev/201109284.asp#7</a></div></td></tr>
<tr>
<td bgcolor="#ffffff" height="16">&nbsp;</td></tr>
<tr>
<td valign="top" bgcolor="#ffffff">
<table class="artcontent" cellspacing="1" cellpadding="3" width="95%" align="center" border="0">
<tbody>
<tr>
<td valign="top" bgcolor="#ffffff">
<p class="artdir1"><a name="7">Android 核心分析 之七------Service深入分析</a></p>
<p class="artcon">上一章我们分析了Android IPC架构,知道了Android服务构建的一些基本理念和原理，本章我们将深入分析Android的服务。Android体系架构中三种意义上服务：</p>
<p class="artcon">Native服务</p>
<p class="artcon">Android服务</p>
<p class="artcon">Init空间的服务，主要是属性设置，这个ＩＰＣ是利用Ｓｏｃｋｅｔ来完成的，这个我将在另外一章来讨论。</p>
<p class="artcon">Navite服务，实际上就是指完全在C++空间完成的服务，主要是指系统一开始初始化，通过Init.rc脚本起来的服务，例如Service Manger service,Zygote service,Media service , ril_demon service等。</p>
<p class="artcon">Android服务是指在JVM空间完成的服务，虽然也要使用Navite上的框架，但是服务主体存在于Android空间。Android是二阶段初始（Init2）初始化时建立的服务。</p>
<p class="artcon"><strong>1 Service本质结构</strong></p>
<p class="artcon">我们还是从Service的根本意义分析入手，服务的本质就是响应客户端请求。要提供服务，就必须建立接收请求，处理请求，应答客服端的框架。我想在Android Service设计者也会无时不刻把这个服务本质框图挂在脑海中。从程序的角度，服务一定要存在一个闭合循环框架和请求处理框架</p>
<p class="artcon"><img height="232" src="http://www.uml.org.cn/mobiledev/images/2011092819.gif" width="530"  alt="" /></p>
<p class="artcon">分析清楚服务框就必须弄清楚以下的机制及其构成。</p>
<p class="artcon">（1）闭合循环结构放置在哪里？</p>
<p class="artcon">（2）处理请求是如何分发和管理？</p>
<p class="artcon">（3）处理框架是如何建立的？</p>
<p class="artcon">（4）概念框架是如何建立的？</p>
<p class="artcon"><strong>2 Service基本框架分析</strong></p>
<p class="artcon">Android设计中，Native Service和Android Service采用了同一个闭合循环框架。这个闭合循环框架放置在Native的C++空间中，,ProcessState@ProcessState.cpp 和IPCThreadState@IPCThreadState.cpp两个类完成了全部工作。</p>
<p class="artcon"><img height="258" src="http://www.uml.org.cn/mobiledev/images/2011092820.gif" width="279"  alt="" /></p>
<p class="artcon">在服务框架中，ProcessState是公用的部分，这个公用部分最主要的框架就是闭合循环框架和接收到从Binder来的请求后的处理框架。我们将服务框架用ProcessSate来表示,简言之：</p>
<p class="artcon">（1） addservice</p>
<p class="artcon">（2） 建立闭合循环处理框架。</p>
<p class="artcon">int main(int argc, char** argv)</p>
<p class="artcon">{</p>
<p class="artcon">sp&lt;ProcessState&gt; proc(ProcessState::self());</p>
<p class="artcon">addService(String16("xxx0"), new xxx0Service());</p>
<p class="artcon">addService(String16("xxx1"), new xxx1Service());</p>
<p class="artcon">&#8230;</p>
<p class="artcon">ProcessState::self()-&gt;startThreadPool();</p>
<p class="artcon">IPCThreadState::self()-&gt;joinThreadPool();//闭合循环框架</p>
<p class="artcon">}</p>
<p class="artcon"><img height="768" src="http://www.uml.org.cn/mobiledev/images/2011092821.gif" width="614"  alt="" /></p>
<p class="artcon">2.1 Native Service </p>
<p class="artcon">Native Service是在系统Init阶段通过Init.rc脚本建立的服务。</p>
<p class="artcon">首先来看看一个例子mediaserver@main_mediaserver.cpp的建立过程。</p>
<p class="artcon">int main(int argc, char** argv)</p>
<p class="artcon">{</p>
<p class="artcon">sp&lt;ProcessState&gt; proc(ProcessState::self());</p>
<p class="artcon">sp&lt;IServiceManager&gt; sm = defaultServiceManager();</p>
<p class="artcon">LOGI("ServiceManager: %p", sm.get());</p>
<p class="artcon">AudioFlinger::instantiate();</p>
<p class="artcon">MediaPlayerService::instantiate();</p>
<p class="artcon">CameraService::instantiate();</p>
<p class="artcon">AudioPolicyService::instantiate();</p>
<p class="artcon">ProcessState::self()-&gt;startThreadPool();</p>
<p class="artcon">IPCThreadState::self()-&gt;joinThreadPool();</p>
<p class="artcon">}</p>
<p class="artcon">我们将代码向下展开了一层，更能看到事物的本质。</p>
<p class="artcon">int main(int argc, char** argv)</p>
<p class="artcon">{</p>
<p class="artcon">sp&lt;ProcessState&gt; proc(ProcessState::self());</p>
<p class="artcon">sp&lt;IServiceManager&gt; sm = defaultServiceManager();</p>
<p class="artcon">defaultServiceManager()-&gt;addService(String16("media.audio_flinger"), new AudioFlinger());</p>
<p class="artcon">&#8230;</p>
<p class="artcon">ProcessState::self()-&gt;startThreadPool();</p>
<p class="artcon">IPCThreadState::self()-&gt;joinThreadPool();</p>
<p class="artcon">}</p>
<p class="artcon">（1）服务进程建立了ProcessState对象，并将给对象登记在进程的上下文中。</p>
<p class="artcon">（2）建立一个新AudioFlinger对象，并将对象登记Service Manager Service中。</p>
<p class="artcon">（3）开始就收请求，处理请求，应答这个循环闭合框架。</p>
<p class="artcon">2.2 Android Service</p>
<p class="artcon">Androids service是系统二阶段（Init2）初始化时建立的服务。</p>
<p class="artcon">Android的所有服务循环框架都是建立SystemServer@(SystemServer.java）上。在SystemServer.java中看不到循环结构，只是可以看到建立了init2的实现函数，建立了一大堆服务，并AddService到service Manager。</p>
<p class="artcon">main() @ com/android/server/SystemServer </p>
<p class="artcon">{</p>
<p class="artcon">init1();</p>
<p class="artcon">}</p>
<p class="artcon">Init1()是在Native空间实现的（com_andoird_server_systemServer.cpp）。我们一看这个函数就知道了，原来这个闭合循环处理框架在这里：</p>
<p class="artcon">init1-&gt;system_init() @System_init.cpp</p>
<p class="artcon">在system_init()我们看到了这个久违的循环闭合管理框架。</p>
<p class="artcon">{</p>
<p class="artcon">Call "com/android/server/SystemServer", "init2"</p>
<p class="artcon">&#8230;.. </p>
<p class="artcon">ProcessState::self()-&gt;startThreadPool();</p>
<p class="artcon">IPCThreadState::self()-&gt;joinThreadPool();</p>
<p class="artcon">}</p>
<p class="artcon">Init2()@SystemServer.java中建立了Android中所有要用到的服务：</p>
<p class="artcon">Entropy Service</p>
<p class="artcon">Power Manager</p>
<p class="artcon">Activity Manager</p>
<p class="artcon">Telephony Registry</p>
<p class="artcon">Package Manager</p>
<p class="artcon">Account Manager</p>
<p class="artcon">Content Manager</p>
<p class="artcon">System Content Providers</p>
<p class="artcon">Battery Service</p>
<p class="artcon">Hardware Service</p>
<p class="artcon">Alarm Manager</p>
<p class="artcon">Init Watchdog</p>
<p class="artcon">Sensor Service</p>
<p class="artcon">Window Manager</p>
<p class="artcon">Bluetooth Service</p>
<p class="artcon">statusbar</p>
<p class="artcon">Clipboard Service</p>
<p class="artcon">Input Method Service</p>
<p class="artcon">NetStat Service</p>
<p class="artcon">Connectivity Service</p>
<p class="artcon">Accessibility Manager</p>
<p class="artcon">Notification Manager</p>
<p class="artcon">Mount Service</p>
<p class="artcon">Device Storage Monitor</p>
<p class="artcon">Location Manager</p>
<p class="artcon">Search Service</p>
<p class="artcon">Checkin Service</p>
<p class="artcon">Wallpaper Service</p>
<p class="artcon">Audio Service</p>
<p class="artcon">Headset Observer</p>
<p class="artcon">Backup Service</p>
<p class="artcon">AppWidget Service</p>
<p class="artcon"><strong>3 ProcessState和IPCThreadState</strong></p>
<p class="artcon">从宏观来讲，PocessState及其IPCThreadState处于IPC与内核打交道包装层。前面的章节已经提到，下面我将更详细的分析。有关IPC的c++空间的实现都是从ProcessState这个对象完成的。</p>
<p class="artcon"><img height="294" src="http://www.uml.org.cn/mobiledev/images/2011092822.gif" width="555"  alt="" /></p>
<p class="artcon">我们可以得出如下的结论：不管JVM的Binder做了多么复杂的操作，最终还是需要利用ProcessState 这个c++空间的对象把数据传递给Binder Driver，接收数据也是通过ProcessState这个对象完成，ProcessState是所有Binder IPC必经的通道。</p>
<p class="artcon"><img height="294" src="http://www.uml.org.cn/mobiledev/images/2011092823.gif" width="471"  alt="" /></p>
<p class="artcon">ProcessState放置在全局变量gProcess中，每个进程只有一个ProcessState对象，负责打开Binder设备驱动，建立线程池等。而IPCThreadState每个线程有一个，IPCThreadState实例登记在Linux线程程的上下文附属数据中，主要负责Binder数据读取，写入和请求处理框架。IPCThreadSate在构造的时候，获取进程的ProcessSate并记录在自己的成员变量mProcess中,通过mProcess可以获取到Binder的句柄。</p>
<p class="artcon">3.1 ProcessState的生命周期</p>
<p class="artcon">既然ProcessState是Binder通讯的基础，那么Process必须在Binder通讯之前建立。客户端，服务端都必须建立。由于现在重点讨论服务端，所以重心放置在服务端。在Android体系中有c++空间的服务，JVM空间的服务，这两类服务在本质上相同的，只是形式上不同，由于他们都是建立在ＰｒｏｃｅｓｓＳｔａｔｅ这个基础上，所以在形式上不同就仅仅表现在对OnTransact的回调处理的不同。</p>
<p class="artcon">Native Service </p>
<p class="artcon">我们直接可以看到使用sp&lt;ProcessState&gt; proc(ProcessState::self())，建立建立ProcessState，一旦调用ProcessState就建立了，并且这个self将ProcessSate登记在全局变量中。</p>
<p class="artcon">Android Service</p>
<p class="artcon">建立Android Service服务system_init @System_init.cpp中我们可以看到相同的结构。有一点不同的是所有的Android Service都运行在一个进程中：systemsever进程。</p>
<p class="artcon">3.2 Binder Driver包装 @IPCThreadState</p>
<p class="artcon">ProcessSate构造的时候，使用open_binder打开/driver/binder，并将句柄记录在mDriverFD，在ProcessState中并不使用这个句柄，真正使用这个Binder设备句柄的是IPCThreadState，所有关于Binder的操作放置在IPCThreadState中：</p>
<p class="artcon">(1)读取/写入：talkWithDriver（）@IPCThreadState对ioctl(mProcess-&gt;mDriverFD, BINDER_WRITE_READ, &amp;bwr)进行包装。</p>
<p class="artcon">(2)请求处理：executeCommand(...)@ IPCThreadState</p>
<p class="artcon">(3)循环结构：joinThreadPool()</p>
<p class="artcon">joinThreadPool()</p>
<p class="artcon">{</p>
<p class="artcon">While(1){</p>
<p class="artcon">talkWithDriver(...)</p>
<p class="artcon">...</p>
<p class="artcon">executeCommand(...)</p>
<p class="artcon">}</p>
<p class="artcon">}</p></td></tr></tbody></table><br /></td></tr></tbody></table><img src ="http://www.cppblog.com/life02/aggbug/161747.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-08 11:38 <a href="http://www.cppblog.com/life02/archive/2011/12/08/161747.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android核心分析（转）</title><link>http://www.cppblog.com/life02/archive/2011/12/08/161746.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Thu, 08 Dec 2011 03:37:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/08/161746.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161746.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/08/161746.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161746.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161746.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2011/12/08/161746.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/161746.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-08 11:37 <a href="http://www.cppblog.com/life02/archive/2011/12/08/161746.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>android应用开发之intent的妙用二（转）</title><link>http://www.cppblog.com/life02/archive/2011/12/07/161707.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Wed, 07 Dec 2011 15:27:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/07/161707.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161707.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/07/161707.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161707.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161707.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/life02/archive/2011/12/07/161707.html'>阅读全文</a><img src ="http://www.cppblog.com/life02/aggbug/161707.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-07 23:27 <a href="http://www.cppblog.com/life02/archive/2011/12/07/161707.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>android应用开发之intent的妙用一(转)</title><link>http://www.cppblog.com/life02/archive/2011/12/07/161706.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Wed, 07 Dec 2011 15:26:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/07/161706.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161706.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/07/161706.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161706.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161706.html</trackback:ping><description><![CDATA[<div><strong><div>http://blog.csdn.net/louiswangbing/article/details/6607612</div><br />Intent妙用之编写自己的android主界面</strong><p>众所周知，android的主界面名为 laucher2，功能强大，会在android启动之后运行，也是所有其他应用程序的入口程序。那么，如何让自己的应用程序取代laucher2的位 置，成为android系统的主界面呢？其实说出来很简单，接下来，我就来为大家揭开这其中的神秘面纱。</p><p>首先，我们来看看一个普通的应用程序的intent声明：</p><ol start="1" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #5c5c5c; "><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&lt;intent-filter&gt;&nbsp;&nbsp;</span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;action&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.action.MAIN"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.LAUNCHER"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/intent-filter&gt; &nbsp;</span></li></ol>是不是看着很眼熟呢？没错了，这段代码是在AndroidManifest.xml中相应activity的intent声明，相信大家目前缩写的应用程序的intent声明无一例外都是这样的吧。可能很多人都会对着两行代码有下面的理解：<p>第二行<span style="line-height: 18px; ">&lt;action&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.action.MAIN"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;表示这个activity是当前应用程序的主activity，而第二行表示当前activity在lancher中加载。</span></span></p><p><span style="line-height: 18px;">这么理解的却是没错，但事实上，有更加简单的理解方式。这里请容许我先卖个关子。我们继续往下看。</span></p><p><span style="line-height: 18px;">既然是想让我们自己的应用程序取代laucher的位置，那么我们就来看一下lancher的intent声明是怎么样的吧：</span></p><p><span style="line-height: 18px;"></span></p><ol start="1" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #5c5c5c; "><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&lt;intent-filter&gt;&nbsp;&nbsp;</span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;action&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.action.MAIN"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.HOME"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.DEFAULT"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.MONKEY"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&lt;/intent-filter&gt;&nbsp;</span></li></ol>当 然，想要看到这段代码，你必须要有laucher的源代码。（关于android源代码的问题，前面的文章已经提到过很多次了，有源代码真的是件很好的事 情，不管是对应用程序开发者来说还是对底层开发者来说亦或是对android爱好者来说，都会受益匪浅的，所以这里再次建议大家不要仅仅局限于 android sdk和avd的使用来进行应用程序的开发，不管你有没有时间，下份源代码总是没有坏处的。）<p><span style="line-height: 18px;">大家看出这两个intent声明之间的差别了吗？没错，当出现<span style="line-height: 18px; ">&nbsp; &lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.HOME"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">/&gt;   &nbsp;的时候，我们的应用程序就会变成跟lancher具有相同功能的（当然，我指的仅仅是主界面，laucher的功能是在是太强大了），成为 android系统的主界面，并且，当我们按下Home键的时候，会出现一个选择界面，是不是发现你的应用程序和laucher同时出现在了选择框中呢？ 如果是，那么恭喜你，从功能上来说，你已经实现了。如果没有看到，请email我louiswangbing@gmail.com。</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">但是，所谓一山不容二虎，同时有两个主界面存在当然不是我们想要的。</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">如 果你有android源代码的话，你甚至可以把laucher删掉，直接将你自己的应用程序编译到android系统中，这样，系统启动的时候就会直接运 行你的应用程序，而你的应用程序就会堂而皇之地鸠占鹊巢，顺理成章的成为系统的主界面，并且当你按下Home键的时候，就会跳到你的应用程序的主 activity了！！</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">如果你只是一个功能上的追求者，那么看到这里你就可以关掉这个页面去试试上面的功能了。</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">如果你是一个好奇心比较强的人，那么请跟着我继续往下看。</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">刚才我说过，intent声明的理解问题。事实上，你可以有更好的理解方式，当然，这要建立在你对android系统的启动进程有一定的了解的基础上。</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">简 单来说，intent其实就是一个条件过滤器，activity的intent声明的每一条都可以作为一个过滤条件，条目越多，过滤条件也就越强，定位起 来也就越容易。相信这个原理学过数据库的朋友理解起来更加容易。你可以将整个android系统中所有的activity都集中起来当成是一个数据库，而 intent本身就是一条select语句，其中每一项声明都是一项过滤条件，而过滤之后剩下的，就是将要被调用的activity。当过滤条件足够强大 或者过滤条件比较特殊的时候，最后剩下的只有一个activity，那么系统会毫不犹豫地启动它；当过滤条件不足导致过滤之后还剩下比较多的 activity的时候，系统会将满足这些条件的所有的activity用一个listview列出来让你选择。</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">相信原理大家都已经很清楚了，那么上面所述的这个功能就很好理解了。没错，android在启动的时候会有一个PackageManager选择系统中满足过滤条件：</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "></span></span></span></p><ol start="1" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #5c5c5c; "><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&lt;action&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.action.MAIN"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.HOME"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.DEFAULT"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li><li style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; 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-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-color: #6ce26c; background-color: #ffffff; color: inherit; line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;category&nbsp;android:name=<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: blue; background-color: inherit; ">"android.intent.category.MONKEY"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&nbsp;/&gt; &nbsp;</span></span></li></ol>的 activity作为系统启动的第一个activity，也就是主界面，任何一个满足这个过滤条件的activity都将进入候选名单，如果候选名单中仅 有一人，那么很荣幸，不战而胜，你就是系统的主界面了；如果候选名单中有多个人（比如刚才说的有laucher和你自己的应用程序），那么就进入残酷的&#8221; 民意选举&#8220;，也就是用户自己的选择，你选谁系统就会启动谁。但和民意选举不同的是，选举出来的结果通常会有一定的任期，而这个主界面的选举任期非常的短， 每当你按下一次Home键或者重启一下系统，系统就会重新安排一次&#8221;民意选举&#8220;。如果想要永远的成为主界面，那么唯一的办法就是--没错，虽然很残酷，但 不得不说--就是，干掉它......或者和谐一点，你可以让它活着，但是也必须将它贬为庶民或者是将其发配边疆，具体的实施手段就是--改掉它的 intent声明，使其不满足主界面的这个过滤条件。<p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">在 整个intent妙用章节中，这一章算是比较特殊的，因为虽然所有的章节运用的原理基本相通，也就是我上面说到的原理，但是后续章节的intent过滤条 件都是可以由用户自己定义、手动修改的，而唯独只有这个是不能改的，你只能决定去适应或者不适应这个过滤条件，而不能去改变这个过滤条件，就像你只能去适 应这个社会，却很难改变它--或许不是不能，而是暂时还能找到途径......</span></span></span></p><p><span style="line-height: 18px;"><span style="line-height: 18px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">有时候真的，生活跟计算机，居然有如此之多的相似之处......</span></span></span></p></div><br /> <div><embed id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" height="0" hidden="true" width="0"></div><img src ="http://www.cppblog.com/life02/aggbug/161706.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-07 23:26 <a href="http://www.cppblog.com/life02/archive/2011/12/07/161706.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android开发组件——Intent（转）</title><link>http://www.cppblog.com/life02/archive/2011/12/07/161705.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Wed, 07 Dec 2011 15:25:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/07/161705.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161705.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/07/161705.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161705.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161705.html</trackback:ping><description><![CDATA[<div><div id="article_content"><p align="center"><strong><br /></strong></p>http://blog.csdn.net/louiswangbing/article/details/6610936<p align="center"><strong>Android开发组件&#8212;&#8212;Intent</strong></p><p align="left">Intent提供了一种通用的消息系统，允许你的应用程序与其它的应用程序间传递Intent来执行动作和产生事件。通过使用Intent可以激活Android应用的三个核心组件：活动、服务和广播接收器。</p><p align="left">Intent 的意思是意图，也就是说App通过Intent向android表达自己的意图即想干什么，想启动另一个Activity或者去调用其它的App等 等；Intentfilter反应了App对意图的响应能力，比如书上的例子拨号程序，在自己的App中声明一个Intentfilter对这个动作意图 进行相应，这样当我们按下拨号键时，android就会在所用的Intentfilter中找到合适的App来满足。</p><p align="left">&#165;&nbsp; <strong>Intent的分类</strong></p><p align="left">&#216;&nbsp; <strong>显示意图</strong>：</p><p align="left">调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显示意图，明确指定了Intent应该传递到哪个组件。</p><p align="left">&#216;&nbsp; <strong>隐式意图</strong></p><p align="left">没有明确指定组件名的Intent为隐式意图。Android系统会根据隐式意图中设置的动作、类别、数据找到最适合的组件来处理这个意图</p><p align="left">&#165;&nbsp; <strong>Intent究竟是什么</strong></p><p align="left">我 们可以将Intent看成是Activity通信的标准的。比如Intent中的内容告诉了系统激发Intent的Activity需要什么服务，而服务 者Activity应该满足的条件。然后就是Android系统的事了，它负责找出符合条件的Activity服务者，并将Intent给 Activity服务者，然后这个Activity根据Intent中剩余的信息做出相应的操作。由上面可知，Intent包含两部分信息：</p><p align="left">&#216;&nbsp; Activity服务者的信息，这个是给Android用来找到合适Activity的。</p><p align="left">&#216;&nbsp; Activity服务者要做什么操作的信息，这个是给Activity服务者用的。</p><p align="left">&#165;&nbsp; <strong>Intent</strong></p><p align="left">Android 中提供了Intent机制来协助应用间的交互与通讯，Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述，Android则根据此 Intent的描述，负责找到对应的组件，将Intent传递给调用的组件，并完成组件的调用。Intent不仅可用于应用程序之间，也可用于应用程序内 部的Activity/Service之间的交互。因此，Intent在这里起着一个媒体中介的作用，专门提供组件互相调用的相关信息，实现调用者与被调 用者之间的解耦。</p><p align="left">&#165;&nbsp; <strong>Intent的属性</strong></p><p align="left">&#216;&nbsp; <strong>Action</strong></p><p align="left">表示要执行的动作，当然也可以自定义动作，并可定义相应的Activity</p><p align="left">来处理我们的自定义动作。</p><p align="left">&#216;&nbsp; <strong>Data</strong></p><p align="left">也就是执行动作要操作的数据。Android中采用指向数据的一个URI</p><p align="left">来表示。如在联系人应用中，一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作，其URI数据的类型是不同的（可以设置type属性指定特定类型数据），如ACTION_EDIT指定Data为文件URI，打电话为<a href="tel:URI">tel:URI</a>，访问网络为http:URI，而由content provider提供的数据则为content:URIs。</p><p align="left">&nbsp;如果动作字段是ACTION_EDIT，数据字段包含将显示用于编辑的文</p><p align="left">档的URI；如果动作时ACTION_CALL。数据字段将是一个<a href="tel:URI%E5%92%8C%E5%B0%86%E6%8B%A8">tel:URI和将拨</a>&nbsp; 打的号码；如果动作是ACTION_VIEW，数据字段是一个http:URI，接收活动将被调用去下载和显示URI指向的数据。</p><p align="left">&#216;&nbsp; <strong>Type</strong></p><p align="left">数据类型，显示指定Intent的数据类型（MIME）。一般Intent的数据</p><p align="left">类型能够根据数据本身进行判断，但是通过设置这个属性，可以强制采用显式指定的类型而不再进行推导。</p><p align="left">&#216;&nbsp; <strong>Category</strong></p><p align="left">被执行动作的附加信息。例如LAUNCHER_CATEGORY表示intent的接</p><p align="left">受 者应该在Launcher中作为顶级应用出现；而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个，这些动作 可以在同一数据上执行。Alternative表明，这个Activity可以变成OptionMenu，供其他Activity直接调用。</p><p align="left">&#216;&nbsp; <strong>Component</strong></p><p align="left">指定Intent的目标组件的类名称。通常Android会根据Intent中包含</p><p align="left">的 其它属性的信息，比如action、data/type、category进行查找，最终找到一个与之匹配的目标组件。但是，如果component这个 属性有指定的话，将直接使用它指定的组件，而不再执行上述查找过程。指定了这个属性以后，Intent的其它所有属性都是可选的。</p><p align="left">&#216;&nbsp; <strong>Extra</strong></p><p align="left">是其它所有附加信息的集合。使用extras可以为组件提供扩展信息，</p><p align="left">比如，如果要执行&#8220;发送电子邮件&#8221;这个动作，可以将电子邮件的标题、正文等保存在extras里，传给电子邮件发送组件。</p><p align="left">Android需要解析的是那些隐式Intent，通过解析，将Intent映射给可以</p><p align="left">处理此Intent的Activity、IntentReceiver或Service。Intent解析机制主要是通</p><p align="left">过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent，</p><p align="left">最终找到匹配的Intent。在这个解析过程中，Android是通过Intent的action、</p><p align="left">type、category这三个属性来进行判断，判断方法如下：</p><p align="left">@&nbsp; 如果Intent指定了action，则目标组件的IntentFilter的action列表中就必须包含有这个action，否则就不能匹配。</p><p align="left">@&nbsp; 如果Intent没有提供type，系统将从data中得到数据类型。和action一样，目标组件的数据类型列表中必须包含Intent的数据类型，否则不能匹配。</p><p align="left">@&nbsp;   如果Intent中的数据不是content：类型的URI，而且Intent也没有明确指定它的type，将根据Intent中数据的scheme（比 如http:或者mailto:）进行匹配。同上，Intent的scheme必须出现在目标组件的scheme列表中。</p><p align="left">@&nbsp; 如果Intent指定了一个或多个category，这些类别必须全部出现在组件的类别列表中。比如Intent中包含了两个类别:LAUNCHER_CATEGORY和ALTERNATIVE_CATEGORY，解析得到的目标组件必须至少包含这两个类别。</p><p align="left">当匹配一个intent到一个能够处理数据的组件，通常知道数据的类型（它</p><p align="left">的MIME 类型）和它的URI很重要。例如，一个组件能够显示图像数据，不应该被调用去播放一个音频文件。</p><p align="left">&nbsp;在许多情况下，数据类型能够从URI中推测，特别是content:URIs,它表</p><p align="left">示 位于设备上的数据且被内容提供者(content  provider)控制。但是类型也能够显示地设置，setData()方法指定数据的URI，setType()指定MIME类 型，setDataAndType()指定数据的URI和MIME类型。通过getData()读取URI，getType()读取类型。</p><p align="left">&#165;&nbsp; <strong>Intent过滤器</strong></p><p align="left">活动、服务、广播接收者为了告知系统能够处理哪些隐式Intent，它们</p><p align="left">可 以有一个或多个intent过滤器。每个过滤器描述组件的一种能力，即乐意接收的一组Intent。实际上，它筛掉不想要的Intents，也仅仅是不想 要的隐式Intents。一个显示Intent总是能够传递到它的目标组件，不管它包含什么；不考虑过滤器。但是一个隐式Intent，仅当它能够通过组 件的过滤器之一才能够传递给它。</p><p align="left">一个组件能够做的每一工作有独立的过滤器，例如，记事本中的NoteEditer活动有两个过滤器，一个是启动一个指定的记录，用户可以查看和编辑；另一个是启动一个新的、空的记录，用户能够填充并保存。</p><p align="left">一 个intent过滤器是一个IntentFilter类的实例。因为Android系统在启动一个组件之前必须知道它的能力，但是intent过滤器通常 不在java代码中设置，而是在应用程序的清单文件(AndroidManifest.xml)中设置。但是有一个例外，广播接收者的过滤器通过调用 Context.registerReceiver()动态地注册，它直接创建一个IntentFilter对象。</p><p align="left">一 个过滤器有对应于Intent对象的动作、数据、种类的字段。过滤器要检测隐式Intent的所有这三个字段，其中任何一个失败，Android系统都不 会传递Intent给组件。然而，因为一个组件可以有多个Intent过滤器，一个Intent通不过组件的过滤器检测，其它的过滤器可能通过检测。</p><p align="left">一个过滤器必须至少包含一个&lt;action&gt;子元素，否则它将阻塞所有的Intents。</p><p align="left">要通过检测，Intent对象中指定的动作必须匹配过滤器的动作列表中的一个。如果对象或过滤器没有指定一个动作，结果将如下：</p><p align="left">&nbsp;&nbsp; 如果过滤器没有指定动作，没有一个Intent将匹配，所有的Intent将检测失败，即没有intent能够通过过滤器。</p><p align="left">&nbsp;&nbsp; 如果Intent对象没有指定动作，将自动通过检查（只要过滤器至少有一个过滤器，否则就是上面的情况了）</p><p align="left">对于一个Intent要通过种类检测，Intent对象中的每个种类必须匹配过</p><p align="left">滤器中的一个。即过滤器能够列出额外的种类，但是Intent对象中的种类都必须能够在过滤器中找到，只有一个种类在过滤器列表中没有，就算种类检测失败!</p><p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因此原则上如果一个Intent对象中没有种类（即种类字段为空）应该总</p><p align="left">是 通过种类测试，而不管过滤器中有什么种类。但是有个例外，Android对待所有传递给Context.startActivity()的隐式 Intent好像它们至少包含&#8221;android.intent.category.DEFAULT&#8221;(对应CATEGORY_DEFAULT常量)。因 此，活动想要接收隐式Intent必须要在Intent过滤器中包含&#8221;android.intent.category.DEFAULT&#8221;。</p><p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Intents对照着Intent过滤器匹配，不仅去发现一个目标组件去激活，而且</p><p align="left">去 发现设备上的组件的其他信息。例如，Android系统填充应用程序启动列表，最高层屏幕显示用户能够启动的应用程序：是通过查找所有的包含指定 了&#8221;android.intent.action.MAIN&#8221;的动作和&#8221;android.intent.category.LAUNCHER&#8221; 种&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 类的过滤器的活动，然后在活动列表中显示这些活动的图标和标签。类似的，&nbsp;&nbsp;  它通过查找&#8221;android.intent.category.HOME&#8221;过滤器的活动发掘主菜单。</p><p align="left">&#165;&nbsp; <strong>Intent示例</strong></p><p align="left">要实现两个Activity的跳转，我们可以将要跳转的Activity类名绑定到</p><p align="left">Intent对象中，然后通过startActivity方法激活Intent对象中所指定的Activity，关键代码如下：</p> </div></div><br /> <div><embed id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" height="0" hidden="true" width="0"></div><img src ="http://www.cppblog.com/life02/aggbug/161705.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-07 23:25 <a href="http://www.cppblog.com/life02/archive/2011/12/07/161705.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>context(转)</title><link>http://www.cppblog.com/life02/archive/2011/12/05/161514.html</link><dc:creator>life02</dc:creator><author>life02</author><pubDate>Mon, 05 Dec 2011 09:13:00 GMT</pubDate><guid>http://www.cppblog.com/life02/archive/2011/12/05/161514.html</guid><wfw:comment>http://www.cppblog.com/life02/comments/161514.html</wfw:comment><comments>http://www.cppblog.com/life02/archive/2011/12/05/161514.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/life02/comments/commentRss/161514.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/life02/services/trackbacks/161514.html</trackback:ping><description><![CDATA[很多初入Android开发的网友向我们问到Context有什么作用,很多地方都用到它，这里Android123给这些新入门的网友做个简单的解释：<br /><br />&nbsp;&nbsp; Context字面意思上下文，位于framework package的android.content.Context中，其实该类为LONG型，类似Win32中的Handle句柄，很多方法需要通过Context才能识别调用者的实例，比如说Toast的第一个参数就是Context，一般在Activity中我们直接用this代替，代表调用者的实例为Activity，而到了一个button的onClick(View view)等方法时，我们用this时就会报错，所以我们可能使用ActivityName.this来解决，主要原因是因为实现Context的类主要有Android特有的几个模型，Activity、Service以及BroadcastReceiver。<br /><br />&nbsp; 常规需要Context实例的方法主要有各种Service实现的类，比如说SensorManager在实例化时需要getSystemService(String)方法就必须由Context的实例执行，还有一些私有的文件系统I<span style="color: #000000">/</span><span style="color: #000000">O比如说openFileInput以及常用的Toast的makeText方法。<br /><br />在android中context可以作很多操作，但是最主要的功能是加载和访问资源。在android中有两种context，一种是 application context，一种是activity context，通常我们在各种类和方法间传递的是activity context。<br />比如一个activity的onCreate：<br /><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> onCreate(Bundle state) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super.onCreate(state);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextView label </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> TextView(</span><span style="color: #0000ff">this</span><span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">传递context给view control</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label.setText(</span><span style="color: #800000">"</span><span style="color: #800000">Leaks are bad</span><span style="color: #800000">"</span><span style="color: #000000">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setContentView(label);<br />}<br />把activity context传递给view，意味着view拥有一个指向activity的引用，进而引用activity占有的资源：view hierachy, resource等。<br />这样如果context发生内存泄露的话，就会泄露很多内存。<br />这里泄露的意思是gc没有办法回收activity的内存。<br /><br />Leaking an entire activity是很容易的一件事。<br /><br />当屏幕旋转的时候，系统会销毁当前的activity，保存状态信息，再创建一个新的。<br /><br />比如我们写了一个应用程序，它需要加载一个很大的图片，我们不希望每次旋转屏 幕的时候都销毁这个图片，重新加载。实现这个要求的简单想法就是定义一个静态的Drawable，这样Activity 类创建销毁它始终保存在内存中。<br />实现类似：<br /></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> myactivity extends Activity {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> Drawable sBackground;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> onCreate(Bundle state) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super.onCreate(state);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextView label </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> TextView(</span><span style="color: #0000ff">this</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label.setText(</span><span style="color: #800000">"</span><span style="color: #800000">Leaks are bad</span><span style="color: #800000">"</span><span style="color: #000000">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (sBackground </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sBackground </span><span style="color: #000000">=</span><span style="color: #000000"> getDrawable(R.drawable.large_bitmap);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label.setBackgroundDrawable(sBackground);</span><span style="color: #008000">//</span><span style="color: #008000">drawable attached to a view</span><span style="color: #008000"><br /></span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setContentView(label);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />}<br />这段程序看起来很简单，但是却问题很大。当屏幕旋转的时候会有leak（即gc没法销毁activity）。<br />我们刚才说过，屏幕旋转的时候系统会销毁当前的activity。但是当drawable和view关联后，drawable保存了view的 reference，即sBackground保存了label的引用，而label保存了activity的引用。既然drawable不能销毁，它所 引用和间接引用的都不能销毁，这样系统就没有办法销毁当前的activity，于是造成了内存泄露。gc对这种类型的内存泄露是无能为力的。<br /><br />避免这种内存泄露的方法是避免activity中的任何对象的生命周期长过activity，避免由于对象对 activity的引用导致activity不能正常被销毁。我们可以使用application context。application context伴随application的一生，与activity的生命周期无关。application context可以通过Context.getApplicationContext或者Activity.getApplication方法获取。<br /><br />避免context相关的内存泄露，记住以下几点：<br /></span><span style="color: #800080">1</span><span style="color: #000000">. 不要让生命周期长的对象引用activity context，即保证引用activity的对象要与activity本身生命周期是一样的<br /></span><span style="color: #800080">2</span><span style="color: #000000">. 对于生命周期长的对象，可以使用application context<br /></span><span style="color: #800080">3</span><span style="color: #000000">. 避免非静态的内部类，尽量使用静态类，避免生命周期问题，注意内部类对外部对象引用导致的生命周期变化<br /></span><br /><!--End_rbody_71633761//--><br /></span><br /> <div><embed id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" height="0" hidden="true" width="0"></div><img src ="http://www.cppblog.com/life02/aggbug/161514.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/life02/" target="_blank">life02</a> 2011-12-05 17:13 <a href="http://www.cppblog.com/life02/archive/2011/12/05/161514.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>