<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5952752232226176368</id><updated>2012-02-16T02:55:00.510-08:00</updated><title type='text'>My Ubuntu</title><subtitle type='html'>This is All about Ubuntu.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>50</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-7784715158651209868</id><published>2010-11-02T07:42:00.001-07:00</published><updated>2010-11-02T07:42:52.728-07:00</updated><title type='text'>本机上创建多个基于域名的虚拟主机</title><content type='html'>这是一些配置文件，使用这些配置文件后，就可使用eclipse来访问Eclipse建立的项目目录，而使用old则访问用户目录中的Apache2目录。&lt;br&gt;这是一个使用Apache2的基于域名的多个虚拟主机创建实例。但遗憾的是Apache在运行时，会有一个小小的警告&lt;br&gt; * Reloading web server config apache2&lt;br&gt;         apache2: Could not reliably determine the server&amp;#39;s fully&lt;br&gt;qualified domain name, using 127.0.1.1 for ServerName&lt;br&gt;不过，这些都素浮云，因为伊可以正常运行了。&lt;br&gt;127.0.0.1	localhost&lt;br&gt;127.0.1.1	westnorth-tablet&lt;br&gt;127.0.1.2	eclipse&lt;br&gt;127.0.1.3	old&lt;br&gt;192.168.1.100	pc1&lt;br&gt;# The following lines are desirable for IPv6 capable hosts&lt;br&gt;::1     localhost ip6-localhost ip6-loopback&lt;br&gt;fe00::0 ip6-localnet&lt;br&gt;ff00::0 ip6-mcastprefix&lt;br&gt;ff02::1 ip6-allnodes&lt;br&gt;ff02::2 ip6-allrouters&lt;br&gt;ff02::3 ip6-allhosts&lt;br&gt;/////////////etc/hosts&lt;p&gt;&lt;br&gt;#NameVirtualHost 127.0.1.3&lt;br&gt;&amp;lt;VirtualHost *:80&amp;gt;&lt;br&gt;        ServerAdmin webmaster@localhost&lt;br&gt;#	ServerName old&lt;br&gt;        DocumentRoot /home/westnorth/apache2&lt;br&gt;        &amp;lt;Directory /&amp;gt;&lt;br&gt;                Options FollowSymLinks&lt;br&gt;                AllowOverride None&lt;br&gt;        &amp;lt;/Directory&amp;gt;&lt;br&gt;        &amp;lt;Directory /home/westnorth/apache2/&amp;gt;&lt;br&gt;                Options Indexes FollowSymLinks MultiViews&lt;br&gt;                AllowOverride None&lt;br&gt;                Order allow,deny&lt;br&gt;                allow from all&lt;br&gt;        &amp;lt;/Directory&amp;gt;&lt;p&gt;        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/&lt;br&gt;        &amp;lt;Directory &amp;quot;/usr/lib/cgi-bin&amp;quot;&amp;gt;&lt;br&gt;                AllowOverride None&lt;br&gt;                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch&lt;br&gt;                Order allow,deny&lt;br&gt;                Allow from all&lt;br&gt;        &amp;lt;/Directory&amp;gt;&lt;br&gt;      ErrorLog /var/log/apache2/error.log&lt;p&gt;        # Possible values include: debug, info, notice, warn, error, crit,&lt;br&gt;        # alert, emerg.&lt;br&gt;        LogLevel warn&lt;p&gt;        CustomLog /var/log/apache2/access.log combined&lt;p&gt;    Alias /doc/ &amp;quot;/usr/share/doc/&amp;quot;&lt;br&gt;    &amp;lt;Directory &amp;quot;/usr/share/doc/&amp;quot;&amp;gt;&lt;br&gt;        Options Indexes MultiViews FollowSymLinks&lt;br&gt;        AllowOverride None&lt;br&gt;        Order deny,allow&lt;br&gt;        Deny from all&lt;br&gt;        Allow from &lt;a href="http://127.0.0.0/255.0.0.0"&gt;127.0.0.0/255.0.0.0&lt;/a&gt; ::1/128&lt;br&gt;    &amp;lt;/Directory&amp;gt;&lt;p&gt;&amp;lt;/VirtualHost&amp;gt;&lt;br&gt;#########################/etc/apache2/default&lt;p&gt;&lt;p&gt;NameVirtualHost 127.0.1.2&lt;br&gt;&amp;lt;VirtualHost 127.0.1.2&amp;gt;&lt;br&gt;        ServerAdmin webmaster@localhost&lt;br&gt;	ServerName eclipse&lt;br&gt;        DocumentRoot /home/westnorth/workspace/php&lt;br&gt;        &amp;lt;Directory /&amp;gt;&lt;br&gt;                Options FollowSymLinks&lt;br&gt;                AllowOverride None&lt;br&gt;        &amp;lt;/Directory&amp;gt;&lt;br&gt;        &amp;lt;Directory /home/westnorth/workspace/php/&amp;gt;&lt;br&gt;                Options Indexes FollowSymLinks MultiViews&lt;br&gt;                AllowOverride None&lt;br&gt;                Order allow,deny&lt;br&gt;                allow from all&lt;br&gt;        &amp;lt;/Directory&amp;gt;&lt;p&gt;        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/&lt;br&gt;        &amp;lt;Directory &amp;quot;/usr/lib/cgi-bin&amp;quot;&amp;gt;&lt;br&gt;                AllowOverride None&lt;br&gt;                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch&lt;br&gt;                Order allow,deny&lt;br&gt;                Allow from all&lt;br&gt;        &amp;lt;/Directory&amp;gt;&lt;br&gt;      ErrorLog /var/log/apache2/error.log&lt;p&gt;        # Possible values include: debug, info, notice, warn, error, crit,&lt;br&gt;        # alert, emerg.&lt;br&gt;        LogLevel warn&lt;p&gt;        CustomLog /var/log/apache2/access.log combined&lt;p&gt;    Alias /doc/ &amp;quot;/usr/share/doc/&amp;quot;&lt;br&gt;    &amp;lt;Directory &amp;quot;/usr/share/doc/&amp;quot;&amp;gt;&lt;br&gt;        Options Indexes MultiViews FollowSymLinks&lt;br&gt;        AllowOverride None&lt;br&gt;        Order deny,allow&lt;br&gt;        Deny from all&lt;br&gt;        Allow from &lt;a href="http://127.0.0.0/255.0.0.0"&gt;127.0.0.0/255.0.0.0&lt;/a&gt; ::1/128&lt;br&gt;    &amp;lt;/Directory&amp;gt;&lt;p&gt;&amp;lt;/VirtualHost&amp;gt;&lt;br&gt;#############################/etc/apache2/site-availvabale/eclipse&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-7784715158651209868?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/7784715158651209868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/blog-post_02.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/7784715158651209868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/7784715158651209868'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/blog-post_02.html' title='本机上创建多个基于域名的虚拟主机'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-1968931676613715955</id><published>2010-11-01T06:08:00.001-07:00</published><updated>2010-11-01T06:08:23.079-07:00</updated><title type='text'>再续</title><content type='html'>&lt;span style=""&gt;&lt;/span&gt;记忆力越来越差了，证明了记笔记的重要性。没自己写，找了个现成的，注意红色部分。&lt;br&gt;&lt;br&gt;引用：&lt;br&gt;随便找个项目里的py文件以"Python Run"方式运行一下。然后右键点项目，选择"Debug as"，在第2个输入框右侧点"Browser"，选择Google App Engine文件夹里的dev_appserver.py文件（最终路径类似于${workspace_loc:keakon/appengine/dev_appserver.py}）。&lt;font class="Apple-style-span" color="#FF0000"&gt;切换到"Arguments"标签，填入"."（不含引号）。&lt;/font&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-1968931676613715955?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/1968931676613715955/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/blog-post_01.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1968931676613715955'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1968931676613715955'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/blog-post_01.html' title='再续'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-4532800967817792728</id><published>2010-11-01T06:04:00.001-07:00</published><updated>2010-11-01T06:04:36.423-07:00</updated><title type='text'>前篇之续</title><content type='html'>前面有点内容没有提，补一下。&lt;p&gt;安装Pydev后，还需要在Eclipse中设置一下Python的运行环境。&lt;br&gt;打开菜单windows-&amp;gt;preference,在Interpreter-Python中，单击New按钮，选择Python的安装位置，Ubuntu的在/usr/bin/python，下面的system&lt;br&gt;PYTHONPATH中会出现Python的的系统库的一些目录，要注意的是里面的版本号应该是2.5.这样，就完成了Pydev的设置。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-4532800967817792728?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/4532800967817792728/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/blog-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4532800967817792728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4532800967817792728'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/blog-post.html' title='前篇之续'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-1591149448468989701</id><published>2010-11-01T05:59:00.000-07:00</published><updated>2010-11-01T06:00:16.015-07:00</updated><title type='text'>使用pydev插件在Eclipse调用Google App Engine开发Python</title><content type='html'>突然对Google App Engine（GAE）感兴趣，于是准备在Eclipse中进行试验，但其实噩梦刚刚开始。&lt;br&gt;遇到的问题有：&lt;br&gt;1.GAE在Eclipse中插件无法正常安装。&lt;br&gt;2.Pydev建立的GAE项目无法正常运行。&lt;br&gt;安装GAE插件，可以使用地址：&lt;a href="http://dl.google.com/eclipse/plugin/3.6"&gt;http://dl.google.com/eclipse/plugin/3.6&lt;/a&gt;&lt;br&gt;，这是对应于Eclipse的3.6,如果是3.5中需要简单修改数字即可。&lt;br&gt;安装的方法是单击Eclipse的Help，选择&amp;quot;Install new&lt;br&gt;software&amp;quot;，然后，单击Add按钮，在Location中输入地址，在Name中输入描述信息，单击OK会显示可以安装的软件列表，选择要安装的软件，如果不确定，可以全部选择。然后进行安装。不知怎么搞的，我的Eclipse安装GAE插件始终会报错。不过，实践证明，在以后的使用过程中，发现可以使用Python进行开发。&lt;p&gt;现在我使用的是Ubuntu10.04,这个版本安装的Python是2.6版本，但GAE需要2.5,所以需要再建立一个2.5的Python。其他下载方法比较混乱，最简单的方法是加入一个有2.5的源即可。在/etc/apt/source.list中加入deb&lt;br&gt;&lt;a href="http://security.ubuntu.com/ubuntu"&gt;http://security.ubuntu.com/ubuntu&lt;/a&gt; hardy-security&lt;br&gt;main,然后，update一下，就可以使用apt-get进行安装了。截至目前（2010年11月1日）为至，最新的SDK还不太支持Python2.6,所以安装2.5还是必须的。&lt;p&gt;现在安装Pydev,在Eclipse中的安装地址是&lt;a href="http://pydev.sf.net/updates/%E3%80%82"&gt;http://pydev.sf.net/updates/。&lt;/a&gt;这个比较简单。&lt;p&gt;现在开始新建一个项目，可以选择Pydev中的GAE项目，创建的过程也比较简单。复杂的是如何让它运行起来。&lt;br&gt;一般来说，应遵循如下步骤：&lt;br&gt;1.首先，选择一个Python文件，单击Run按钮，使其运行一次。&lt;br&gt;2.右键单击项目，选择新建文件夹，在Folder Name中随便输入一个名字，如GAE。&lt;br&gt;3.单击Advanced按钮，选择Link to the Alternate Location。单击其后的Browser，选择GAE的目录。&lt;br&gt;前面是为了将GAE链接到工程中来。&lt;br&gt;4.右键单击项目，选择Debug as-》debug configuration，在Main&lt;br&gt;Module中，选择GAE目录（刚才新建的包含了GAE SDK的目录）中的dev_appserver.py文件。&lt;br&gt;5.单击Debug按钮则服务开始运行。&lt;br&gt;6.在运行前，可能在Console中会出现一些提示，问要不要在运行前更新之类，输入yes或no（按自己的需要）&lt;br&gt;7.在运行提示中，会发现服务运行于哪个端口，一般是8080。在浏览器中输入 localhost:8080，就可以看到一个简单的示例输出。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-1591149448468989701?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/1591149448468989701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/pydeveclipsegoogle-app-enginepython.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1591149448468989701'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1591149448468989701'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/11/pydeveclipsegoogle-app-enginepython.html' title='使用pydev插件在Eclipse调用Google App Engine开发Python'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-5823990100248852922</id><published>2010-08-20T19:53:00.000-07:00</published><updated>2010-08-20T19:54:20.920-07:00</updated><title type='text'>使用git同步代码</title><content type='html'>&lt;div&gt;git是一个代码同步工具。&lt;br&gt;&lt;ol&gt;&lt;li&gt;1.安装git&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;Ubuntu中的git-core有许多工具是没有的，所以应该下载一个git的deb包进行安装。&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;2.安装ssh&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;同步有许多方法，有http，也可以使用ssh。我使用的是ssh。&lt;/div&gt;&lt;div&gt;首先，安装一个ssh服务，名字叫openssh-server。安装后自动启动。&lt;/div&gt;  &lt;div&gt;然后在客户机上使用ssh-keygen -t rsa生成一个密钥，在生成时，密码掩码要留空，这样就可自动登录。&lt;/div&gt;&lt;div&gt;生成一个名为id_rsa.pub(?)，的文件，该 文件位于.ssh目录中，将这个文件发送到服务器的.ssh目录中，更名为authorized_keys，然后重新使用ssh登录，会发现已经不要密码了。现在准备工作作完，开始部署服务器。&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;3.部署git服务&lt;/li&gt;  &lt;/ol&gt;首先，要在服务器端创建名为/var/cache/git/workspace.git的目录，这个是必须的。&lt;/div&gt;&lt;div&gt;然后要给该目录一个权限。&lt;/div&gt;&lt;div&gt;cd &amp;nbsp;/var/cache/git/workspace.git&lt;/div&gt;&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;div&gt;chown -R root:www-data ./&lt;/div&gt;  &lt;div&gt;而且要将当前用户加入到www-data组之中。&lt;/div&gt;&lt;div&gt;在该目录中生成一个git反响：&lt;/div&gt;&lt;div&gt;git --bare init&lt;/div&gt;&lt;div&gt;chmod g+rwx -R ./&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;4.客户端向服务器添加代码&lt;/li&gt;&lt;/ol&gt;客户端操作：&lt;/div&gt;&lt;div&gt;进入一个不是workspace的目录。创设是code。&lt;/div&gt;&lt;div&gt;执行：&lt;/div&gt;&lt;div&gt;git clone ssh://&lt;a href="http://192.168.1.100/var/cache/git/workspace.git"&gt;192.168.1.100/var/cache/git/workspace.git&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;会将服务器同步下来。由于服务器中并无代码，所以我们还需要加入一些代码。&lt;/div&gt;&lt;div&gt;（1）将代码复制到code目录。&lt;/div&gt;&lt;div&gt;（2）git add ./，这样就会将当前目录中的所有代码添加到git中。&lt;/div&gt;&lt;div&gt;（3）git commit -m &amp;#39;init&amp;#39; ,第2步并未真正提交代码，这是提交的命令&lt;/div&gt;&lt;div&gt;&amp;nbsp;(4)git push origin master ，将本地作为master分支提交到服务器的origin中。这两个关键词都不能更改。这是第一次，执行完后，以后就可直接git push了。&lt;/div&gt;  &lt;div&gt;现在，就将本地的代码同步到服务器上了。&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;5.从任意客户端获取代码&lt;/li&gt;&lt;/ol&gt;现在，任意客户端（安装了git的机器），进入到一个要获取代码的目录。&lt;/div&gt;&lt;div&gt;然后，&lt;/div&gt;&lt;div&gt;git clone ssh://&lt;a href="http://192.168.1.100/var/cache/git/workspace.git"&gt;192.168.1.100/var/cache/git/workspace.git&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;就会将我们上传到服务器的代码同步到本机。&lt;/div&gt;&lt;div&gt;然后我们也可以参照上一节的操作上传代码了。&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; "&gt;  注意：Eclipse的代码有点特别。如果要从服务器同步代码的话。那么，不能将同步来的项目代码放置在这Eclipse的项目默认目录(一般是~/workspace），否则在打开项目文件时会报错。放置在另外的其他目录就没任何问题了。&lt;/blockquote&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;  &lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-5823990100248852922?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/5823990100248852922/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/git.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5823990100248852922'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5823990100248852922'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/git.html' title='使用git同步代码'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-3271443909897580516</id><published>2010-08-20T06:02:00.000-07:00</published><updated>2010-08-20T06:03:02.110-07:00</updated><title type='text'>使用按钮打开一个activity</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; color: rgb(51, 51, 51); "&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; line-height: 1.3em; "&gt;  For example, inside the&amp;nbsp;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;a href="../../../reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)" style="color: rgb(0, 102, 153); "&gt;onCreateOptionsMenu()&lt;/a&gt;&lt;/code&gt;&lt;/code&gt;&amp;nbsp;method, you can define a new menu item with an Intent like this:&lt;/p&gt;  &lt;pre class="prettyprint" style="color: rgb(0, 112, 0); font-family: monospace; line-height: inherit; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); background-color: rgb(250, 250, 250); padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 1em; overflow-x: auto; overflow-y: auto; "&gt;  &lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;MenuItem&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; menuItem &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; menu&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;add&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;0&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; PHOTO_PICKER_ID&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;0&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;&amp;quot;Select Photo&amp;quot;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;);&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;  menuItem&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;setIntent&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Intent&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;this&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;PhotoPicker&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;));&lt;/span&gt;&lt;/pre&gt;  &lt;pre class="prettyprint" style="color: rgb(0, 112, 0); font-family: monospace; line-height: inherit; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); background-color: rgb(250, 250, 250); padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 1em; overflow-x: auto; overflow-y: auto; "&gt;  &lt;font class="Apple-style-span" color="#666600"&gt;这是SDK中的一段话，意思是可以在创建菜单时使用SetIntent来使该项可打开一个Activity.&lt;/font&gt;&lt;/pre&gt;&lt;pre class="prettyprint" style="color: rgb(0, 112, 0); font-family: monospace; line-height: inherit; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); background-color: rgb(250, 250, 250); padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 1em; overflow-x: auto; overflow-y: auto; "&gt;  &lt;font class="Apple-style-span" color="#666600"&gt;&lt;br&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-3271443909897580516?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/3271443909897580516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/activity.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3271443909897580516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3271443909897580516'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/activity.html' title='使用按钮打开一个activity'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-3122202185644505450</id><published>2010-08-20T05:33:00.001-07:00</published><updated>2010-08-20T05:33:40.986-07:00</updated><title type='text'>关于组菜单的操作</title><content type='html'>在菜单创建代码后面加上如下一句，则会使一组菜单成为单选状态。&lt;div&gt;&amp;nbsp;menu_send.setGroupCheckable(0, true, true);&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;代码：&lt;/div&gt;&lt;div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;menu.setHeaderTitle(R.string.backup_conmenu);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SubMenu menu_send=menu.addSubMenu(&amp;quot;send&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.add(0,menu_email,0,&amp;quot;email&amp;quot;);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.add(0,menu_wifi,0,&amp;quot;wifi&amp;quot;);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.setGroupCheckable(0, true, true);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.setGroupEnabled(0, false);&lt;/div&gt;  &lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;SDK中的一句话应该如何理解：&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; color: rgb(51, 51, 51); "&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; line-height: 1.3em; "&gt;  This will display a checkbox with the menu item (unless it&amp;#39;s in the Icon Menu). When the item is selected, the&amp;nbsp;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;onOptionsItemSelected()&lt;/code&gt;&amp;nbsp;callback is called as usual. It is here that you must set the state of the checkbox. You can query the current state of the item with&amp;nbsp;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;a href="../../../reference/android/view/MenuItem.html#isChecked()" style="color: rgb(0, 102, 153); "&gt;isChecked()&lt;/a&gt;&lt;/code&gt;&lt;/code&gt;&amp;nbsp;and set the checked state with&amp;nbsp;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;a href="../../../reference/android/view/MenuItem.html#setChecked(boolean)" style="color: rgb(0, 102, 153); "&gt;setChecked()&lt;/a&gt;&lt;/code&gt;&lt;/code&gt;. Here&amp;#39;s what this looks like inside the&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;onOptionsItemSelected()&lt;/code&gt;&amp;nbsp;callback:&lt;/p&gt;  &lt;pre class="prettyprint" style="color: rgb(0, 112, 0); font-family: monospace; line-height: inherit; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); background-color: rgb(250, 250, 250); padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 1em; overflow-x: auto; overflow-y: auto; "&gt;  &lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;switch&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;item&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;getItemId&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;())&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;  &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;case&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; VIBRATE_SETTING_ID&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;  &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;if&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;item&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;isChecked&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;())&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; item&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;setChecked&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;false&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;);&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;  &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;else&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; item&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;setChecked&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;true&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;);&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;  &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;return&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;true&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;  &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;...这里为什么要不断设置单选框的状态？&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;  &lt;div&gt;我在实际代码中这样：&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;@Override&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public boolean onContextItemSelected(MenuItem item) {&lt;/div&gt;&lt;div&gt;// &amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;TextView tview3=(TextView)findViewById(R.id.hello);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;switch (item.getItemId()) {&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;case menu_email:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;tview3.setText(&amp;quot;your data will send by email&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;case menu_wifi:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;tview3.setText(&amp;quot;your data will send by wifi&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;case menu_apple:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;if (item.isChecked()) item.setChecked(false);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;else item.setChecked(true);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;tview3.setText(&amp;quot;your favorite is apple&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;case menu_pear:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;if (item.isChecked()) item.setChecked(false);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;else item.setChecked(true);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;tview3.setText(&amp;quot;your favorite is pear&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;case menu_ear:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;if (item.isChecked()) item.setChecked(false);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;else item.setChecked(true);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;tview3.setText(&amp;quot;your favorite is ear&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;case menu_egg:&lt;/div&gt;  &lt;div&gt;// &amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;if (item.isChecked()) item.setChecked(false);&lt;/div&gt;&lt;div&gt;// &amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;else item.setChecked(true);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;tview3.setText(&amp;quot;your favorite is egg&amp;quot;);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;  &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;default:&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;return super.onContextItemSelected(item);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp;}&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;}&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;似乎也没有什么问题。&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-3122202185644505450?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/3122202185644505450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post_8328.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3122202185644505450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3122202185644505450'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post_8328.html' title='关于组菜单的操作'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-4721949919556928940</id><published>2010-08-20T04:37:00.001-07:00</published><updated>2010-08-20T04:37:52.122-07:00</updated><title type='text'>关于菜单组，我的理解错误</title><content type='html'>菜单组的状态设置，应该是在创建菜单时设置。&lt;div&gt;代码：&lt;/div&gt;&lt;div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu.setHeaderTitle(R.string.backup_conmenu);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SubMenu menu_send=menu.addSubMenu(&amp;quot;send&amp;quot;);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.add(0,menu_email,0,&amp;quot;email&amp;quot;);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.add(0,menu_wifi,0,&amp;quot;wifi&amp;quot;);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu_send.setGroupEnabled(0, false);&lt;/div&gt;&lt;/div&gt;&lt;div&gt;这样就可以进行设置了。&lt;/div&gt;&lt;div&gt;思路应该是创建菜单时，根据某些条件动态设置一些菜单组为不可用，或选中状态之类。&lt;/div&gt;&lt;div&gt;下面一句来自SDK：&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; color: rgb(51, 51, 51); line-height: 16px; "&gt;You can show or hide the entire group with&amp;nbsp;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;a href="../../../reference/android/view/Menu.html#setGroupVisible(int, boolean)" style="color: rgb(0, 102, 153); "&gt;setGroupVisible()&lt;/a&gt;&lt;/code&gt;&lt;/code&gt;; enable or disable the group with&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;a href="../../../reference/android/view/Menu.html#setGroupEnabled(int, boolean)" style="color: rgb(0, 102, 153); "&gt;setGroupEnabled()&lt;/a&gt;&lt;/code&gt;&lt;/code&gt;; and set whether the items can be checkable with&amp;nbsp;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;code style="color: rgb(0, 112, 0); font-family: monospace; line-height: 1em; "&gt;&lt;a href="../../../reference/android/view/Menu.html#setGroupCheckable(int, boolean, boolean)" style="color: rgb(0, 102, 153); "&gt;setGroupCheckable()&lt;/a&gt;&lt;/code&gt;&lt;/code&gt;.&lt;/span&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-4721949919556928940?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/4721949919556928940/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post_7287.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4721949919556928940'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4721949919556928940'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post_7287.html' title='关于菜单组，我的理解错误'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-2798225342222503214</id><published>2010-08-20T04:29:00.001-07:00</published><updated>2010-08-20T04:29:38.541-07:00</updated><title type='text'>菜单组的几个命令如何使用</title><content type='html'>Android的菜单组，有几个命令是用来控制菜单组的。但是，菜单只有在创建时才有组的概念。在其他的Activity中也可能通过组id来对该组进行设置吗？这样的话，组编号在整个软件的生命周期中是唯一了？&lt;div&gt;当使用时，却发现提示，说setGroupEnabled函数并未定义。不知是什么原因。&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-2798225342222503214?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/2798225342222503214/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post_20.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2798225342222503214'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2798225342222503214'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post_20.html' title='菜单组的几个命令如何使用'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-3133922256529725160</id><published>2010-08-18T07:46:00.001-07:00</published><updated>2010-08-18T07:46:56.476-07:00</updated><title type='text'>动态生成菜单的一段还末理解的代码</title><content type='html'>&lt;div&gt;这段代码暂时看不太懂，先记下来。等看了intent的内容后再回头看。&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp; @Override&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public boolean onPrepareOptionsMenu(Menu menu) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;super.onPrepareOptionsMenu(menu);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;final boolean haveItems = getListAdapter().getCount() &amp;gt; 0;&lt;/div&gt;  &lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If there are any notes in the list (which implies that one of&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// them is selected), then we need to generate the actions that&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// can be performed on the current selection. &amp;nbsp;This will be a combination&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// of our own specific actions along with any extensions that can be&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// found.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (haveItems) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// This is the selected item.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());&lt;/div&gt;  &lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Build menu... &amp;nbsp;always starts with the EDIT action...&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Intent[] specifics = new Intent[1];&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;specifics[0] = new Intent(Intent.ACTION_EDIT, uri);&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MenuItem[] items = new MenuItem[1];&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// ... is followed by whatever other actions are available...&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Intent intent = new Intent(null, uri);&lt;/div&gt;&lt;div&gt;  &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;intent.addCategory(Intent.CATEGORY_ALTERNATIVE);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;items);&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Give a shortcut to the edit action.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (items[0] != null) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;items[0].setShortcut(&amp;#39;1&amp;#39;, &amp;#39;e&amp;#39;);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else {&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-3133922256529725160?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/3133922256529725160/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3133922256529725160'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3133922256529725160'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/blog-post.html' title='动态生成菜单的一段还末理解的代码'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-2935674196917758467</id><published>2010-08-18T01:52:00.000-07:00</published><updated>2010-08-18T01:53:04.002-07:00</updated><title type='text'>使用xml文件创建菜单的事件处理函数</title><content type='html'>&lt;div&gt;使用xml文件创建的菜单程序，在使用 swtch捕捉用户点击时，要使用形似R.id.opmen1之类的变量才可正确捕捉。&lt;/div&gt;&lt;div&gt;函数如下：&lt;/div&gt;&lt;div&gt;&amp;nbsp;public boolean onOptionsItemSelected(MenuItem item) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;switch (item.getItemId()) {&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;case R.id.opmenu1:&lt;/div&gt;  &lt;div&gt;// &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newGame();&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;int i=0;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;case R.id.opmenu2:&lt;/div&gt;&lt;div&gt;// &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;quit();&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;int j=1;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-2935674196917758467?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/2935674196917758467/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2935674196917758467'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2935674196917758467'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/xml.html' title='使用xml文件创建菜单的事件处理函数'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-4994706910581104837</id><published>2010-08-13T05:22:00.001-07:00</published><updated>2010-08-13T05:22:22.191-07:00</updated><title type='text'>错误：Java Model Status [gen [****]does not exist]</title><content type='html'>在使用Eclipse的过程中，突然所有项目都出现了错误，提示是：&lt;br&gt;&lt;b&gt;Java Model Status [gen [****]does not exist]&lt;/b&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;后来，将gen目录（包括其中的文件R）删除，故障排除。不知是什么原因。&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;&lt;br&gt;  &lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal;"&gt;网上说，也可以更换一下SDK的版本来解决这个问题。&lt;br&gt;&lt;/span&gt;&lt;/b&gt;&lt;div&gt;&lt;b&gt;&lt;br&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-4994706910581104837?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/4994706910581104837/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/java-model-status-gen-does-not-exist.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4994706910581104837'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4994706910581104837'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/08/java-model-status-gen-does-not-exist.html' title='错误：Java Model Status [gen [****]does not exist]'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-6524895912227644473</id><published>2010-07-23T08:58:00.001-07:00</published><updated>2010-07-23T08:58:54.172-07:00</updated><title type='text'>复杂的一些菜单代码（来自系出名门）</title><content type='html'>&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br&gt;&amp;lt;resources&amp;gt;&lt;br&gt;    &amp;lt;string name=&amp;quot;hello&amp;quot;&amp;gt;Hello Layout&amp;lt;/string&amp;gt;&lt;br&gt;    &amp;lt;string name=&amp;quot;app_name&amp;quot;&amp;gt;webabcd_layout&amp;lt;/string&amp;gt;&lt;br&gt;&amp;lt;/resources&amp;gt;&lt;p&gt;Main.java&lt;br&gt;package com.webabcd.layout;&lt;p&gt;import android.app.Activity;&lt;br&gt;import android.os.Bundle;&lt;p&gt;public class Main extends Activity {&lt;br&gt;    /** Called when the activity is first created. */&lt;br&gt;    @Override&lt;br&gt;    public void onCreate(Bundle savedInstanceState) {&lt;br&gt;        super.onCreate(savedInstanceState);&lt;br&gt;        setContentView(R.layout.main);&lt;br&gt;    }&lt;br&gt;}&lt;p&gt;2、上下文菜单，选项菜单，子菜单&lt;br&gt;res/layout/main.xml&lt;br&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br&gt;&amp;lt;LinearLayout xmlns:android=&amp;quot;&lt;a href="http://schemas.android.com/apk/res/android"&gt;http://schemas.android.com/apk/res/android&lt;/a&gt;&amp;quot;&lt;br&gt;    android:orientation=&amp;quot;vertical&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;    android:layout_height=&amp;quot;fill_parent&amp;quot;&amp;gt;&lt;p&gt;    &amp;lt;TextView android:id=&amp;quot;@+id/txt1&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;        android:layout_height=&amp;quot;wrap_content&amp;quot;&lt;br&gt;android:text=&amp;quot;@string/hello_contextMenu&amp;quot; /&amp;gt;&lt;p&gt;    &amp;lt;TextView android:id=&amp;quot;@+id/txt2&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;        android:layout_height=&amp;quot;wrap_content&amp;quot;&lt;br&gt;android:text=&amp;quot;@string/hello_subMenu&amp;quot; /&amp;gt;&lt;p&gt;&amp;lt;/LinearLayout&amp;gt;&lt;p&gt;res/values/strings.xml&lt;br&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br&gt;&amp;lt;resources&amp;gt;&lt;br&gt;    &amp;lt;string name=&amp;quot;hello_contextMenu&amp;quot;&amp;gt;Hello Context Menu&amp;lt;/string&amp;gt;&lt;br&gt;    &amp;lt;string name=&amp;quot;hello_subMenu&amp;quot;&amp;gt;Hello Context Sub Menu&amp;lt;/string&amp;gt;&lt;br&gt;    &amp;lt;string name=&amp;quot;app_name&amp;quot;&amp;gt;webabcd_menu&amp;lt;/string&amp;gt;&lt;br&gt;&amp;lt;/resources&amp;gt;&lt;p&gt;Main.java&lt;br&gt;package com.webabcd.menu;&lt;p&gt;import android.app.Activity;&lt;br&gt;import android.os.Bundle;&lt;br&gt;import android.view.ContextMenu;&lt;br&gt;import android.view.Menu;&lt;br&gt;import android.view.MenuItem;&lt;br&gt;import android.view.SubMenu;&lt;br&gt;import android.view.View;&lt;br&gt;import android.view.ContextMenu.ContextMenuInfo;&lt;br&gt;import android.widget.TextView;&lt;br&gt;import android.widget.Toast;&lt;p&gt;// 演示两种菜单的实现方式：上下文菜单（通过在某元素上长按，来呼出菜单）和选项菜单（通过按手机上的菜单按钮，来呼出菜单）&lt;br&gt;public class Main extends Activity {&lt;br&gt;    /** Called when the activity is first created. */&lt;br&gt;    @Override&lt;br&gt;    public void onCreate(Bundle savedInstanceState) {&lt;br&gt;        super.onCreate(savedInstanceState);&lt;br&gt;        setContentView(R.layout.main);&lt;p&gt;        // 为 R.id.txt1 注册一个上下文菜单（在此 TextView 上长按，则会呼出上下文菜单）&lt;br&gt;        // 具体呼出的菜单内容需要重写 onCreateContextMenu 来创建&lt;br&gt;        TextView txt1 = (TextView) this.findViewById(R.id.txt1);&lt;br&gt;        this.registerForContextMenu(txt1);&lt;p&gt;        // 为 R.id.txt2 注册一个上下文菜单&lt;br&gt;        TextView txt2 = (TextView) this.findViewById(R.id.txt2);&lt;br&gt;        this.registerForContextMenu(txt2);&lt;br&gt;    }&lt;p&gt;    // 重写 onCreateContextMenu 用以创建上下文菜单&lt;br&gt;    // 重写 onContextItemSelected 用以响应上下文菜单&lt;br&gt;    @Override&lt;br&gt;    public void onCreateContextMenu(ContextMenu menu, View v,&lt;br&gt;            ContextMenuInfo menuInfo) {&lt;br&gt;        super.onCreateContextMenu(menu, v, menuInfo);&lt;p&gt;        // 创建 R.id.txt1 的上下文菜单&lt;br&gt;        if (v == (TextView) this.findViewById(R.id.txt1)) {&lt;p&gt;            // ContextMenu.setIcon() - 设置菜单的图标&lt;br&gt;            // ContextMenu.setHeaderTitle() - 设置菜单的标题&lt;br&gt;            menu.setHeaderIcon(R.drawable.icon01);&lt;br&gt;            menu.setHeaderTitle(&amp;quot;我是菜单&amp;quot;);&lt;p&gt;            // 用 ContextMenu.add() 来增加菜单项，返回值为 MenuItem&lt;br&gt;            // 第一个参数：组ID&lt;br&gt;            // 第二个参数：菜单项ID&lt;br&gt;            // 第三个参数：顺序号&lt;br&gt;            // 第四个参数：菜单项上显示的内容&lt;br&gt;            menu.add(1, 0, 0, &amp;quot;菜单1&amp;quot;);&lt;p&gt;            // MenuItem - 新增菜单项后的返回类型，针对菜单项的其他设置在此对象上操作&lt;br&gt;            menu.add(1, 1, 1, &amp;quot;菜单2&amp;quot;).setCheckable(true);&lt;p&gt;        }&lt;br&gt;        // 创建 R.id.txt2 的上下文菜单（多级上下文菜单）&lt;br&gt;        else if (v == (TextView) this.findViewById(R.id.txt2)) {&lt;p&gt;            // ContextMenu.addSubMenu(&amp;quot;菜单名称&amp;quot;) - 用来添加子菜单。子菜单其实就是一个特殊的菜单&lt;br&gt;            SubMenu sub = menu.addSubMenu(&amp;quot;父菜单1&amp;quot;);&lt;br&gt;            sub.setIcon(R.drawable.icon01);&lt;br&gt;            sub.add(0, 0, 0, &amp;quot;菜单1&amp;quot;);&lt;br&gt;            sub.add(0, 1, 1, &amp;quot;菜单2&amp;quot;);&lt;br&gt;            sub.setGroupCheckable(1, true, true);&lt;p&gt;            SubMenu sub2 = menu.addSubMenu(&amp;quot;父菜单2&amp;quot;);&lt;br&gt;            sub2.setIcon(R.drawable.icon01);&lt;br&gt;            sub2.add(1, 0, 0, &amp;quot;菜单3&amp;quot;);&lt;br&gt;            sub2.add(1, 1, 1, &amp;quot;菜单4&amp;quot;);&lt;br&gt;            sub2.setGroupCheckable(1, true, false);&lt;p&gt;        }&lt;br&gt;    }&lt;p&gt;&lt;br&gt;    // 重写 onCreateOptionsMenu 用以创建选项菜单&lt;br&gt;    @Override&lt;br&gt;    public boolean onCreateOptionsMenu(Menu menu) {&lt;p&gt;        MenuItem menuItem = menu.add(0, 0, 0, &amp;quot;菜单111111111111111111111&amp;quot;);&lt;p&gt;        // MenuItem.setIcon() - 设置菜单项的图标&lt;br&gt;        // MenuItem.setTitleCondensed() - 菜单的简标题，如果指定了简标题的话，菜单项上的标题将会以此简标题为准&lt;br&gt;        // MenuItem.setAlphabeticShortcut() - 设置选中此菜单项的快捷键&lt;br&gt;        // 注：菜单项超过 6 个的话，第 6 个菜单将会变为  More 菜单，多余的菜单会在单击 More 菜单之后显示出来&lt;br&gt;        menuItem.setIcon(R.drawable.icon01);&lt;br&gt;        menuItem.setTitleCondensed(&amp;quot;菜单1&amp;quot;);&lt;br&gt;        menuItem.setAlphabeticShortcut(&amp;#39;a&amp;#39;);&lt;p&gt;        menu.add(0, 1, 1, &amp;quot;菜单2&amp;quot;).setIcon(R.drawable.icon02);&lt;br&gt;        menu.add(0, 2, 2, &amp;quot;菜单3&amp;quot;).setIcon(R.drawable.icon03);&lt;br&gt;        menu.add(0, 3, 3, &amp;quot;菜单4&amp;quot;);&lt;br&gt;        menu.add(0, 4, 4, &amp;quot;菜单5&amp;quot;);&lt;br&gt;        menu.add(0, 5, 5, &amp;quot;菜单6&amp;quot;);&lt;br&gt;        menu.add(0, 6, 6, &amp;quot;菜单7&amp;quot;).setIcon(R.drawable.icon04);&lt;br&gt;        menu.add(0, 7, 7, &amp;quot;菜单8&amp;quot;).setIcon(R.drawable.icon05);&lt;p&gt;        return true;&lt;br&gt;    }&lt;p&gt;    // 重写 onOptionsItemSelected 用以响应选项菜单&lt;br&gt;    @Override&lt;br&gt;    public boolean onOptionsItemSelected(MenuItem item) {&lt;br&gt;        super.onOptionsItemSelected(item);&lt;p&gt;        Toast.makeText(Main.this, &amp;quot;被单击的菜单项为：&amp;quot; +&lt;br&gt;String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();&lt;p&gt;        return false;&lt;br&gt;    }&lt;br&gt;}&lt;p&gt;OK&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-6524895912227644473?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/6524895912227644473/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/blog-post_273.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/6524895912227644473'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/6524895912227644473'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/blog-post_273.html' title='复杂的一些菜单代码（来自系出名门）'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-8003208354414169260</id><published>2010-07-23T08:43:00.001-07:00</published><updated>2010-07-23T08:43:24.106-07:00</updated><title type='text'>创建一个上下文菜单</title><content type='html'>创建一个上下文菜单的代码如下,注意要先定义EDIT_ID与DELETE_ID的值.&lt;br&gt;但完成代码后,在界面中长按,并没有弹出相应的菜单,不知是为什么.&lt;br&gt;    @Override&lt;br&gt;    public void onCreateContextMenu(ContextMenu menu, View v,&lt;br&gt;            ContextMenuInfo menuInfo) {&lt;br&gt;    	  super.onCreateContextMenu(menu, v, menuInfo);&lt;br&gt;    	  menu.add(0, EDIT_ID, 0,&amp;quot;Edit&amp;quot;);&lt;br&gt;    	  menu.add(0, DELETE_ID, 0,&amp;quot;Delete&amp;quot;);&lt;br&gt;    	}&lt;br&gt;    @Override&lt;br&gt;    	public boolean onContextItemSelected(MenuItem item) {&lt;br&gt;    	  AdapterContextMenuInfo info = (AdapterContextMenuInfo)&lt;br&gt;item.getMenuInfo();&lt;br&gt;    	  switch (item.getItemId()) {&lt;br&gt;    	  case EDIT_ID:&lt;br&gt;//    	    editNote(&lt;a href="http://info.id"&gt;info.id&lt;/a&gt;);&lt;br&gt;    	    return true;&lt;br&gt;    	  case DELETE_ID:&lt;br&gt;//    	    deleteNote(&lt;a href="http://info.id"&gt;info.id&lt;/a&gt;);&lt;br&gt;    	    return true;&lt;br&gt;    	  default:&lt;br&gt;    	    return super.onContextItemSelected(item);&lt;br&gt;    	  }&lt;br&gt;    	}&lt;br&gt;答案是，在使用content菜单时，要先进行注册。以表示该上下文菜单是对应于哪一个控件。函数是：&lt;br&gt;registerForContextMenu。&lt;br&gt;这个函数一般被加在OnCreate中，示例如下：&lt;br&gt;    @Override&lt;br&gt;    public void onCreate(Bundle savedInstanceState) {&lt;br&gt;        super.onCreate(savedInstanceState);&lt;br&gt;        setContentView(R.layout.main);&lt;br&gt;        TextView tView=(TextView)findViewById(R.id.hello);&lt;br&gt;        registerForContextMenu(tView);&lt;br&gt;    }&lt;br&gt;这是给系统创建的默认页面的默认元素（是一个TextView）加一个上下文菜单的例子。加入这些代码后，就可在程序的主页面中测试到上下文菜单了。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-8003208354414169260?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/8003208354414169260/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/blog-post_23.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/8003208354414169260'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/8003208354414169260'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/blog-post_23.html' title='创建一个上下文菜单'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-5767678210222439818</id><published>2010-07-19T20:23:00.000-07:00</published><updated>2010-07-19T20:24:15.850-07:00</updated><title type='text'>intent 用法(来自eoe)</title><content type='html'>Android Intent的几种用法全面总结&lt;p&gt;Intent, 用法&lt;br&gt;Intent应该算是Android中特有的东西。你可以在Intent中指定程序要执行的动作（比如：view,edit,dial），以及程序执行到该动作时所需要的资料。都指定好后，只要调用startActivity()，Android系统会自动寻找最符合你指定要求的应用程序，并执行该程序。&lt;p&gt;下面列出几种Intent的用法&lt;br&gt;显示网页:&lt;br&gt;Uri uri = Uri.parse(&amp;quot;&lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;&amp;quot;);&lt;br&gt;Intent it  = new Intent(Intent.ACTION_VIEW,uri);&lt;br&gt;startActivity(it);&lt;p&gt;显示地图:&lt;br&gt;Uri uri = Uri.parse(&amp;quot;geo:38.899533,-77.036476&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.Action_VIEW,uri);&lt;br&gt;startActivity(it);&lt;p&gt;路径规划:&lt;br&gt;Uri uri = Uri.parse(&amp;quot;&lt;a href="http://maps.google.com/maps?f=d&amp;amp;saddr=startLat%20startLng&amp;amp;daddr=endLat%20endLng&amp;amp;hl=en"&gt;http://maps.google.com/maps?f=d&amp;amp;saddr=startLat%20startLng&amp;amp;daddr=endLat%20endLng&amp;amp;hl=en&lt;/a&gt;&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_VIEW,URI);&lt;br&gt;startActivity(it);&lt;p&gt;拨打电话:&lt;br&gt;调用拨号程序&lt;br&gt;Uri uri = Uri.parse(&amp;quot;tel:xxxxxx&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_DIAL, uri);&lt;br&gt;startActivity(it);&lt;p&gt;Uri uri = Uri.parse(&amp;quot;tel.xxxxxx&amp;quot;);&lt;br&gt;Intent it =new Intent(Intent.ACTION_CALL,uri);&lt;br&gt;要使用这个必须在配置文件中加入&amp;lt;uses-permission id=&amp;quot;android.permission.CALL_PHONE&amp;quot; /&amp;gt;&lt;p&gt;发送SMS/MMS&lt;br&gt;调用发送短信的程序&lt;br&gt;Intent it = new Intent(Intent.ACTION_VIEW);&lt;br&gt;it.putExtra(&amp;quot;sms_body&amp;quot;, &amp;quot;The SMS text&amp;quot;);&lt;br&gt;it.setType(&amp;quot;vnd.android-dir/mms-sms&amp;quot;);&lt;br&gt;startActivity(it);&lt;p&gt;发送短信&lt;br&gt;Uri uri = Uri.parse(&amp;quot;smsto:0800000123&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_SENDTO, uri);&lt;br&gt;it.putExtra(&amp;quot;sms_body&amp;quot;, &amp;quot;The SMS text&amp;quot;);&lt;br&gt;startActivity(it);&lt;p&gt;发送彩信&lt;br&gt;Uri uri = Uri.parse(&amp;quot;content://media/external/images/media/23&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_SEND);&lt;br&gt;it.putExtra(&amp;quot;sms_body&amp;quot;, &amp;quot;some text&amp;quot;);&lt;br&gt;it.putExtra(Intent.EXTRA_STREAM, uri);&lt;br&gt;it.setType(&amp;quot;image/png&amp;quot;);&lt;br&gt;startActivity(it);&lt;p&gt;发送Email&lt;p&gt;Uri uri = Uri.parse(&amp;quot;mailto:&lt;a href="mailto:xxx@abc.com"&gt;xxx@abc.com&lt;/a&gt;&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_SENDTO, uri);&lt;br&gt;startActivity(it);&lt;p&gt;Intent it = new Intent(Intent.ACTION_SEND);&lt;br&gt;it.putExtra(Intent.EXTRA_EMAIL, &amp;quot;&lt;a href="mailto:me@abc.com"&gt;me@abc.com&lt;/a&gt;&amp;quot;);&lt;br&gt;it.putExtra(Intent.EXTRA_TEXT, &amp;quot;The email body text&amp;quot;);&lt;br&gt;it.setType(&amp;quot;text/plain&amp;quot;);&lt;br&gt;startActivity(Intent.createChooser(it, &amp;quot;Choose Email Client&amp;quot;));&lt;p&gt;Intent it=new Intent(Intent.ACTION_SEND);&lt;br&gt;String[] tos={&amp;quot;&lt;a href="mailto:me@abc.com"&gt;me@abc.com&lt;/a&gt;&amp;quot;};&lt;br&gt;String[] ccs={&amp;quot;&lt;a href="mailto:you@abc.com"&gt;you@abc.com&lt;/a&gt;&amp;quot;};&lt;br&gt;it.putExtra(Intent.EXTRA_EMAIL, tos);&lt;br&gt;it.putExtra(Intent.EXTRA_CC, ccs);&lt;br&gt;it.putExtra(Intent.EXTRA_TEXT, &amp;quot;The email body text&amp;quot;);&lt;br&gt;it.putExtra(Intent.EXTRA_SUBJECT, &amp;quot;The email subject text&amp;quot;);&lt;br&gt;it.setType(&amp;quot;message/rfc822&amp;quot;);&lt;br&gt;startActivity(Intent.createChooser(it, &amp;quot;Choose Email Client&amp;quot;));&lt;p&gt;添加附件&lt;br&gt;Intent it = new Intent(Intent.ACTION_SEND);&lt;br&gt;it.putExtra(Intent.EXTRA_SUBJECT, &amp;quot;The email subject text&amp;quot;);&lt;br&gt;it.putExtra(Intent.EXTRA_STREAM, &amp;quot;file:///sdcard/mysong.mp3&amp;quot;);&lt;br&gt;sendIntent.setType(&amp;quot;audio/mp3&amp;quot;);&lt;br&gt;startActivity(Intent.createChooser(it, &amp;quot;Choose Email Client&amp;quot;));&lt;p&gt;播放多媒体&lt;p&gt;Intent it = new Intent(Intent.ACTION_VIEW);&lt;br&gt;Uri uri = Uri.parse(&amp;quot;file:///sdcard/song.mp3&amp;quot;);&lt;br&gt;it.setDataAndType(uri, &amp;quot;audio/mp3&amp;quot;);&lt;br&gt;startActivity(it);&lt;p&gt;Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,&lt;br&gt;&amp;quot;1&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_VIEW, uri);&lt;br&gt;startActivity(it);&lt;p&gt;Uninstall 程序&lt;br&gt;Uri uri = Uri.fromParts(&amp;quot;package&amp;quot;, strPackageName, null);&lt;br&gt;Intent it = new Intent(Intent.ACTION_DELETE, uri);&lt;br&gt;startActivity(it);&lt;p&gt;eoeAndroid&lt;br&gt;Android中的Intent详细讲解&lt;br&gt;不错，我再补充几个：&lt;br&gt;uninstall apk&lt;br&gt;Uri uninstallUri = Uri.fromParts(&amp;quot;package&amp;quot;, &amp;quot;xxx&amp;quot;, null);&lt;p&gt;returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);&lt;p&gt;&lt;br&gt;install apk&lt;br&gt;Uri installUri = Uri.fromParts(&amp;quot;package&amp;quot;, &amp;quot;xxx&amp;quot;, null);&lt;p&gt;returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);&lt;p&gt;&lt;p&gt;play audio&lt;br&gt;Uri playUri = Uri.parse(&amp;quot;file:///sdcard/download/everything.mp3&amp;quot;);&lt;p&gt;returnIt = new Intent(Intent.ACTION_VIEW, playUri);&lt;p&gt;//发送附件&lt;br&gt;Intent it = new Intent(Intent.ACTION_SEND);&lt;br&gt;it.putExtra(Intent.EXTRA_SUBJECT, &amp;quot;The email subject text&amp;quot;);&lt;br&gt;it.putExtra(Intent.EXTRA_STREAM, &amp;quot;file:///sdcard/eoe.mp3&amp;quot;);&lt;br&gt;sendIntent.setType(&amp;quot;audio/mp3&amp;quot;);&lt;br&gt;startActivity(Intent.createChooser(it, &amp;quot;Choose Email Client&amp;quot;));&lt;p&gt;market相关&lt;p&gt;再来一个market相关的：&lt;br&gt;market相关&lt;p&gt;//搜索应用&lt;br&gt;Uri uri = Uri.parse(&amp;quot;market://search?q=pname:pkg_name&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_VIEW, uri);&lt;br&gt;startActivity(it);&lt;br&gt;//where pkg_name is the full package path for an application&lt;p&gt;//显示指定应用的详细页面（这个好像不支持了，找不到app_id）&lt;br&gt;Uri uri = Uri.parse(&amp;quot;market://details?id=app_id&amp;quot;);&lt;br&gt;Intent it = new Intent(Intent.ACTION_VIEW, uri);&lt;br&gt;startActivity(it);&lt;br&gt;//where app_id is the application ID, find the ID&lt;br&gt;//by clicking on your application on Market home&lt;br&gt;//page, and notice the ID from the address bar&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-5767678210222439818?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/5767678210222439818/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/intent-eoe.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5767678210222439818'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5767678210222439818'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/intent-eoe.html' title='intent 用法(来自eoe)'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-7130699271737598310</id><published>2010-07-19T19:54:00.000-07:00</published><updated>2010-07-19T19:55:18.871-07:00</updated><title type='text'>Salute</title><content type='html'>&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;span class="Apple-style-span" style="font-family: -webkit-sans-serif; font-size: 13px; -webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); -webkit-text-size-adjust: none; "&gt;&lt;div class="entry-contents-inner"&gt;&lt;p&gt;原作者：&lt;br&gt;来源&lt;a href="http://www.yeeyan.org/" target="_blank" style="color: rgb(34, 68, 187); "&gt;Datatypes In SQLite Version 3&lt;/a&gt;&lt;br&gt;译者&lt;a href="http://space.yeeyan.org/u/35282" target="_blank" style="color: rgb(34, 68, 187); "&gt;边之龙&lt;/a&gt;&lt;/p&gt;&lt;p&gt;大多数 SQL 数据库引擎（据我们所知的、SQLite 以外的所有 SQL 数据库引擎）使用静态的、严格的类型。对于静态类型，数据类型的值由它的容器（存储值的特定列）来决定。&lt;/p&gt;&lt;p&gt;SQLite 使用的是一个更加普通的动态类型系统。在 SQLite 中，数据类型的值是与值的本身有关联，而不是它的容器。SQLite 的动态类型系统对于更多其它数据库引擎常见的静态类型系统是向后兼容的，在这个意义上，工作在静态类型数据库上的 SQL 语句同样也能工作在 SQLite 中。然而，SQLite 中的动态类型允许它做一些在传统严格类型数据库中不能做的事情。&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 18px; font-weight: bold; "&gt;1.0 存储类和数据类型&lt;/span&gt;&lt;/p&gt;存储在 SQLite 数据库中的每个值（或者由数据库引擎操纵的值）都是以下的存储类之一：&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;NULL&lt;/span&gt;。该值是一个 NULL 值。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;INTEGER&lt;/span&gt;。该值是一个有符号的整数，根据值的大小，存储 1、2、3、4、5、6 或 8 字节内容。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;REAL&lt;/span&gt;。该值是一个浮点值，存储一个 8 字节的 IEEE 浮点数。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;TEXT&lt;/span&gt;。该值是一个文本字符串，使用数据库编码（UTF-8、 UTF-16BE 或 UTF-16LE）进行存储。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;BLOB&lt;/span&gt;。该值是一个数据块，按照它的输入直接存储。&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;注意，一个存储类要略普遍于一个数据类型。INTEGER 存储类，例如，包含不同长度的 6 个不同的整数类型。这在磁盘上是不同的。但是一旦 INTEGER 值从磁盘读取到内存进行处理，它们都将被转换为最普通的数据类型（8 字节有符号的整数）。因此大部分"存储类"是无异于"数据类型"的，并且有 2 个条款能被互换地使用。&lt;br&gt;&lt;/p&gt;&lt;p&gt;在一个 SQLite 版本 3 数据库中的任何列，除了 INTEGER PRIMARY KEY 列，都可以被用来存储一个任意存储类的值。&lt;br&gt;&lt;/p&gt;&lt;p&gt;SQL 语句中的所有值，无论它们是嵌入在 SQL 语句文本里的字面量，还是绑定到预编译 SQL 语句的参数，都有一个内在存储类。在以下描述的情况中，数据库引擎会在执行查询过程中，在数字存储类（INTEGER 和 REAL）和 TEXT 之间进行转换。&lt;br&gt;&lt;/p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;1.1 Boolean 数据类型&lt;/span&gt;&lt;br&gt;&lt;p&gt;SQLite 没有一个分开的 Boolean 存储类。替代的，Boolean 值被存储为整数 0（false）和 1（true）。&lt;br&gt;&lt;/p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;1.2 Date 和 Time 数据类型&lt;/span&gt;&lt;br&gt;&lt;p&gt;SQLite 没有一个预留的存储日期和/或时间的存储类。替代的，SQLite 内置的 Date 和 Time 函数有能力把日期和时间存储为 TEXT、REAL 或 INTEGER 值：&lt;br&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;TEXT&lt;/span&gt;&amp;nbsp;为 ISO8601 字符串（YYYY-MM-DD HH:MM:SS.SSS）。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;REAL&lt;/span&gt;&amp;nbsp;为"朱利安"天数，天数从"格林威治"公元前4714年11月24日中午到预期的公历。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;INTEGER&lt;/span&gt;&amp;nbsp;为 Unix Time，自 1970-01-01 00:00:00 UTC 以来的秒数。&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;应用程序可以选择任何格式来存储日期和时间，并且使用内置的日期和时间函数在格式之间自由地转换。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 18px; font-weight: bold; "&gt;2.0 亲和类型&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;为了最大化 SQLite 和其它数据库引擎之间的兼容性，SQLite 支持列上的"亲和类型"概念。一个列的亲和类型是存储在该列的数据的推荐类型。在这里最重要的是，类型是被推荐的，而不是必须的。所有列仍可以存储任意类型的数据。这只是一些列，给出选择，将会使用一个存储类覆盖原先的。一个列首选的存储类称为它的"亲和类型"。&lt;br&gt;&lt;/p&gt;&lt;p&gt;SQLite 3 数据库中的每个列被分配了以下亲和类型之一：&lt;br&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;TEXT&lt;/li&gt;&lt;li&gt;NUMERIC&lt;/li&gt;&lt;li&gt;INTEGER&lt;/li&gt;&lt;li&gt;REAL&lt;/li&gt;&lt;li&gt;NONE&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;一个带有 TEXT 亲和类型的列，可以使用存储类 NULL、TEXT、BLOB 来存储所有的数据。如果数值数据被插入到一个带有 TEXT 亲和类型的列中，它将从原来被存储的格式转换为现在的文本格式。&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个带有 NUMERIC 亲和类型的列可以包含所有的 5 种存储类。当文本数据被插入到一个 NUMERIC 列中，如果转换是无损和可逆的，文本存储类将被转换为 INTEGER 或 REAL（优先顺序）。对于 TEXT 和 REAL 存储类之间的转换，如果数的前 15 位是有意义的十进制数字，SQLite 考虑到转换要无损和可逆，都将会被保留。如果 TEXT 到 INTEGER 或 REAL 的无损转换是不可能的，那么值将使用 TEXT 存储类进行存储。不要尝试将其转换为 NULL 或 BLOB 值。&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个字符串可能看起来像一个带有小数点和/或指数符号的浮点数字面量，但只要该值能够被表示为一个整数，NUMERIC 亲和类型会将转换它为一个整数。因此，在一个 NUMERIC 亲和类型的列中，字符串 '3.0e+5' 被存储为整数 300000，而不是浮点值 300000.0。&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个使用 INTEGER 亲和类型的列与一个使用 NUMERIC 亲和类型的列在行为上是相同的。INTEGER 和 NUMERIC 亲和类型不同点只表现在一个 CAST 表达式上。&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个带有 REAL 亲和类型的列与一个带有 NUMERIC 亲和类型的列在行为上是相同的，除了它强制把整数值作为浮点数来表示。（作为一个内部的优化，没有小数部分的小浮点数值存储到带有 REAL 亲和类型的列中时，将被作为一个整数写入到磁盘，为的是占用更少的空间和在它被读出时能被自动地转换回浮点数。该优化在 SQL 级别是完全不可见的，并且只有通过研究数据库文件的原始数据才能被检测。）&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个带有 NULL 亲和类型的列不建议将一个存储类覆盖到另一个存储类，并且不要尝试强制将数据从一个存储类变为另一个存储类。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;2.1 亲和列的测定&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个列的亲和类型是由列的声明类型决定的，根据下面展示的有顺序的规则：&lt;br&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;如果类型声明包含字符串"INT"，它将被分配为 INTEGER 亲和类型。&lt;/li&gt;&lt;li&gt;如果列的类型声明包含字符串"CHAR"、"CLOB"或"TEXT"的任意之一，列将拥有 TEXT 亲和类型。&lt;/li&gt;&lt;li&gt;如果一个列的类型声明包含字符串"BLOB"，或者如果没有类型被指定，列将拥有 NONE 亲和类型。&lt;/li&gt;&lt;li&gt;如果一个类的类型声明包含字符串"REAL"、"FLOA"或"DOUB"的任意之一，列将拥有 REAL 亲和类型。&lt;/li&gt;&lt;li&gt;否则，亲和类型为 NUMERIC。&lt;br&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;注意，决定列的亲和类型的规则的顺序是十分重要的。一个列的类型声明若是"CHARINT"，则将匹配规则 1 和 2，但是第一个规则更加优先，因此列的亲和类型为 INTEGER。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;2.2 亲和名称的例子&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;下表展示了，如何通过前一章节的 5 个规则，将传统的 SQL 实现的通用数据类型名称转换为亲和的类型。该表只展示了 SQLite 允许的数据类型名称的一个小子集。注意，类型名称后面括号中的数字参数（如："VARCHAR(255)"）将被 SQLite 忽略 - SQLite 不会在全局 SQLITE_MAX_LENGTH 以外，再对字符串、BLOBs 或数字值的长度上强加任何的长度限制。&lt;/p&gt;&lt;br&gt;&lt;table border="1" style="font-size: 12px; text-align: center; "&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;来自 CREATE TABLE 语句 CAST 表达式中&lt;br&gt;类型名称的例子&lt;/th&gt;&lt;th&gt;亲和类型结果&lt;/th&gt;&lt;th&gt;被用来决定亲和类型的规则&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;INT&lt;br&gt;INTEGER&lt;br&gt;TINYINT&lt;br&gt;SMALLINT&lt;br&gt;MEDIUMINT&lt;br&gt;BIGINT&lt;br&gt;UNSIGNED BIG INT&lt;br&gt;INT2&lt;br&gt;INT8&lt;/td&gt;&lt;td&gt;INTEGER&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CHARACTER(20)&lt;br&gt;VARCHAR(255)&lt;br&gt;VARYING CHARACTER(255)&lt;br&gt;NCHAR(55)&lt;br&gt;NATIVE CHARACTER(70)&lt;br&gt;NVARCHAR(100)&lt;br&gt;TEXT&lt;br&gt;CLOB&lt;/td&gt;&lt;td&gt;TEXT&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BLOB&lt;br&gt;&lt;span style="font-style: italic; "&gt;no datatype specified&lt;/span&gt;&lt;/td&gt;&lt;td&gt;NONE&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;REAL&lt;br&gt;DOUBLE&lt;br&gt;DOUBLE PRECISION&lt;br&gt;FLOAT&lt;/td&gt;&lt;td&gt;REAL&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;NUMERIC&lt;br&gt;DECIMAL(10, 5)&lt;br&gt;BOOLEAN&lt;br&gt;DATE&lt;br&gt;DATETIME&lt;/td&gt;&lt;td&gt;NUMERIC&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;注意，一个"FLOATING POINT"类型声明会给出 INTEGER 亲和类型，而不是 REAL 亲和类型，由于"POINT"的结尾是"INT"。并且类型声明"STRING"将是一个 NUMBERIC 亲和类型，而不是 TEXT。&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;2.3 列亲和行为的例子&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;下面的 SQL 演示了 SQLite 在值被插入到一个表时，如何使用列的亲和类型来进行类型转换。&lt;/p&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;CREATE TABLE t1(&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t&amp;nbsp; TEXT,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- 规则 2 的文本亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nu NUMERIC,&amp;nbsp; -- 规则 5 的数字亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i&amp;nbsp; INTEGER,&amp;nbsp;&amp;nbsp; -- 规则 1 的整数亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; r&amp;nbsp; REAL,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- 规则 4 的浮点数亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; no BLOB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- 规则 3 的无亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;);&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 值被存储为 TEXT、INTEGER、INTEGER、REAL、TEXT。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES('500.0', '500.0', '500.0', '500.0', '500.0');&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;text|integer|integer|real|text&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 值被存储为 TEXT、INTEGER、INTEGER、REAL、REAL。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;DELETE FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(500.0, 500.0, 500.0, 500.0, 500.0);&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;text|integer|integer|real|real&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 值被存储为 TEXT、INTEGER、INTEGER、REAL、INTEGER。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;DELETE FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(500, 500, 500, 500, 500);&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;text|integer|integer|real|integer&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 无论列的亲和类型，BLOBs 总是被存储为 BLOBs。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;DELETE FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(x'0500', x'0500', x'0500', x'0500', x'0500');&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;blob|blob|blob|blob|blob&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- NULLs 也不会被亲和类型所影响。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;DELETE FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(NULL,NULL,NULL,NULL,NULL);&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;null|null|null|null|null&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&lt;p&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="margin-left: 40px; "&gt;&lt;/div&gt;&lt;p&gt;&lt;span style="font-weight: bold; font-size: 18px; "&gt;3.0 比较表达式&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;SQLite 版本 3 有通常的 SQL 比较操作符集，包括"="、"&amp;lt;"、"&amp;lt;="、"&amp;gt;="、"!="、"IN"、"BETWEEN"和"IS"等。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold; font-size: 16px; "&gt;3.1 排序顺序&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;一个比较的结果是依靠操作数的存储类来决定的，根据下面的规则：&lt;br&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;存储类 NULL 的值被认为是小于其它任何值的（包括其它存储类 NULL 的值）。&lt;/li&gt;&lt;li&gt;一个 INTEGER 或 REAL 值小于任何 TEXT 或 BLOB 值。当一个 INTEGER 或 REAL 与其它 INTEGER 或 REAL 比较时，一个数值的比较将被执行。&lt;/li&gt;&lt;li&gt;一个 TEXT 值是小于一个 BLOB 值的。当两个 TEXT 值进行比较时，一个适当的集合序列将被用来决定其结果。&lt;/li&gt;&lt;li&gt;当两个 BLOB 值进行比较时，使用 memcmp() 来决定结果。&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;3.2 比较操作数的亲和类型&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;SQLite 在执行一个比较之前，会尝试在存储类 INTEGER、REAL 和/或 TEXT 之间执行值的转换。在比较发生之前，无论尝试任何转换，都是依靠操作数的亲和类型而进行的。操作数的亲和类型是由以下规则决定的：&lt;br&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;一个表达式是一个对列值的简单引用，则拥有与列相同的亲和类型。注意，如果 X 和 Y.Z 是列名，那么 +X 和 +Y.Z 将被认为是表达式，目的是为了确定亲和类型。&lt;/li&gt;&lt;li&gt;一个"CAST(expr TO type)"格式的表达式，拥有与一个列的"type"类型声明相同的亲和类型。&lt;/li&gt;&lt;li&gt;否则，表达式为 NONE 亲和类型。&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;3.3 比较之前的类型转换&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;要"应用亲和类型"意思是转换一个操作数为一个特定的存储类，当且仅当转换是无损和可逆的。要在比较之前将亲和类型应用到一个比较操作符的操作数中，根据以下展示的规则顺序：&lt;br&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;如果一个操作数为 INTEGER、REAL 或 NUMERIC 亲和类型，并且另一个操作数为 TEXT 或 NONE 亲和类型，NUMERIC 亲和类型将被应用到另一个操作数上。&lt;/li&gt;&lt;li&gt;如果一个操作数为 TEXT 亲和类型，并且另一个操作数为 NONE 亲和类型，TEXT 亲和类型将被应用到另一个操作数上。&lt;/li&gt;&lt;li&gt;否则，无亲和类型将被应用到两个比较的操作数上。&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;表达式"a BETWEEN b AND c"被视为两个分开的二元比较"a &amp;gt;= b AND a &amp;lt;= c"，即使这意味着不同的亲和类型被应用到每个比较的"a"上。格式"x IN (SELECT y ...)"比较中的数据类型转换，被处理为"x = y"是否为真。表达式"a IN (x, y, z, ...)"相当于"a = +x, a = +y, a = +z, ..."。换句话讲，IN 操作符右边的值（该例中为"x"、"y"和"z"的值）将被视为无亲和类型，即使它们碰巧是列值或 CAST 表达式。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;3.4 比较示例&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;div style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;CREATE TABLE t1(&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a TEXT,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- 文本亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; b NUMERIC,&amp;nbsp;&amp;nbsp; -- 数字亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; c BLOB,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- 无亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; d&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -- 无亲和类型&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;);&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 值将分别被存储为 TEXT、INTEGER、TEXT 和 INTEGER。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES('500', '500', '500', 500);&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT typeof(a), typeof(b), typeof(c), typeof(d) FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;text|integer|text|integer&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 因为列"a"是文本亲和类型，右手边的数字值在发生比较之前，就被转换为文本了。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT a &amp;lt; 40,&amp;nbsp;&amp;nbsp; a &amp;lt; 60,&amp;nbsp;&amp;nbsp; a &amp;lt; 600 FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|1|1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 文本亲和类型被应用到右手边的操作数，但因为它们已经是 TEXT，这将是一个空操作；不会发生转换。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT a &amp;lt; '40', a &amp;lt; '60', a &amp;lt; '600' FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|1|1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 列"b"是数字亲和类型，因此数字亲和类型将被应用给右边的操作数。因为操作数已经是数字，应用程序的亲和类型将是空操作；不会发生转换。所有值进行数值比较。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT b &amp;lt; 40,&amp;nbsp;&amp;nbsp; b &amp;lt; 60,&amp;nbsp;&amp;nbsp; b &amp;lt; 600 FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|0|1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 数字亲和类型将被应用到右边的操作数，将它们从文本转换为整数。然后产生一个数字比较。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT b &amp;lt; '40', b &amp;lt; '60', b &amp;lt; '600' FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|0|1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 发生无亲和类型转换。右手边的值都是存储类 INTEGER，它们总是小于左边的 TEXT 值。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT c &amp;lt; 40,&amp;nbsp;&amp;nbsp; c &amp;lt; 60,&amp;nbsp;&amp;nbsp; c &amp;lt; 600 FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|0|0&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 发生无亲和类型转换。值作为 TEXT 进行比较。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT c &amp;lt; '40', c &amp;lt; '60', c &amp;lt; '600' FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|1|1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 发生无亲和类型转换。右手边的值都是存储类 INTEGER，它们与左边的 INTEGER 值进行数值比较。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT d &amp;lt; 40,&amp;nbsp;&amp;nbsp; d &amp;lt; 60,&amp;nbsp;&amp;nbsp; d &amp;lt; 600 FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;0|0|1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 发生无亲和类型转换。左边的 INTEGER 值总是小于右边的 TEXT 值。&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT d &amp;lt; '40', d &amp;lt; '60', d &amp;lt; '600' FROM t1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;1|1|1&lt;/span&gt;&lt;br&gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp;&lt;p&gt;如果比较被换算，该例中所有的结果都是相同的 - 如果表达式为"a &amp;lt; 40"格式，将被重写为"40 &amp;gt; a"。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 18px; font-weight: bold; "&gt;4.0 操作符&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;所有数学运算操作符（+、-、*、/、%、&amp;lt;&amp;lt;、&amp;gt;&amp;gt;、&amp;amp; 和 |）两边的操作数在执行之前都被转换为 NUMERIC 存储类。即使它是有损的和不可逆的，转换都会通过。数学运算操作符中的一个 NULL 操作数，产生一个 NULL 结果。数学运算操作符中的一个操作数看起来不像任何数字或不是 NULL，它将被转换为 0 或 0.0。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 18px; font-weight: bold; "&gt;5.0 排序、分组和复合的 SELECTs&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;当查询结果被 ORDER BY 子句进行排序，存储类 NULL 的值将被置顶，其后是穿插的以数字为序 INTEGER 和 REAL 值，再后是以序列进行排序的 TEXT 值，最后是 memcmp() 顺序的 BLOB 值。在排序之前不会发生任何存储类的转换。&lt;br&gt;&lt;/p&gt;&lt;p&gt;当使用 GROUP BY 子句对值进行分组时，不同存储类的值被认为是不同的，除了被认为是相等的 INTEGER 和 REAL 值，如果它们在数值上是相等的话。没有亲和类型会被应用到 GROUP BY 子句的结果中。&lt;br&gt;&lt;/p&gt;&lt;p&gt;复合 SELECT 操作符 UNION、INTERSECT 和 EXCEPT 执行内部的值比较。没有亲和类型被应用到与 UNION、INTERSECT 或 EXCEPT 关联的内部比较操作数上 - 值被直接进行比较。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 18px; font-weight: bold; "&gt;6.0 校对序列&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;当 SQLite 比较两个字符串时，如果两个字符串相等，它使用一个校对序列或校对函数来决定哪个字符串更大一些。SQLite 有 3 个内置的校对函数：BINARY、NOCASE 和 RTRIM。&lt;br&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;BINARY&lt;/span&gt;&amp;nbsp;- 使用 memcmp() 对字符串数据进行比较，无论文本编码。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;NOCASE&lt;/span&gt;&amp;nbsp;- 与 BINARY 相同，除了在比较执行之前会将 ASCII 码的 26 个大写字母被折换为与其等值的小写字母。注意，只有 ASCII 码的字符进行大小写折换。由于所需表的大小，SQLite 不会尝试对完整 UTF 的大小写进行折换。&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold; "&gt;RTRIM&lt;/span&gt;&amp;nbsp;- 与 BINARY 相同，除了尾随的空格将被忽略。&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;使用 sqlite3_create_collation() 接口，一个应用程序可以注册附加的校对函数。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;6.1 从 SQL 分配校对序列&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;每个表的每个列都有一个相关联的校对函数。如果没有校对函数被明确定义，校对规则默认为 BINARY。列定义的 COLLATE 子句被用来替代地为一个列定义校对函数。&lt;br&gt;&lt;/p&gt;&lt;p&gt;为一个二元比较操作符（=、&amp;lt;、&amp;gt;、&amp;lt;=、&amp;gt;=、!=、IS 和 IS NOT）决定使用何种校对函数，按照下面展示的规则顺序进行：&lt;br&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;如果两个操作数都使用 COLLATE 后缀操作符分配了一个明确的校对函数，那么明确的校对函数被用在比较中，左边操作数的校对函数有较高的优先级。&lt;/li&gt;&lt;li&gt;如果两个操作数都是一个列，左边列的校对函数有较高的优先级。前一句的目的是，带有一个或多个一元"+"操作符的列名仍被认为是一个列名。&lt;/li&gt;&lt;li&gt;否则，比较将会使用 BINARY 校对函数。&lt;br&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;表达式"x BETWEEN y AND z"在逻辑上等价于两个比较"x &amp;gt;= y AND x &amp;lt;= z"，并且与校对函数一起工作，就像这是有两个分开的比较一样。表达式"x IN (SELECT y ...)"与"x = y"的处理方式一样，目的是为了确定校对序列。用在"x IN (y, z, ...)"格式表达式上的校对序列，就是 x 的校对序列。&lt;br&gt;&lt;/p&gt;&lt;p&gt;ORDER BY 子句是一个 SELECT 语句的一部分，其规则是，可使用 COLLATE 操作符为 SELECT 语句分配一个校对序列，在这种情况下，特定的校对函数被用于排序。否则，如果通过 ORDER BY 子句进行排序的表达式是一个列，列的校对序列将被用来确定排序顺序。如果表达式不是一个列并且没有 COLLATE 子句，则 BINARY 校对序列将被使用。&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 16px; font-weight: bold; "&gt;6.2 校对序列示例&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;下面的例子用来辨认校对序列，这可被用来确定由各 SQL 语句执行的文本比较的结果。注意，在 NUMERIC、BLOB 或 NULL 值的情况，一个文本的比较并不是必须的，并且不会使用校对序列。&lt;/p&gt;&lt;p style="margin-left: 40px; "&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;CREATE TABLE t1(&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x INTEGER PRIMARY KEY,&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 校对序列 BINARY */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; b COLLATE BINARY,&amp;nbsp; /* 校对序列 BINARY */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; c COLLATE RTRIM,&amp;nbsp;&amp;nbsp; /* 校对序列 RTRIM */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; d COLLATE NOCASE&amp;nbsp;&amp;nbsp; /* 校对序列 NOCASE */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;);&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* x&amp;nbsp;&amp;nbsp; a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(1,'abc','abc', 'abc&amp;nbsp; ','abc');&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(2,'abc','abc', 'abc',&amp;nbsp; 'ABC');&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(3,'abc','abc', 'abc ', 'Abc');&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;INSERT INTO t1 VALUES(4,'abc','abc ','ABC',&amp;nbsp; 'abc');&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 BINARY 校对序列执行文本比较 a = b。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 WHERE a = b ORDER BY x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 2 3&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 RTRIM 校对序列执行文本比较 a = b。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 WHERE a = b COLLATE RTRIM ORDER BY x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 2 3 4&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 NOCASE 校对序列执行文本比较 d = a。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 WHERE d = a ORDER BY x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 2 3 4&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 BINARY 校对序列执行文本比较 a = d。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 WHERE a = d ORDER BY x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 4&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 RTRIM 校对序列执行文本比较 'abc' = c。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 WHERE 'abc' = c ORDER BY x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 2 3&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 RTRIM 校对序列执行文本比较 c = 'abc'。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 WHERE c = 'abc' ORDER BY x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 2 3&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 NOCASE 校对序列进行分组（值"abc"、"ABC"和"Abc"被放在相同的分组中）。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT count(*) FROM t1 GROUP BY d ORDER BY 1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 4&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 BINARY 校对序列执行分组。"abc"、"ABC"和"Abc"来自不同的分组。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT count(*) FROM t1 GROUP BY (d || '') ORDER BY 1;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 1 1 2&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 RTRIM 校对序列或列 c 执行排序。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 ORDER BY c, x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 4 1 2 3&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 BINARY 校对序列或 (c || '') 执行排序。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 ORDER BY (c||''), x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 4 2 3 1&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;/* 使用 NOCASE 校对序列或列 c 执行排序。 */&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;SELECT x FROM t1 ORDER BY c COLLATE NOCASE, x;&lt;/span&gt;&lt;br&gt;&lt;span style="background-color: rgb(229, 228, 228); "&gt;-- 结果 2 4 3 1&lt;/span&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://article.yeeyan.org/view/35282/119158#newComment" target="_blank" style="color: rgb(34, 68, 187); "&gt;添加新评论&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;相关文章：&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;a href="http://article.yeeyan.org/view/35282/117000?from=rss_related" target="_blank" style="color: rgb(34, 68, 187); "&gt;SQLite 对比其它数据库引擎的 NULL 处理&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;a href="http://article.yeeyan.org/view/35282/116959?from=rss_related" target="_blank" style="color: rgb(34, 68, 187); "&gt;SQLite 没有实现的 SQL 特征&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www1.feedsky.com/t1/393297998/yeeyan/feedsky/s.gif?r=http://article.yeeyan.org/view/35282/119158" border="0" height="0" width="0"&gt;&lt;p&gt;&lt;a href="http://www1.feedsky.com/r/l/feedsky/yeeyan/393297998/art01.html" target="_blank" style="color: rgb(34, 68, 187); "&gt;&lt;img border="0" ismap="ismap" src="http://www1.feedsky.com/r/i/feedsky/yeeyan/393297998/art01.gif"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;发自我的 iPhone&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-7130699271737598310?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/7130699271737598310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/salute.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/7130699271737598310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/7130699271737598310'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/salute.html' title='Salute'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-757836864792183051</id><published>2010-07-19T01:50:00.001-07:00</published><updated>2010-07-19T01:50:47.736-07:00</updated><title type='text'>阅读SDK示例程序Note的一点笔记</title><content type='html'>也可以在主程序中使用Intent&lt;br&gt;intent=getIntent();来设置当前要显示的Activity，这个由AndroidManifest.xml中的下面语句来定义：&lt;br&gt;        &amp;lt;activity android:name=&amp;quot;NotesList&amp;quot;&lt;br&gt;android:label=&amp;quot;@string/title_notes_list&amp;quot;&amp;gt;&lt;br&gt;            &amp;lt;intent-filter&amp;gt;&lt;br&gt;                &amp;lt;action android:name=&amp;quot;android.intent.action.MAIN&amp;quot; /&amp;gt;&lt;br&gt;                &amp;lt;category android:name=&amp;quot;android.intent.category.LAUNCHER&amp;quot; /&amp;gt;&lt;br&gt;            &amp;lt;/intent-filter&amp;gt;&lt;br&gt;  &amp;lt;/activity&amp;gt;&lt;br&gt;上面的语句定义默认显示NotesList这个布局。&lt;br&gt;在调试器中查看intent的值如下所示：&lt;br&gt;intent	Intent  (id=830066438464)	&lt;br&gt;	mAction	&amp;quot;android.intent.action.MAIN&amp;quot; (id=830066438512)	&lt;br&gt;		count	26	&lt;br&gt;		hashCode	-1173447682	&lt;br&gt;		offset	0	&lt;br&gt;		value	 (id=830066438544)	&lt;br&gt;	mCategories	HashSet  (id=830066438880)	&lt;br&gt;	mComponent	ComponentName  (id=830066438728)	&lt;br&gt;	mData	null	&lt;br&gt;	mExtras	null	&lt;br&gt;	mFlags	268435456	&lt;br&gt;	mPackage	null	&lt;br&gt;	mSourceBounds	null	&lt;br&gt;	mType	null	&lt;br&gt;在mAction变量中，保存了要打开的Activity的值（在前所述的文件中定义了）。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-757836864792183051?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/757836864792183051/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/sdknote.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/757836864792183051'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/757836864792183051'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/sdknote.html' title='阅读SDK示例程序Note的一点笔记'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-8173961397775594362</id><published>2010-07-19T01:49:00.001-07:00</published><updated>2010-07-19T01:49:56.030-07:00</updated><title type='text'>我的第一个使用按钮打开另一个Activity的程序</title><content type='html'>android 按钮打开另一个activity的示例程序&lt;br&gt;首先，是布局排列问题。一般来说，有经验的用户会选择使用java代码生成界面，但是我对界面代码不太熟悉，所以，我使用了xml文件来生成界面。&lt;br&gt;打开res-&amp;gt;layout，选择main.xml，这是系统默认生成的界面布置xml文件。双击该项，会打开该文件，在页面底部选择layout，出现页面布置页面。在页面上右击鼠标，选择Add菜单项。选择&amp;quot;Button&amp;quot;项，这样就在主界面中加入了一个按钮。&lt;br&gt;其次要创建一个该按钮打开的activity，也可以理解为打开一个对话框。在Android中，一个Activity由一个java文件和一个xml文件组成。&lt;br&gt;要创建一个java文件比较简单，单击工具栏中的新建按钮，选择class。在弹出的对话框中，单击&amp;quot;Source&lt;br&gt;Folder：&amp;quot;后面的&amp;quot;Browse&amp;quot;，选择要保存文件的工程名称，一般来说应是当前工程。&lt;br&gt;在Name编辑框中，输入要创建的java文件名，也就是类名称。如wnAbout.&lt;br&gt;单击Superclass编辑框后面的&amp;quot;Browse&amp;quot;按钮，选择Activity类。&lt;br&gt;单击&amp;quot;Finish&amp;quot;按钮结束创建。&lt;br&gt;这样就创建了一个类。&lt;br&gt;然后开始创建xml布局文件。&lt;br&gt;单击工具栏新建按钮，选择Android XML file项。&lt;br&gt;在弹出的对话框中，单击&amp;quot;Project&amp;quot;后面的&amp;quot;Browse&amp;quot;按钮，选择要创建布局文件的项目。&lt;br&gt;在File文本框中，输入文件名称。如about.xml。要注意，必须要输入xml这个扩展名。&lt;br&gt;在&amp;quot;What type of Resource Would you like create?&amp;quot;单选框中，选择Layout项，意思是创建一个布局的xml文件。&lt;br&gt;在&amp;quot;Select the root element for the XML file&amp;quot;下拉框中，选择LinearLayout项，意思是选择线性布局模式。&lt;br&gt;单击finish按钮完成创建。&lt;p&gt;为了让程序可以响应按钮的单击事件，需要建立一个监听器。&lt;br&gt;现在，单击res-&amp;gt;layout，然后双击main.xml项，打开主界面设置页面。单击页面底部的main.xml，直接编辑该xml文件。将其更改成如下内容：&lt;br&gt;&amp;lt;Button android:text=&amp;quot;@string/strAbout&amp;quot;&lt;br&gt;	android:id=&amp;quot;@+id/btnAbout&amp;quot;&lt;br&gt;	android:layout_width=&amp;quot;wrap_content&amp;quot;&lt;br&gt;	android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;gt;&lt;br&gt;&amp;lt;/Button&amp;gt;&lt;br&gt;意思是让按钮显示字符串strAbout的内容，并且将按钮的id设置为btnAbout。&lt;br&gt;单击res-&amp;gt;values，双击打开strings.xml文件，在其中加入：&lt;br&gt;    &amp;lt;string name=&amp;quot;strAbout&amp;quot;&amp;gt;about&amp;lt;/string&amp;gt;&lt;br&gt;意思是将strAbout字符串的内容设置为about。&lt;br&gt;这样，就做好了准备工作。可以单击工具栏的运行按钮或者按&amp;quot;Ctrl+F11&amp;quot;来启动模拟器，并在模拟器中运行该程序。应该可以看到一个问候语字符串和一个显示了About字符的按钮。&lt;br&gt;现在开始输入代码。&lt;br&gt;单击src-&amp;gt;项目空间名称，双击项目名称为名的java文件。&lt;br&gt;修改该文件，使其像如下样子：&lt;br&gt;public class wnReader extends Activity {&lt;br&gt;	&lt;br&gt;	private Button btnAbout;//定义一个按钮对象&lt;br&gt;    /** Called when the activity is first created. */&lt;br&gt;    @Override&lt;br&gt;    public void onCreate(Bundle savedInstanceState) {&lt;br&gt;        super.onCreate(savedInstanceState);&lt;br&gt;        setContentView(R.layout.main);&lt;p&gt;        btnAbout=(Button)findViewById(R.id.btnAbout);&lt;br&gt;        btnAbout.setOnClickListener(new View.OnClickListener()//设置监听器&lt;br&gt;	{&lt;br&gt;            public void onClick(View v) {&lt;br&gt;                About();//按钮单击时触发About函数。&lt;br&gt;            }&lt;br&gt;        });           protected void About() {&lt;br&gt;        Intent i = new Intent(this, wnAbout.class);&lt;br&gt;        startActivity(i);&lt;br&gt;    }&lt;p&gt;    }&lt;p&gt;        protected void About()//这个是按钮单击时触发的事件，注意应该定义为protected类型。&lt;br&gt;	{&lt;br&gt;        Intent i = new Intent(this, wnAbout.class);&lt;br&gt;        startActivity(i);&lt;br&gt;    }&lt;br&gt;现在建立了按钮单击事件的监听器，可以响应该按钮的单击事件。为了让程序可以打开新建的activity,首先需要修改一下AAndroidManifest.xml文件。双击该文件，打开。&lt;br&gt;在其中加入如下一段内容：&lt;br&gt;&amp;lt;activity android:name=&amp;quot;.wnAbout&amp;quot;&lt;br&gt;        		android:label=&amp;quot;@string/strAbout&amp;quot;&amp;gt;&lt;br&gt;        &amp;lt;/activity&amp;gt;&lt;br&gt;修改后的内容应该如下所示：&lt;br&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br&gt;&amp;lt;manifest xmlns:android=&amp;quot;&lt;a href="http://schemas.android.com/apk/res/android"&gt;http://schemas.android.com/apk/res/android&lt;/a&gt;&amp;quot;&lt;br&gt;      package=&amp;quot;com.wnReader&amp;quot;&lt;br&gt;      android:versionCode=&amp;quot;1&amp;quot;&lt;br&gt;      android:versionName=&amp;quot;1.0&amp;quot;&amp;gt;&lt;br&gt;    &amp;lt;application android:icon=&amp;quot;@drawable/icon&amp;quot; android:label=&amp;quot;@string/app_name&amp;quot;&amp;gt;&lt;br&gt;        &amp;lt;activity android:name=&amp;quot;.wnReader&amp;quot;&lt;br&gt;                  android:label=&amp;quot;@string/app_name&amp;quot;&amp;gt;&lt;br&gt;            &amp;lt;intent-filter&amp;gt;&lt;br&gt;                &amp;lt;action android:name=&amp;quot;android.intent.action.MAIN&amp;quot; /&amp;gt;&lt;br&gt;                &amp;lt;category android:name=&amp;quot;android.intent.category.LAUNCHER&amp;quot; /&amp;gt;&lt;br&gt;            &amp;lt;/intent-filter&amp;gt;&lt;br&gt;        &amp;lt;/activity&amp;gt;&lt;p&gt;        &amp;lt;activity android:name=&amp;quot;.wnAbout&amp;quot;&lt;br&gt;        		android:label=&amp;quot;@string/strAbout&amp;quot;&amp;gt;&lt;br&gt;        &amp;lt;/activity&amp;gt;&lt;p&gt;    &amp;lt;/application&amp;gt;&lt;br&gt;    &amp;lt;uses-sdk android:minSdkVersion=&amp;quot;7&amp;quot; /&amp;gt;&lt;p&gt;&amp;lt;/manifest&amp;gt;&lt;br&gt;	但是由于系统自动建立的java文件和xml布局文件其实是空白的，所以应该在其上加入一些控件。&lt;br&gt;单击src-&amp;gt;com.wnReader，双击其中的wnAbout.java，打开该文件。修改其内容，修改后内容应该如下所示：&lt;br&gt;public class wnAbout extends Activity {&lt;br&gt;    @Override&lt;br&gt;    protected void onCreate(Bundle savedInstanceState) {&lt;br&gt;        super.onCreate(savedInstanceState);&lt;br&gt;        setContentView(R.layout.about);//意思是显示名为about.xml的布局文件&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;单击res-&amp;gt;layout，双击about.xml文件。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-8173961397775594362?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/8173961397775594362/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/activity.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/8173961397775594362'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/8173961397775594362'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/activity.html' title='我的第一个使用按钮打开另一个Activity的程序'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-2187672276199275070</id><published>2010-07-19T01:48:00.001-07:00</published><updated>2010-07-19T01:48:54.054-07:00</updated><title type='text'>我的第一个菜单程序</title><content type='html'>创建菜单&lt;br&gt;一般来说，菜单有选项菜单、上下文菜单，以及子菜单。&lt;br&gt;选项菜单是指程序运行时，按Menu键弹出的菜单，一般来说，一屏最多显示六个菜单项而且参照惯例，应该会给每个菜单项添加图标。而上下文菜单，是指长按某项后出现的菜单。子菜单，顾名思义，就是一个菜单的下级菜单。&lt;br&gt;创建菜单可以通过代码menu.add来实现。创建菜单应该重写函数onCreateOptionsMenu.使用代码创建菜单的示例如下：&lt;br&gt;/* Creates the menu items */&lt;br&gt;public boolean onCreateOptionsMenu(Menu menu) {&lt;br&gt;    menu.add(0, MenuOpenBook, 0, &amp;quot;Open book&amp;quot;);&lt;br&gt;    menu.add(0, MenuOption, 0, &amp;quot;Option&amp;quot;);&lt;br&gt;    return true;&lt;br&gt;}&lt;p&gt;/* 如果要让这些菜单做些工作的话，可以使用如下函数。要注意的是，这些功能都是在重写函数onOptionsItemSelected中实现 */&lt;br&gt;public boolean onOptionsItemSelected(MenuItem item) {&lt;br&gt;    switch (item.getItemId()) {//使用getItemId来获得选中的菜单项。&lt;br&gt;    case MenuOpenBook:&lt;br&gt;        OpenBook();&lt;br&gt;        return true;&lt;br&gt;    case MenuOption:&lt;br&gt;        Option();&lt;br&gt;        return true;&lt;br&gt;    }&lt;br&gt;    return false;&lt;br&gt;}&lt;br&gt;如果要让这个菜单在不同的时候有不同的内容，可以重写onPrepareOptionsMenu()函数来实现。该函数在每次菜单被加载时都会运行。&lt;br&gt;不过，为了方便起见，还是建议使用xml文件来创建菜单。下面来介绍一下使用xml文件创建菜单的方法。&lt;br&gt;首先，右键单击res项，选择new菜单项，在弹出的级联菜单中选择folder，在Folder&lt;br&gt;name中输入menu，创建存放菜单文件的目录。这个位置和名称都是不能更改的。必须存放在res目录中，子目录名称必须为menu。&lt;br&gt;然后，单击工具栏的新建按钮，选择Android xml file。&lt;br&gt;在File文本框中输入要创建的菜单文件的名称，如menuopenbook.xml。注意，这里的名字一般应该采用小写字母与数字的组合，不应该会采用大写字母。&lt;br&gt;在&amp;quot;What type of resource would you like to create?&amp;quot;单选框中选择Menu项。意思是创建一个菜单XML文件。&lt;br&gt;Folder文本框会自动定位到res/menu目录。&lt;br&gt;单击Finish完成创建。&lt;br&gt;现在虽然已经创建了一个菜单文件，但其中各项内容还是空白，需要向其中添加内容。&lt;br&gt;依次单击res-&amp;gt;menu-&amp;gt;menuopenbook.xml，打开文件准备编辑。&lt;br&gt;单击页面下方的layout切换到设计视图。&lt;br&gt;单击Add按钮，在弹出的对话框中选择Create a new element at the top level ,in Menu.意思是创建一个菜单项。&lt;br&gt;在中间的文本框中输入菜单项的名称。如menuopen。&lt;br&gt;在列表栏中选择Item项。&lt;br&gt;单击Ok。&lt;br&gt;将Id更改为@+id/menuopen，将Title更改为open book。&lt;br&gt;现在就新建了一个菜单项，id为menuopen，而显示的文字是open book。&lt;p&gt;打开项目的主文件，wnReader.java，在其中添加代码，将菜单应用到项目中去。&lt;br&gt;只需要添加如下代码即可：&lt;br&gt;    public boolean onCreateOptionsMenu(Menu menu) {&lt;br&gt;        MenuInflater inflater = getMenuInflater();&lt;br&gt;        inflater.inflate(R.menu.menuopenbook, menu);&lt;br&gt;        return true;&lt;br&gt;    }&lt;br&gt;然后在import区域中添加如下两行：&lt;br&gt;import android.view.Menu;&lt;br&gt;import android.view.MenuInflater;&lt;br&gt;按Ctrl+F11运行程序，在主界面出现时，按模拟器的Menu键，刚才设计的菜单就会从屏幕下方弹出。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-2187672276199275070?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/2187672276199275070/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/blog-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2187672276199275070'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2187672276199275070'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/blog-post.html' title='我的第一个菜单程序'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-509979132383493479</id><published>2010-07-13T06:10:00.001-07:00</published><updated>2010-07-13T06:10:58.869-07:00</updated><title type='text'>android学习小结2</title><content type='html'>文章分类:移动开发&lt;br&gt;1 在1。5的android中，可以指定edittext中只输入数字，比如可以这样，就弹出小键盘了：&lt;br&gt;android:inputType=&amp;quot;numberDecimal&amp;quot;，这样还可以接受输入包含小数点的数字了。&lt;br&gt;2 读取资源文件中的内容&lt;br&gt;   result.setText(getText(R.String.bmi_result));&lt;br&gt;注意使用getText函数，读取资源文件中的内容.&lt;p&gt;3 做一个象&amp;quot;关于我们&amp;quot;的对话框：&lt;br&gt;    new AlertDialog.Builder(Bmi.this)&lt;br&gt;            .setTitle(R.string.about_title)&lt;br&gt;            .setMessage(R.string.about_msg)&lt;br&gt;            .setPositiveButton(&amp;quot;确认&amp;quot;,&lt;br&gt;       new DialogInterface.OnClickListener(){&lt;br&gt;           public void onClick(&lt;br&gt;               DialogInterface dialoginterface, int i){&lt;br&gt;               }&lt;br&gt;            })&lt;br&gt;            .show();&lt;br&gt;4 toast组件：显示短的提示消息，过几秒之后就消失：&lt;br&gt;    Toast.makeText(Bmi.this, &amp;quot;关于我们&amp;quot;, Toast.LENGTH_SHORT).show();&lt;br&gt;5 url的打开&lt;br&gt;   new AlertDialog.Builder(Bmi.this)&lt;br&gt;    .setNegativeButton(R.string.homepage_label,&lt;br&gt;            new DialogInterface.OnClickListener(){&lt;br&gt;            public void onClick(&lt;br&gt;                DialogInterface dialoginterface, int i){&lt;br&gt;                //go to url&lt;br&gt;                Uri uri = Uri.parse(&amp;quot;&lt;a href="http://sites.google.com/site/gasodroid/"&gt;http://sites.google.com/site/gasodroid/&lt;/a&gt;&amp;quot;);&lt;br&gt;                Intent intent = new Intent(Intent.ACTION_VIEW, uri);&lt;br&gt;                startActivity(intent);&lt;br&gt;            }&lt;br&gt;     })&lt;br&gt;     .show();&lt;br&gt;   也可以把URI写到资源文件中去，&lt;br&gt;   Uri uri=uri.parase(getString(R.string.homepage_uri));&lt;br&gt;6 menu菜单&lt;br&gt;   public boolean onCreateOptionsMenu(Menu menu) {&lt;br&gt;  // TODO Auto-generated method stub&lt;br&gt;     menu.add(0, MENU_ABOUT, 0, &amp;quot;关于&amp;quot;).setIcon(R.drawable.help_browser);&lt;br&gt;     menu.add(0, MENU_Quit, 0, &amp;quot;结束&amp;quot;).setIcon(R.drawable.emblem_unreadable);&lt;br&gt;  return super.onCreateOptionsMenu(menu);&lt;br&gt; }&lt;br&gt;    处理menu的点击动作&lt;br&gt;public boolean onOptionsItemSelected(MenuItem item) {&lt;br&gt;  // TODO Auto-generated method stub&lt;br&gt;  switch(item.getItemId()) {&lt;br&gt;        case MENU_ABOUT:&lt;br&gt;             openOptionsDialog();&lt;br&gt;             break;&lt;br&gt;        case MENU_Quit:&lt;br&gt;             finish();&lt;br&gt;             break;&lt;br&gt;  }&lt;br&gt;  return super.onOptionsItemSelected(item);&lt;br&gt; }&lt;br&gt;7   &amp;lt;activity android:name=&amp;quot;.Bmi&amp;quot;&lt;br&gt;                  android:label=&amp;quot;@string/app_name&amp;quot;&amp;gt;&lt;br&gt;            &amp;lt;intent-filter&amp;gt;&lt;br&gt;                &amp;lt;action android:name=&amp;quot;android.intent.action.MAIN&amp;quot; /&amp;gt;&lt;br&gt;                &amp;lt;category android:name=&amp;quot;android.intent.category.LAUNCHER&amp;quot; /&amp;gt;&lt;br&gt;            &amp;lt;/intent-filter&amp;gt;、&lt;br&gt;  这里指出了启动的类是Bmi这个类，，&amp;lt;intent-filter&amp;gt;讲明了这个程序的性质，其中&lt;br&gt;  &amp;lt;action android:name=&amp;quot;android.intent.action.MAIN&amp;quot; /&amp;gt;讲明了这个BMI是程序的切入点，&lt;br&gt;&amp;lt;category android:name=&amp;quot;android.intent.category.LAUNCHER&amp;quot;&lt;br&gt;/&amp;gt;讲明这个出现在程序的lanucher列表中&lt;br&gt;8 intent之间传递数据&lt;br&gt;    Intent intent = new Intent();&lt;br&gt;                intent.setClass(Bmi.this, Report.class);&lt;br&gt;                Bundle bundle = new Bundle();&lt;br&gt;                bundle.putString(&amp;quot;KEY_HEIGHT&amp;quot;,&lt;br&gt;field_height.getText().toString());&lt;br&gt;                bundle.putString(&amp;quot;KEY_WEIGHT&amp;quot;,&lt;br&gt;field_weight.getText().toString());&lt;br&gt;                intent.putExtras(bundle);&lt;br&gt;                startActivity(intent);&lt;br&gt;接收信息：&lt;br&gt;    Bundle bunde = this.getIntent().getExtras();&lt;br&gt;        double height = Double.parseDouble(bunde.getString(&amp;quot;KEY_HEIGHT&amp;quot;))/100;&lt;br&gt;9 使用状态拦信息&lt;br&gt;    import android.app.Notification;&lt;br&gt;import android.app.NotificationManager;&lt;br&gt;import android.app.PendingIntent;&lt;p&gt;NotificationManager barManager = (NotificationManager)&lt;br&gt;getSystemService(NOTIFICATION_SERVICE);&lt;p&gt;     Notification barMsg = new Notification(&lt;br&gt;       R.drawable.icon_128,&lt;br&gt;          &amp;quot;HI�&amp;quot;,&lt;br&gt;          System.currentTimeMillis()&lt;br&gt;          );&lt;br&gt;     barMsg.defaults |= Notification.DEFAULT_SOUND;&lt;br&gt;     //barMsg.defaults |= Notification.DEFAULT_ALL;&lt;p&gt;     PendingIntent contentIntent = PendingIntent.getActivity(&lt;br&gt;          this,&lt;br&gt;          0,&lt;br&gt;                new Intent(this, Bmi.class),&lt;br&gt;                PendingIntent.FLAG_UPDATE_CURRENT);&lt;p&gt;     barMsg.setLatestEventInfo(&lt;br&gt;       Report.this,&lt;br&gt;       &amp;quot;HI&amp;quot;,&lt;br&gt;                &amp;quot;OK&amp;quot;,&lt;br&gt;                contentIntent);&lt;p&gt;     barManager.notify(0, barMsg);&lt;br&gt;NotificationManager barManager = (NotificationManager)&lt;br&gt;getSystemService(NOTIFICATION_SERVICE);&lt;br&gt;声明管理器，&lt;br&gt;Notification barMsg = new Notification(&lt;br&gt;       R.drawable.icon_128,&lt;br&gt;          &amp;quot;HI�&amp;quot;,&lt;br&gt;          System.currentTimeMillis()&lt;br&gt;          );&lt;br&gt;中声明提醒信息，System.currentTimeMillis()表示立刻显示；&lt;br&gt;barMsg.setLatestEventInfo(&lt;br&gt;       Report.this,&lt;br&gt;       &amp;quot;HI&amp;quot;,&lt;br&gt;                &amp;quot;OK&amp;quot;,&lt;br&gt;                contentIntent);&lt;br&gt;这里是添加状态栏的详细信息，这里的&amp;quot;HI&amp;quot;是显示的主题，第三个参数，这里的&amp;#39;OK&amp;#39;是说明，&lt;br&gt;contentIntent这个参数指明当点状态栏时，相应打开的intent.&lt;br&gt;10 保存优先级的设置：&lt;br&gt;   private void restorePrefs() {&lt;br&gt;        SharedPreferences settings = getSharedPreferences(PREF, 0);&lt;br&gt;        String pref_height = settings.getString(PREF_HEIGHT, &amp;quot;&amp;quot;);&lt;p&gt;    }&lt;br&gt;  这里是找系统中是否以&amp;quot;BMI_PREF&amp;quot;字符串作为文件名的优先级设置的文件，有的话，以settings做为代号来操作&lt;br&gt;当用户离开activity时，把值进行保存，重写onPause()函数：&lt;br&gt; super.onPause();&lt;br&gt;  // Save user preferences. use Editor object to make changes.&lt;br&gt;        SharedPreferences settings = getSharedPreferences(PREF, 0);&lt;br&gt;            settings.edit()&lt;br&gt;                .putString(PREF_HEIGHT, field_height.getText().toString())&lt;br&gt;                .commit();&lt;br&gt;11 多语言界面并存：&lt;br&gt;   比如原来的是英文，则res目录下放values,中文的话，在res目录下再放一个values-zh-rTW&lt;br&gt;其中zh是主语系，-r是必须的，后面跟分支&lt;br&gt;    Resources res = getResources();&lt;br&gt;        Configuration conf = res.getConfiguration();&lt;br&gt;        conf.locale = Locale.TRADITIONAL_CHINESE;&lt;br&gt;        DisplayMetrics dm = res.getDisplayMetrics();&lt;br&gt;        res.updateConfiguration(conf, dm);&lt;br&gt;这里是强制使用中文界面，在程序中，针对不同语言提供不同的界面：&lt;br&gt;  if (conf.locale==Local.TRADITIONAL_CHINESE)&lt;br&gt;12 Spinner下拉菜单中的选择事件：&lt;br&gt;    field_feet.setOnItemSelectedListener(getFeet);&lt;br&gt;  private Spinner.OnItemSelectedListener getFeet = new&lt;br&gt;Spinner.OnItemSelectedListener() {&lt;br&gt;        public void onItemSelected(AdapterView parent, View v, int&lt;br&gt;position, long id) {&lt;br&gt;               。。。。。。&lt;br&gt;        }&lt;br&gt;        public void onNothingSelected(AdapterView parent) {&lt;br&gt;        }&lt;br&gt;    };&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-509979132383493479?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/509979132383493479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/509979132383493479'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/509979132383493479'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android2.html' title='android学习小结2'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-1086135376980485532</id><published>2010-07-13T06:09:00.001-07:00</published><updated>2010-07-13T06:09:59.065-07:00</updated><title type='text'>android学习小结3</title><content type='html'>文章分类:移动开发&lt;br&gt;1 activity可以继承扩展 ListActivity&lt;br&gt; 比如：&lt;br&gt;class DummyNote extends ListActivity {&lt;br&gt; private String[] note_array = {&lt;p&gt;            &amp;quot;gasolin&amp;quot;,&lt;br&gt;            &amp;quot;crota&amp;quot;,&lt;br&gt;            &amp;quot;louk&amp;quot;,&lt;br&gt;            &amp;quot;magicion&amp;quot;&lt;p&gt;    };&lt;br&gt;  ListAdapter adapter = new ArrayAdapter&amp;lt;String&amp;gt;(this,&lt;br&gt;                    android.R.layout.simple_list_item_1,&lt;br&gt;                    note_array);&lt;br&gt;        setListAdapter(adapter);&lt;p&gt;}&lt;br&gt;2 使用sqlite&lt;br&gt;  在新建立工程后，打开模拟器后，在sdk的tools下&lt;br&gt;运行adb shell&lt;br&gt;cd data/data/&lt;br&gt;ls&lt;br&gt; cd 工程名&lt;br&gt;mkdir databases&lt;br&gt;cd databases&lt;br&gt;sqlite3 notes.db （建立了一个notes.db数据库）&lt;br&gt;create talbe notes&lt;br&gt;......;&lt;br&gt;sqlite&amp;gt;.databases  (查看当前目录下的数据库列表）&lt;br&gt;sqllite&amp;gt;.tables  (查看所有数据表）&lt;br&gt;     .schema notes (查看指定表的结构）&lt;br&gt;离开sqllite:  .exit&lt;p&gt;3  CRUD的典型例子&lt;br&gt; public class NotesDbAdapter {&lt;p&gt; private static final String DATABASE_NAME = &amp;quot;notes.db&amp;quot;;&lt;br&gt;    private static final int DATABASE_VERSION = 1;&lt;br&gt;    private static final String DATABASE_TABLE = &amp;quot;notes&amp;quot;;&lt;br&gt;    private static final String DATABASE_CREATE =&lt;br&gt;    &amp;quot;create table notes(&amp;quot;&lt;br&gt;        +&amp;quot;_id INTEGER PRIMARY KEY,&amp;quot;&lt;br&gt;        +&amp;quot;note TEXT,&amp;quot;&lt;br&gt;        +&amp;quot;created INTEGER,&amp;quot;&lt;br&gt;        +&amp;quot;modified INTEGER&amp;quot;&lt;br&gt;    +&amp;quot;);&amp;quot;;&lt;br&gt; private static class DatabaseHelper extends SQLiteOpenHelper {&lt;br&gt;  public DatabaseHelper(Context context) {&lt;br&gt;   super(context, DATABASE_NAME, null, DATABASE_VERSION);&lt;br&gt;   // TODO Auto-generated constructor stub&lt;br&gt;  }&lt;br&gt;  @Override&lt;br&gt;  public void onCreate(SQLiteDatabase db) {&lt;br&gt;   // TODO Auto-generated method stub&lt;br&gt;   db.execSQL(DATABASE_CREATE);&lt;br&gt;  }&lt;br&gt;  @Override&lt;br&gt;  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {&lt;br&gt;   // TODO Auto-generated method stub&lt;br&gt;   db.execSQL(&amp;quot;DROP TABLE IF EXISTS &amp;quot;+DATABASE_TABLE);&lt;br&gt;   onCreate(db);&lt;br&gt;  }&lt;br&gt;    }&lt;p&gt;    private Context mCtx = null;&lt;br&gt;    private DatabaseHelper dbHelper ;&lt;br&gt;    private SQLiteDatabase db;&lt;br&gt;    /** Constructor */&lt;br&gt;    public NotesDbAdapter(Context ctx) {&lt;br&gt;        this.mCtx = ctx;&lt;br&gt;    }&lt;br&gt;    public NotesDbAdapter open () throws SQLException {&lt;br&gt;        dbHelper = new DatabaseHelper(mCtx);&lt;br&gt;        db = dbHelper.getWritableDatabase();&lt;br&gt;        return this;&lt;br&gt;    }&lt;br&gt;    public void close() {&lt;br&gt;        dbHelper.close();&lt;br&gt;    }&lt;br&gt;    //start query&lt;br&gt;    public static final String KEY_ROWID = &amp;quot;_id&amp;quot;;&lt;br&gt;    public static final String KEY_NOTE = &amp;quot;note&amp;quot;;&lt;br&gt;    public static final String KEY_CREATED = &amp;quot;created&amp;quot;;&lt;br&gt;    String[] strCols = new String[] {&lt;br&gt;        KEY_ROWID,&lt;br&gt;        KEY_NOTE,&lt;br&gt;        KEY_CREATED&lt;br&gt;    };&lt;br&gt;    /*&lt;br&gt;    public Cursor getall() {&lt;br&gt;        return db.rawQuery(&amp;quot;SELECT * FROM notes&amp;quot;, null);&lt;br&gt;    }&lt;br&gt;    */&lt;p&gt;    /*&lt;br&gt;    // get all entries&lt;br&gt;    public Cursor getall() {&lt;br&gt;        return db.query(DATABASE_TABLE, //Which table to Select&lt;br&gt;             strCols,// Which columns to return&lt;br&gt;             null, // WHERE clause&lt;br&gt;             null, // WHERE arguments&lt;br&gt;             null, // GROUP BY clause&lt;br&gt;             null, // HAVING clause&lt;br&gt;             null //Order-by clause&lt;br&gt;             );&lt;br&gt;    }&lt;br&gt;    */&lt;p&gt;    // get all entries&lt;br&gt;    public Cursor getall() {&lt;br&gt;        return db.query(DATABASE_TABLE,&lt;br&gt;            new String[] {KEY_ROWID, KEY_NOTE, KEY_CREATED},&lt;br&gt;            null, null, null, null, null);&lt;br&gt;    }&lt;br&gt;    // add an entry&lt;br&gt;    public long create(String Note) {&lt;br&gt;        Date now = new Date();&lt;br&gt;        ContentValues args = new ContentValues();&lt;br&gt;        args.put(KEY_NOTE, Note);&lt;br&gt;        args.put(KEY_CREATED, now.getTime());&lt;br&gt;        return db.insert(DATABASE_TABLE, null, args);&lt;br&gt;    }&lt;br&gt;    //remove an entry&lt;br&gt;    public boolean delete(long rowId) {&lt;br&gt;        return db.delete(DATABASE_TABLE, KEY_ROWID + &amp;quot;=&amp;quot; + rowId, null) &amp;gt; 0;&lt;br&gt;    }&lt;p&gt;    //query single entry&lt;br&gt;    public Cursor get(long rowId) throws SQLException {&lt;br&gt;        Cursor mCursor = db.query(true,&lt;br&gt;                DATABASE_TABLE,&lt;br&gt;                new String[] {KEY_ROWID, KEY_NOTE, KEY_CREATED},&lt;br&gt;                KEY_ROWID + &amp;quot;=&amp;quot; + rowId,&lt;br&gt;                null, null, null, null, null);&lt;br&gt;        if (mCursor != null) {&lt;br&gt;            mCursor.moveToFirst();&lt;br&gt;        }&lt;br&gt;        return mCursor;&lt;br&gt;    }&lt;br&gt;    //update&lt;br&gt;    public boolean update(long rowId, String note) {&lt;br&gt;        ContentValues args = new ContentValues();&lt;br&gt;        args.put(KEY_NOTE, note);&lt;br&gt;        return db.update(DATABASE_TABLE, args, KEY_ROWID + &amp;quot;=&amp;quot; +&lt;br&gt;rowId, null) &amp;gt; 0;&lt;br&gt;    }&lt;p&gt;}&lt;br&gt;主程序调用：&lt;br&gt;    private NotesDbAdapter mDbHelper;&lt;br&gt;    private Cursor mNotesCursor;&lt;p&gt;    private void setAdapter() {&lt;br&gt;     mDbHelper = new NotesDbAdapter(this);&lt;br&gt;        mDbHelper.open();&lt;br&gt;        fillData();&lt;br&gt;}&lt;br&gt;   private void fillData() {&lt;br&gt;        mNotesCursor = mDbHelper.getall();&lt;br&gt;        startManagingCursor(mNotesCursor);&lt;br&gt;        String[] from = new String[]{&amp;quot;note&amp;quot;};&lt;br&gt;        int[] to = new int[]{android.R.id.text1};&lt;br&gt;        // Now create a simple cursor adapter&lt;br&gt;        SimpleCursorAdapter adapter =&lt;br&gt;                    new SimpleCursorAdapter(this,&lt;br&gt;android.R.layout.simple_list_item_1, mNotesCursor,&lt;br&gt;from, to);&lt;br&gt;        setListAdapter(adapter);&lt;br&gt;    }&lt;p&gt;新增记录：&lt;br&gt;   public long create(String Note) {&lt;br&gt;        Date now = new Date();&lt;br&gt;        ContentValues args = new ContentValues();&lt;br&gt;        args.put(KEY_NOTE, Note);&lt;br&gt;       。。。。。。&lt;br&gt;        return db.insert(DATABASE_TABLE, null, args);&lt;br&gt;    }&lt;br&gt;删除记录：&lt;br&gt;   public boolean delete(long rowId) {&lt;br&gt;        return db.delete(DATABASE_TABLE, KEY_ROWID + &amp;quot;=&amp;quot; + rowId, null) &amp;gt; 0;&lt;br&gt;    }&lt;br&gt;  mDbHelper.delete(getListView().getSelectedItemId());//这里用getListView().getSelectedItemId()获得选定&lt;br&gt;删除哪一条记录&lt;br&gt;查询记录：&lt;br&gt;//query single entry&lt;br&gt;    public Cursor get(long rowId) throws SQLException {&lt;br&gt;        Cursor mCursor = db.query(true,&lt;br&gt;                DATABASE_TABLE,&lt;br&gt;                new String[] {KEY_ROWID, KEY_NOTE, KEY_CREATED},&lt;br&gt;                KEY_ROWID + &amp;quot;=&amp;quot; + rowId,&lt;br&gt;                null, null, null, null, null);&lt;br&gt;        if (mCursor != null) {&lt;br&gt;            mCursor.moveToFirst();&lt;br&gt;        }&lt;br&gt;        return mCursor;&lt;br&gt;    }&lt;br&gt;更新记录：&lt;br&gt; //update&lt;br&gt;    public boolean update(long rowId, String note) {&lt;br&gt;        ContentValues args = new ContentValues();&lt;br&gt;        args.put(KEY_NOTE, note);&lt;br&gt;        return db.update(DATABASE_TABLE, args, KEY_ROWID + &amp;quot;=&amp;quot; +&lt;br&gt;rowId, null) &amp;gt; 0;&lt;br&gt;    }&lt;br&gt;4 activity中的相关，比如选了A中的记录，然后打开B来编辑，B编辑完后，再返回A。&lt;br&gt;   首先在B中，接收BUNDLE：&lt;br&gt; private void showViews(Bundle savedInstanceState) {&lt;br&gt;        //mRowId = savedInstanceState != null ?&lt;br&gt;savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID) :&lt;br&gt;null;&lt;br&gt;        if (mRowId == null) {&lt;br&gt;            Bundle extras = getIntent().getExtras();&lt;br&gt;            mRowId = extras != null ?&lt;br&gt;extras.getLong(NotesDbAdapter.KEY_ROWID) : null;&lt;br&gt;        }&lt;br&gt; //把编辑的数据拿出来&lt;br&gt;  if (mRowId != null) {&lt;br&gt;            Cursor note = mDbHelper.get(mRowId);&lt;br&gt;            startManagingCursor(note);&lt;br&gt;            field_note.setText(note.getString(&lt;br&gt;                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_NOTE)&lt;br&gt;                ));&lt;br&gt;        }&lt;br&gt;  当编辑成功提交后，&lt;br&gt;     public void onClick(View view) {&lt;br&gt;                mDbHelper.update(mRowId, field_note.getText().toString());&lt;br&gt;                setResult(RESULT_OK);&lt;br&gt;                finish();&lt;br&gt;   这里的setResult，表示这个activity成功，返回&lt;br&gt;在调用方中，如果是一个LISTVIEW的列表的话，点某一个列&lt;br&gt;  protected void onListItemClick(ListView l, View v, int position, long id) {&lt;br&gt;     super.onListItemClick(l, v, position, id);&lt;br&gt;     Intent intent = new Intent(this, NoteEdit.class);&lt;br&gt;     intent.putExtra(NotesDbAdapter.KEY_ROWID, id);&lt;br&gt;     startActivityForResult(intent, ACTIVITY_EDIT);&lt;br&gt; }&lt;br&gt;   这里的startActivityForResult表示的是要调用另外一个activity，并且要求结果返回&lt;br&gt;同时：&lt;br&gt;  protected void onActivityResult(int requestCode, int resultCode,&lt;br&gt;                                 Intent intent) {&lt;br&gt;     super.onActivityResult(requestCode, resultCode, intent);&lt;br&gt;     fillData();&lt;br&gt; }&lt;br&gt;5 长按菜单&lt;br&gt;    A 在oncreate函数中，注册&lt;br&gt;     registerForContextMenu(getListView());//说明点listview时会使用长按菜单&lt;br&gt;   B public void onCreateContextMenu(ContextMenu menu, View v,&lt;br&gt;   ContextMenuInfo menuInfo) {&lt;br&gt;  // TODO Auto-generated method stub&lt;br&gt;  menu.add(0, MENU_DELETE, 0,  &amp;quot;删除记事&amp;quot;);&lt;br&gt;        menu.setHeaderTitle(&amp;quot;要怎么处理这个项目&amp;quot;);&lt;br&gt;  super.onCreateContextMenu(menu, v, menuInfo);&lt;br&gt; }&lt;p&gt;&lt;p&gt;thanks party ,this post by email.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-1086135376980485532?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/1086135376980485532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1086135376980485532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1086135376980485532'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android3.html' title='android学习小结3'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-3683156414785177188</id><published>2010-07-13T06:08:00.001-07:00</published><updated>2010-07-13T06:08:51.014-07:00</updated><title type='text'>android学习小结4</title><content type='html'>文章分类:移动开发&lt;p&gt;1 用一个VIEW做为消息提示&lt;br&gt;    btn3.setOnClickListener(new Button.OnClickListener() {&lt;br&gt;   public void onClick(View v) {&lt;br&gt;    View view = inflateView(R.layout.view);&lt;br&gt;    TextView txtMsg = (TextView) view.findViewById(R.id.txtMsg);&lt;br&gt;    txtMsg.setText(&amp;quot;提示内容&amp;quot;);&lt;p&gt;    Toast toast = new Toast(Main.this);&lt;br&gt;    toast.setView(view);&lt;br&gt;    toast.setDuration(Toast.LENGTH_LONG);&lt;br&gt;    toast.show();&lt;br&gt;   }&lt;br&gt;  });&lt;br&gt;2 状态栏通知&lt;br&gt;   public void onClick(View v) {&lt;br&gt;    // 实例化通知管理器&lt;br&gt;    NotificationManager nm = (NotificationManager) getSystemService&lt;p&gt;(NOTIFICATION_SERVICE);&lt;p&gt;    // 指定单击通知后所打开的详细的通知页面（单击通知后打开 NotificationView&lt;p&gt;）&lt;br&gt;    PendingIntent contentIntent = PendingIntent.getActivity(&lt;br&gt;      Main.this, 0, new Intent(Main.this,&lt;p&gt;NotificationView.class), 0);&lt;p&gt;    // 实例化一个通知，并指定其图标和标题（在提示栏上显示）&lt;br&gt;    Notification n = new Notification(R.drawable.icon01, &amp;quot;我是滚动的通知信息&lt;p&gt;我是滚动的通知信息我是滚动的通知信息&amp;quot;, System.currentTimeMillis());&lt;p&gt;    // 设置通知的发送人和通知的详细内容（打开提示栏后在通知列表中显示）&lt;br&gt;    n.setLatestEventInfo(Main.this, &amp;quot;通知发送人&amp;quot;, &amp;quot;我是详细的通知信息我是详&lt;p&gt;细的通知信息我是详细的通知信息&amp;quot;, contentIntent);&lt;p&gt;    // 100 毫秒延迟后，震动 250 毫秒，暂停 100 毫秒后，再震动 500 毫秒&lt;br&gt;    n.vibrate = new long[] { 100, 250, 100, 500 };&lt;p&gt;    // 发出通知（其中第一个参数为通知标识符）&lt;br&gt;    nm.notify(0, n);&lt;br&gt;   }&lt;br&gt;  });&lt;p&gt;3 图片按钮：&lt;br&gt;    ImageButton imgButton = (ImageButton) this.findViewById(R.id.imageButton);&lt;br&gt;  // 设置图片按钮的背景&lt;br&gt;  imgButton.setBackgroundResource(R.drawable.icon01);&lt;br&gt;4 图片显示：&lt;br&gt;      ImageView imgView = (ImageView) this.findViewById(R.id.imageView);&lt;br&gt;  // 指定需要显示的图片&lt;br&gt;  imgView.setBackgroundResource(R.drawable.icon01);&lt;br&gt;5 checkbox:&lt;br&gt;     CheckBox chk = (CheckBox) this.findViewById(R.id.chk1);&lt;br&gt;  // setOnCheckedChangeListener() - 响应复选框的选中状态改变事件&lt;br&gt;  chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {&lt;p&gt;   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {&lt;br&gt;    TextView txt = (TextView) _CheckBox.this.findViewById(R.id.textView);&lt;br&gt;    txt.setText(&amp;quot;CheckBox01 的选中状态：&amp;quot; + String.valueOf(isChecked));&lt;p&gt;&lt;br&gt;   }&lt;br&gt;  });&lt;p&gt;6 ToggleButton:&lt;br&gt;    &amp;lt;!--&lt;br&gt;        ToggleButton - 双状态按钮控件&lt;br&gt;            textOn - 当按钮状态为 true 时所显示的文本&lt;br&gt;            textOff - 当按钮状态为 false 时所显示的文本&lt;br&gt;    --&amp;gt;&lt;br&gt;    &amp;lt;ToggleButton android:id=&amp;quot;@+id/toggleButton&amp;quot;&lt;br&gt;        android:layout_width=&amp;quot;wrap_content&amp;quot; android:layout_height=&amp;quot;wrap_content&amp;quot;&lt;br&gt;        android:textOn=&amp;quot;关闭&amp;quot; android:textOff=&amp;quot;打开&amp;quot; /&amp;gt;&lt;p&gt;    final ToggleButton btn = (ToggleButton)&lt;br&gt;this.findViewById(R.id.toggleButton);&lt;br&gt;        // setOnClickListener() - 响应按钮的鼠标单击事件&lt;br&gt;        btn.setOnClickListener(new Button.OnClickListener(){&lt;br&gt;            @Override&lt;br&gt;            public void onClick(View v) {&lt;br&gt;                TextView txt = (TextView)&lt;br&gt;_ToggleButton.this.findViewById(R.id.textView);&lt;br&gt;                // ToggleButton.isChecked() - 双状态按钮的按钮状态&lt;br&gt;                txt.setText(&amp;quot;按钮状态：&amp;quot; + String.valueOf(btn.isChecked()));&lt;br&gt;            }&lt;br&gt;        });&lt;br&gt;7 进度条：&lt;br&gt;   &amp;lt;!--&lt;br&gt;        进度条控件（条状）的演示&lt;br&gt;            style - 进度条的样式，本例使用内置样式&lt;br&gt;            max - 进度的最大值&lt;br&gt;            progress - 第一进度位置&lt;br&gt;            secondaryProgress - 第二进度位置&lt;br&gt;    --&amp;gt;&lt;br&gt;    &amp;lt;ProgressBar android:id=&amp;quot;@+id/progress_horizontal&amp;quot;&lt;br&gt;        style=&amp;quot;?android:attr/progressBarStyleHorizontal&amp;quot;&lt;br&gt;android:layout_width=&amp;quot;200px&amp;quot;&lt;br&gt;        android:layout_height=&amp;quot;wrap_content&amp;quot; android:max=&amp;quot;100&amp;quot;&lt;br&gt;        android:progress=&amp;quot;50&amp;quot; android:secondaryProgress=&amp;quot;75&amp;quot; /&amp;gt;&lt;p&gt;     // 设置特性以允许在应用程序的标题栏上显示进度条（条状）&lt;br&gt;        requestWindowFeature(Window.FEATURE_PROGRESS);&lt;br&gt;        // 设置特性以允许在应用程序的标题栏上显示进度条（圆圈状）&lt;br&gt;        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);&lt;p&gt;        this.setContentView(R.layout.progressbar);&lt;p&gt;        setTitle(&amp;quot;ProgressBar&amp;quot;);&lt;p&gt;        // 在标题栏上显示进度条（条状）&lt;br&gt;        setProgressBarVisibility(true);&lt;br&gt;        // 在标题栏上显示进度条（圆圈状）&lt;br&gt;        setProgressBarIndeterminateVisibility(true);&lt;p&gt;        // 指定进度条的进度&lt;br&gt;        setProgress(50 * 100);&lt;br&gt;        setSecondaryProgress(75 * 100);&lt;p&gt;9 SeekBar：&lt;br&gt;   &amp;lt;!--&lt;br&gt;  SeekBar - 可拖动的进度条控件&lt;br&gt;   max - 进度的最大值&lt;br&gt;   progress - 第一进度位置&lt;br&gt;   secondaryProgress - 第二进度位置&lt;br&gt; --&amp;gt;&lt;br&gt; &amp;lt;SeekBar android:id=&amp;quot;@+id/seekBar&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;  android:layout_height=&amp;quot;wrap_content&amp;quot; android:max=&amp;quot;100&amp;quot;&lt;br&gt;  android:progress=&amp;quot;50&amp;quot; android:secondaryProgress=&amp;quot;75&amp;quot; /&amp;gt;&lt;p&gt;        mSeekBar = (SeekBar) findViewById(R.id.seekBar);&lt;br&gt;  // setOnSeekBarChangeListener() - 响应拖动进度条事件&lt;br&gt;  mSeekBar.setOnSeekBarChangeListener(this);&lt;br&gt;       // 拖动进度条后，进度发生改变时的回调事件&lt;br&gt; public void onProgressChanged(SeekBar seekBar, int progress,&lt;br&gt;   boolean fromTouch) {&lt;br&gt;  mProgressText.setText(progress + &amp;quot;%&amp;quot;);&lt;br&gt; }&lt;p&gt; // 拖动进度条前开始跟踪触摸&lt;br&gt; public void onStartTrackingTouch(SeekBar seekBar) {&lt;br&gt;  mTrackingText.setText(&amp;quot;开始跟踪触摸&amp;quot;);&lt;br&gt; }&lt;p&gt; // 拖动进度条后停止跟踪触摸&lt;br&gt; public void onStopTrackingTouch(SeekBar seekBar) {&lt;br&gt;  mTrackingText.setText(&amp;quot;停止跟踪触摸&amp;quot;);&lt;br&gt; }&lt;p&gt;10 放大缩小控件：&lt;br&gt;   ZoomControls zoomControls = (ZoomControls)&lt;br&gt;this.findViewById(R.id.zoomControls);&lt;br&gt;  // setOnZoomInClickListener() - 响应单击放大按钮的事件&lt;br&gt;  zoomControls.setOnZoomInClickListener(new OnClickListener() {&lt;br&gt;   public void onClick(View v) {&lt;br&gt;    Toast.makeText(_ZoomControls.this, &amp;quot;单击了放大按钮&amp;quot;,&lt;p&gt;Toast.LENGTH_SHORT).show();&lt;br&gt;   }&lt;br&gt;  });&lt;p&gt;  // setOnZoomOutClickListener() - 响应单击缩小按钮的事件&lt;br&gt;  zoomControls.setOnZoomOutClickListener(new OnClickListener() {&lt;br&gt;   public void onClick(View v) {&lt;br&gt;    Toast.makeText(_ZoomControls.this, &amp;quot;单击了缩小按钮&amp;quot;,&lt;p&gt;Toast.LENGTH_SHORT).show();&lt;br&gt;   }&lt;br&gt;  });&lt;p&gt;11 videoview:&lt;br&gt;    VideoView videoView = (VideoView) findViewById(R.id.videoView);&lt;p&gt;        // 指定需要播放的视频的地址&lt;br&gt;        videoView.setVideoURI(Uri.parse(&amp;quot;android.resource://com.webabcd.view/&amp;quot;&lt;br&gt;+ R.raw.demo));&lt;br&gt;        // videoView.setVideoPath();&lt;p&gt;        // 设置播放器的控制条&lt;br&gt;        videoView.setMediaController(new MediaController(this));&lt;br&gt;        // 开始播放视频&lt;br&gt;        videoView.start();&lt;p&gt;12 tab控件：&lt;br&gt;   &amp;lt;!-- Tab 1 的内容　--&amp;gt;&lt;br&gt; &amp;lt;TextView android:id=&amp;quot;@+id/view1&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;  android:layout_height=&amp;quot;fill_parent&amp;quot; android:text=&amp;quot;tab1 content&amp;quot; /&amp;gt;&lt;p&gt; &amp;lt;!-- Tab 2 的内容 --&amp;gt;&lt;br&gt; &amp;lt;TextView android:id=&amp;quot;@+id/view2&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;  android:layout_height=&amp;quot;fill_parent&amp;quot; android:text=&amp;quot;tab2 content&amp;quot; /&amp;gt;&lt;p&gt;// 实现 Tab 功能的话要继承 TabActivity&lt;br&gt;public class _Tab extends TabActivity {&lt;p&gt; @Override&lt;br&gt; protected void onCreate(Bundle savedInstanceState) {&lt;br&gt;  // TODO Auto-generated method stub&lt;br&gt;  super.onCreate(savedInstanceState);&lt;p&gt;  TabHost tabHost = getTabHost();&lt;br&gt;  LayoutInflater.from(this).inflate(R.layout.tab,&lt;br&gt;tabHost.getTabContentView(), true);&lt;p&gt;  // Tab 1 的内容&lt;br&gt;  tabHost.addTab(tabHost.newTabSpec(&amp;quot;tab1&amp;quot;)&lt;br&gt;    .setIndicator(&amp;quot;tab1&amp;quot;)&lt;br&gt;    .setContent(R.id.view1));&lt;p&gt;  // Tab 2 的内容（设置了 Tab 图片）&lt;br&gt;  tabHost.addTab(tabHost.newTabSpec(&amp;quot;tab2&amp;quot;)&lt;br&gt;    .setIndicator(&amp;quot;tab2&amp;quot;, getResources().getDrawable(R.drawable.icon01))&lt;br&gt;    .setContent(R.id.view2));&lt;p&gt;  // Tab 3 的内容（设置 Tab 的内容为指定的 Activity）&lt;br&gt;  tabHost.addTab(tabHost.newTabSpec(&amp;quot;tab3&amp;quot;)&lt;br&gt;    .setIndicator(&amp;quot;tab3&amp;quot;)&lt;br&gt;    .setContent(new Intent(this, _TextView.class)));&lt;p&gt; }&lt;br&gt;}&lt;p&gt;13 GALLERY缩略图组件：&lt;br&gt;    &amp;lt;!--&lt;br&gt;        Gallery - 缩略图浏览器控件&lt;br&gt;            spacing - 缩略图列表中各个缩略图之间的间距&lt;br&gt;    --&amp;gt;&lt;br&gt;    &amp;lt;Gallery android:id=&amp;quot;@+id/gallery&amp;quot; android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;br&gt;        android:layout_height=&amp;quot;wrap_content&amp;quot; android:spacing=&amp;quot;20px&amp;quot; /&amp;gt;&lt;p&gt;  Gallery gallery = (Gallery) findViewById(R.id.gallery);&lt;br&gt;        // 为缩略图浏览器指定一个适配器&lt;br&gt;        gallery.setAdapter(new ImageAdapter(this));&lt;br&gt;        // 响应 在缩略图列表上选中某个缩略图后的 事件&lt;br&gt;        gallery.setOnItemSelectedListener(new&lt;br&gt;AdapterView.OnItemSelectedListener() {&lt;br&gt;            @Override&lt;br&gt;            public void onItemSelected(AdapterView&amp;lt;?&amp;gt; parent, View v,&lt;br&gt;                    int position, long id) {&lt;br&gt;                Toast.makeText(_Gallery.this,&lt;br&gt;String.valueOf(position), Toast.LENGTH_SHORT).show();&lt;br&gt;            }&lt;p&gt;            @Override&lt;br&gt;            public void onNothingSelected(AdapterView&amp;lt;?&amp;gt; arg0) {&lt;p&gt;            }&lt;br&gt;        });&lt;br&gt;    }&lt;p&gt;    // 继承 BaseAdapter 用以实现自定义的图片适配器&lt;br&gt;    public class ImageAdapter extends BaseAdapter {&lt;p&gt;        private Context mContext;&lt;p&gt;        public ImageAdapter(Context context) {&lt;br&gt;            mContext = context;&lt;br&gt;        }&lt;p&gt;        public int getCount() {&lt;br&gt;            return mThumbIds.length;&lt;br&gt;        }&lt;p&gt;        public Object getItem(int position) {&lt;br&gt;            return position;&lt;br&gt;        }&lt;p&gt;        public long getItemId(int position) {&lt;br&gt;            return position;&lt;br&gt;        }&lt;p&gt;        public View getView(int position, View convertView, ViewGroup parent) {&lt;br&gt;            ImageView image = new ImageView(mContext);&lt;p&gt;            image.setImageResource(mThumbIds[position]);&lt;br&gt;            image.setAdjustViewBounds(true);&lt;br&gt;            image.setLayoutParams(new Gallery.LayoutParams(&lt;br&gt;                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));&lt;p&gt;            return image;&lt;br&gt;        }&lt;br&gt;    }&lt;p&gt;    // 需要显示的图片集合&lt;br&gt;    private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,&lt;br&gt;            R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-3683156414785177188?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/3683156414785177188/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android4.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3683156414785177188'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3683156414785177188'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android4.html' title='android学习小结4'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-3920864968538061765</id><published>2010-07-13T05:58:00.001-07:00</published><updated>2010-07-13T05:58:23.057-07:00</updated><title type='text'>android beginning :button and OnClick</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; "&gt;&lt;h5 class="posthead" style="font-size: 1.5em; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-top-color: rgb(223, 223, 223); border-right-color: rgb(223, 223, 223); border-bottom-color: rgb(223, 223, 223); border-left-color: rgb(223, 223, 223); color: rgb(82, 158, 0); font-weight: normal; padding-bottom: 6px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;  &lt;a id="ctl04_TitleUrl" href="http://www.cnblogs.com/windwithlife/archive/2009/07/22/1528666.html" style="color: rgb(0, 111, 247); text-decoration: none; font-size: 0.9em; "&gt;Android 入门到精通 （界面编程#1-从Button说起)&lt;/a&gt;&lt;/h5&gt;&lt;div class="postText"&gt;  &lt;div&gt;Android 界面编程有两种基本的方法，一种是在代码中，动态创建一个个组件，及把这些组件用Layout来进行组合成复杂的界面展现。一种是用图形化的方式来编写 布局Layout，这些布局被保存在XML文件中，会编译成资源，被程序中的Activity来加载（setContentView()), 再通过findViewById方式来获得每一个界面组件的引用进行操作。对于大多数人来说，喜欢最直观的方式，既代码中动态生成的方式。我们就先从这里说起，至于可视化编程及布局资源的方式以后专门来讲述。&lt;br&gt;  一，布局管理（Layout)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 每一个界面组件都是View的子类，都可以单独占用一个屏幕，但是真正的有用的界面都是这些组件的组合，在Android中都是用各种Layout来进行布局管理，这与传统的J2SE中的一些AWT,SWING界面方式基本相同，这里就不多说。&lt;br&gt;二，一个单独的界面元素：&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 在前面说到Hello World例子中，讲过这样一段代码。在Activity中.&lt;br&gt;&amp;nbsp;&amp;nbsp; public class HelloActivity extends Activity {&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp; /** Called when the activity is first created. */&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void onCreate(Bundle savedInstanceState) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; super.onCreate(savedInstanceState);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TextView tv = new TextView(this);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tv.setText(&amp;quot;Hello, World!&amp;quot;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.setContentView(tv);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;br&gt;这里并没有用到Layout,这就是单独的组件方式。也可以改为：&lt;br&gt;super.onCreate(savedInstanceState);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Button btn = new Button(this);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn.setText(&amp;quot;TestButton&amp;quot;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.setContentView(btn);&lt;br&gt;编译运行，会有一个全屏的Button，当然这不是你想要的实用的界面.那我们就用Layout来布局&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;super.onCreate(savedInstanceState);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Button btn = new Button(this);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn.setText(&amp;quot;TestButton&amp;quot;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Button btn2 = new Button(this);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn2.setText(&amp;quot;TestButton2&amp;quot;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LinearLayout layout = new LinearLayout(this);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layout.setOrientation(LinearLayout.VERTICAL);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layout.addView(btn);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layout.addView(btn2);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.setContentView(layout);&lt;br&gt;编译运行，你就可以看到了两个上下排列的按钮，当然对于布局管理器的使用，做过PC 上AWT，SWING的人都不陌生，这里就不赘述。&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 那如何响应事件呢： 大家猜一猜？想必大家不难猜到，在AWT中，在手机的J2ME中，都是用Listener 来处理事件响应，Android也未能脱俗。这与Blackberry,Symbian中的Observer是同一个道理。都是使用了设计模式的观察者模式。下面来看一个能响应事件的例子。&lt;br&gt;  &lt;p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "&gt;import android.app.Activity;&lt;br&gt;import android.os.Bundle;&lt;br&gt;import android.view.View;&lt;br&gt;import android.view.View.OnClickListener;&lt;br&gt;  import android.widget.Button;&lt;br&gt;import android.widget.LinearLayout;&lt;/p&gt;&lt;p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; text-indent: 0px; "&gt;&lt;br&gt;public class HelloActivity extends Activity implements OnClickListener {&lt;br&gt;  &amp;nbsp; Button btn = null;&lt;br&gt;&amp;nbsp; Button btn2 = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void onClick(View v) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;if (v ==&amp;nbsp; btn)&lt;br&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.setTitle(&amp;quot;You Clicked Button1&amp;quot;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;if (v ==&amp;nbsp; btn2)&lt;br&gt;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.setTitle(&amp;quot;You Clicked Button2&amp;quot;);&lt;br&gt;  &amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;}&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void onCreate(Bundle savedInstanceState) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; super.onCreate(savedInstanceState);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn = new Button(this);&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn2 = new Button(this);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn.setText(&amp;quot;TestButton1&amp;quot;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn2.setText(&amp;quot;TestButton2&amp;quot;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn.setOnClickListener(this);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; btn2.setOnClickListener(this);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LinearLayout layout = new LinearLayout(this);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layout.setOrientation(LinearLayout.VERTICAL);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layout.addView(btn);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layout.addView(btn2);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.setContentView(layout);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;/p&gt;步骤是：&lt;br&gt;一，生成两个Button，配置Click事件监听者为HelloActivity ,此类实现了OnClickListener接口。&lt;br&gt;二，放入布局，按布局显示两个Button&lt;br&gt;  三，按下其中一个Button,生成Click事件，调用HelloActivity 的OnClick接口函数。&lt;br&gt;四，对于View参数的值，判断是哪个View(Button)。改写Activity的Titile内容。注意，可别去对比View.getId()，缺省情况下，每个组件的Id值都为-1,除非人为设定Id值，用可视化编程时，为自动为其生成一个Id值。&lt;/div&gt;&lt;/div&gt;&lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-3920864968538061765?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/3920864968538061765/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-beginning-button-and-onclick.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3920864968538061765'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/3920864968538061765'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-beginning-button-and-onclick.html' title='android beginning :button and OnClick'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-6717407452323810810</id><published>2010-07-12T23:55:00.000-07:00</published><updated>2010-07-12T23:56:04.623-07:00</updated><title type='text'>Android API – SMS handling</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, &amp;#39;BitStream vera Sans&amp;#39;, Helvetica, sans-serif; font-size: 12px; color: rgb(85, 85, 85); "&gt;&lt;h2 style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 5px; padding-bottom: 3px; padding-left: 5px; font-weight: bolder; letter-spacing: -0.07em; font-family: Verdana, &amp;#39;BitStream vera Sans&amp;#39;; font-size: 16px; display: block; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); "&gt;  Android API – SMS handling&lt;/h2&gt;&lt;div class="info" style="margin-top: 5px; margin-right: 5px; margin-bottom: 5px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;&lt;span class="date" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 22px; background-image: url(http://www.androidcompetencycenter.com/wp-content/themes/inove/img/icons.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; height: 16px; line-height: 16px; display: block; font-size: 11px; float: left; background-position: 0px -48px; background-repeat: no-repeat no-repeat; "&gt;December 29th, 2008&lt;/span&gt;&lt;div class="act" 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; height: 16px; float: right; "&gt;  &lt;span class="comments" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 15px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 22px; background-image: url(http://www.androidcompetencycenter.com/wp-content/themes/inove/img/icons.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; height: 16px; line-height: 16px; display: block; font-size: 11px; float: left; background-position: 0px -96px; background-repeat: no-repeat no-repeat; "&gt;&lt;a href="#comments" 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; color: rgb(41, 112, 166); text-decoration: none; "&gt;Goto comments&lt;/a&gt;&lt;/span&gt;&lt;span class="addcomment" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 15px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 22px; background-image: url(http://www.androidcompetencycenter.com/wp-content/themes/inove/img/icons.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; height: 16px; line-height: 16px; display: block; font-size: 11px; float: left; background-position: 0px -112px; background-repeat: no-repeat no-repeat; "&gt;&lt;a href="#respond" 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; color: rgb(41, 112, 166); text-decoration: none; "&gt;Leave a comment&lt;/a&gt;&lt;/span&gt;&lt;div class="fixed" 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; clear: both; "&gt;  &lt;/div&gt;&lt;/div&gt;&lt;div class="fixed" 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; clear: both; "&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="content" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 5px; line-height: 17px; overflow-x: hidden; overflow-y: hidden; "&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Many new application will use SMS as data delivery platform. Reality shows, on-demand movies etc request users to send predefined formatted SMS. Similarly some applications are coming up which sends data to user using SMS. Let's see how such an application can be built using &lt;span class="hi_kw_0 hi_link" 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; text-decoration: none; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 2px; border-left-width: 0px; border-style: initial; border-color: initial; border-bottom-style: dotted; border-bottom-color: rgb(18, 15, 255); background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: rgb(18, 15, 255); cursor: pointer; background-position: initial initial; background-repeat: initial initial; "&gt;Android &lt;img class="hi_kw_0 hi_icon" src="http://cache2.hover.in/hi_link.gif" width="10px" height="10px" alt="[^]" 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-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; text-decoration: none; vertical-align: middle; max-width: 600px; border-style: initial; border-color: initial; "&gt;&lt;/span&gt;platform.&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Android API support developing applications that can send and receive SMS messages. The android emulator does not support sending of the SMS currently. But the emulator can receive SMS. Lets explore the android SMS support and develop a small program that listens to the SMSes received on the device (on emulator) and will show that message as notification.&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;The event handling on Android is done with the help of intents and intent receivers. The intents  announce (or broadcast) the event and intent receivers respond to the event. Intent receivers act as the event handlers.&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Let's define an intent receiver that can handle the SMS received event:&lt;/p&gt;  &lt;pre class="java" style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 15px; margin-left: 0pt; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 25px; background-image: url(http://www.androidcompetencycenter.com/wp-content/plugins/snippet-highlight/linenumbers.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); font-size: 12px; line-height: 14px; background-position: 0% 0%; background-repeat: no-repeat no-repeat; "&gt;  package com.wissen.sms.receiver; /** * The class is called when SMS is received. */ public class SMSReceiver extends BroadcastReceiver {  @Override public void onReceive(Context context, Intent intent) { // TODO } } &lt;/pre&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;We need to configure this intent receiver to receive SMS receive event. For SMS receive event android has defined an intent as ' android.provider.Telephony.SMS_RECEIVED '. The receiver can be configured in AndroidManifest.xml as follows:&lt;/p&gt;  &lt;pre class="xml" style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 15px; margin-left: 0pt; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 25px; background-image: url(http://www.androidcompetencycenter.com/wp-content/plugins/snippet-highlight/linenumbers.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); font-size: 12px; line-height: 14px; background-position: 0% 0%; background-repeat: no-repeat no-repeat; "&gt;       &lt;/pre&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;To receive SMS, application also needs to specify permission for receiving SMS. The permission can be set in AndroidManifest.xml as follows:&lt;/p&gt;  &lt;pre class="xml" style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 15px; margin-left: 0pt; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 25px; background-image: url(http://www.androidcompetencycenter.com/wp-content/plugins/snippet-highlight/linenumbers.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); font-size: 12px; line-height: 14px; background-position: 0% 0%; background-repeat: no-repeat no-repeat; "&gt;   &lt;/pre&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Now our intent receiver is all set to be called when the android device will receive SMS. Now we only need to retrieve the received SMS and show the SMS text in a notification.&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Here is the code of intent receiver that will read the SMS from intent received and show the first message (pdu).&lt;/p&gt;  &lt;pre class="java" style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 15px; margin-left: 0pt; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 25px; background-image: url(http://www.androidcompetencycenter.com/wp-content/plugins/snippet-highlight/linenumbers.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(204, 204, 204); border-right-color: rgb(204, 204, 204); border-bottom-color: rgb(204, 204, 204); border-left-color: rgb(204, 204, 204); font-size: 12px; line-height: 14px; background-position: 0% 0%; background-repeat: no-repeat no-repeat; "&gt;  public void onReceive(Context context, Intent intent) { 		Bundle bundle = intent.getExtras();  		Object messages[] = (Object[]) bundle.get(&amp;quot;pdus&amp;quot;); 		SmsMessage smsMessage[] = new SmsMessage[messages.length]; 		for (int n = 0; n &amp;amp;lt; messages.length; n++) { 		smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 		}  		// show first message 		Toast toast = Toast.makeText(context, 		&amp;quot;Received SMS: &amp;quot; + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG); 		toast.show(); 		} &lt;/pre&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;The SMS received by the Android device is in the form of pdus (protocol description unit). Class SmsMessage, defined in android.telephony.gsm package, can store information about the SMS. The class can also be used to create SmsMessage object from received pdus. Toast widget is used to show the SMS body as an notification.&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Running the Program:&lt;br 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; "&gt;  Only remaining thing now is running the application and sending the SMS message to the emulator. An SMS message can be sent to the emulator in the DDMS eclipse perspective (Dalvik Debug Monitor Service). 'Emulator Control' window can be used to send SMS message (an incoming number has to be provided which can be anything).&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;Here is the application screen shot in action,&lt;/p&gt;&lt;/div&gt;  &lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-6717407452323810810?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/6717407452323810810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-api-sms-handling.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/6717407452323810810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/6717407452323810810'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-api-sms-handling.html' title='Android API – SMS handling'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-1837883340014151114</id><published>2010-07-12T23:49:00.001-07:00</published><updated>2010-07-12T23:49:26.819-07:00</updated><title type='text'>android 界面安排</title><content type='html'>&lt;div&gt;这是我的一个Android程序的界面，很简单，一个输入用户名，一个输入密码，下面是一个提交按钮。&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;lt;LinearLayout xmlns:android=&amp;quot;&lt;a href="http://schemas.android.com/apk/res/android"&gt;http://schemas.android.com/apk/res/android&lt;/a&gt;&amp;quot;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:orientation=&amp;quot;vertical&amp;quot;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:layout_height=&amp;quot;fill_parent&amp;quot;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;TextView &amp;nbsp;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:layout_width=&amp;quot;fill_parent&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:text=&amp;quot;@string/hello&amp;quot;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;android:layout_centerHorizontal=&amp;quot;true&amp;quot;/&amp;gt; &amp;nbsp;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&amp;lt;LinearLayout xmlns:android=&amp;quot;&lt;a href="http://schemas.android.com/apk/res/android"&gt;http://schemas.android.com/apk/res/android&lt;/a&gt;&amp;quot;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:orientation=&amp;quot;horizontal&amp;quot;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:layout_width=&amp;quot;wrap_content&amp;quot;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:layout_height=&amp;quot;wrap_content&amp;quot;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:alwaysDrawnWithCache=&amp;quot;true&amp;quot;&amp;gt;&lt;/div&gt;  &lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;&amp;lt;TextView&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:id=&amp;quot;@+id/TextView01&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:layout_width=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:editable=&amp;quot;false&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:enabled=&amp;quot;true&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:singleLine=&amp;quot;true&amp;quot; android:text=&amp;quot;name:&amp;quot; android:minWidth=&amp;quot;80dip&amp;quot;&amp;gt;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;&amp;lt;/TextView&amp;gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;&amp;lt;EditText&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:id=&amp;quot;@+id/EditText01&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:layout_width=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:minWidth=&amp;quot;152dip&amp;quot;&amp;gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;&amp;lt;/EditText&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;  &lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&amp;lt;/LinearLayout&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&amp;lt;LinearLayout&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;android:orientation=&amp;quot;horizontal&amp;quot;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;android:layout_width=&amp;quot;wrap_content&amp;quot;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;android:layout_height=&amp;quot;wrap_content&amp;quot;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;&amp;gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;&amp;lt;TextView android:id=&amp;quot;@+id/TextView02&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:layout_width=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;android:text=&amp;quot;password:&amp;quot; android:minWidth=&amp;quot;80dip&amp;quot;&amp;gt;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;&amp;lt;/TextView&amp;gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;			&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;&amp;lt;EditText android:id=&amp;quot;@+id/EditText02&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;android:layout_width=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;android:password=&amp;quot;true&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;		&lt;/span&gt;android:minWidth=&amp;quot;152dip&amp;quot;&amp;gt;&amp;lt;/EditText&amp;gt;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&amp;lt;/LinearLayout&amp;gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&amp;lt;LinearLayout&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;android:orientation=&amp;quot;horizontal&amp;quot;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;android:layout_width=&amp;quot;fill_parent&amp;quot;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;android:layout_height=&amp;quot;wrap_content&amp;quot;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt; &amp;nbsp; &amp;nbsp;&amp;gt; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;Button android:id=&amp;quot;@+id/Button01&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:layout_width=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:layout_height=&amp;quot;wrap_content&amp;quot;&amp;nbsp;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:onClick=&amp;quot;btn_login&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:text=&amp;quot;login&amp;quot;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;android:layout_centerInParent=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;/Button&amp;gt;&lt;/div&gt;  &lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&amp;lt;/LinearLayout&amp;gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;  &lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;lt;/LinearLayout&amp;gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-1837883340014151114?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/1837883340014151114/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android_12.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1837883340014151114'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/1837883340014151114'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android_12.html' title='android 界面安排'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-5574025883390924077</id><published>2010-07-12T20:57:00.001-07:00</published><updated>2010-07-12T20:57:35.244-07:00</updated><title type='text'>android应用程序基础(包括活动、服务、广播、内容提供等）</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: verdana, sans-serif; font-size: 12px; line-height: 18px; "&gt;&lt;h1 class="title_txt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0.5em; padding-left: 1.5em; font-size: 15px; font-family: &amp;#39;Microsoft yahei&amp;#39;, verdana, sans-serif; border-bottom-width: 2px; border-bottom-style: dotted; border-bottom-color: rgb(220, 220, 220); "&gt;  &amp;nbsp;Android开发指南-框架主题-基础知识&amp;nbsp;&lt;cite class="fav_csdnstylebykimi" style="font-style: normal; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; display: inline; text-decoration: none; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;&lt;a href="" class="fav_csdnstylebykimi" title="收藏到我的网摘中，并分享给我的朋友" style="text-decoration: none; color: rgb(51, 102, 153); font: normal normal normal 12px/normal Tahoma, sans-serif; padding-top: 1px; padding-right: 25px; padding-bottom: 1px; padding-left: 10px; background-image: url(http://blog.csdn.net/images/share-add.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: 0px 0px; background-repeat: no-repeat no-repeat; "&gt;收藏&lt;/a&gt;&lt;/cite&gt;&lt;/h1&gt;  &lt;div class="blogstory" style="float: left; width: 831px; font-size: 14px; line-height: 21px; "&gt;&lt;div id="articlebody" class="itembody" style="position: static; height: 15957px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;  &lt;div id="article_body" style="height: 15957px; "&gt;&lt;h1 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0.5em; padding-left: 1.5em; font-size: 15px; font-family: &amp;#39;Microsoft yahei&amp;#39;, heiti, verdana, sans-serif; "&gt;  应用程序基础&lt;/h1&gt;&lt;h2 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; "&gt;关键类&lt;/h2&gt;&lt;ol style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;Activity&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;Service&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;BroadcastReceiver&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;ContentProvider&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Intent.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;Intent&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;  &lt;/ol&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;Android应用程序使用Java做为&lt;a class="bodytag" href="articles/tag/开发" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(51, 85, 51); "&gt;开发&lt;/span&gt;&lt;/a&gt;语言。&lt;a href="http://developer.android.com/guide/developing/tools/aapt.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;code style="font-style: normal; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;aapt&lt;/span&gt;&lt;/code&gt;&lt;/a&gt;工具把编译后的Java代码连同其它应用程序需要的数据和资源文件一起打包到一个Android包文件中，这个文件使用.apk做为扩展名，它是分发应用程序并安装到移动设备的媒介，用户只需下载并安装此文件到他们的设备。单一.apk文件中的所有代码被认为是一个应用程序。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;从很多方面来看，每个Android应用程序都存在于它自己的世界之中：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;默认情况下，每个应用程序均运行于它自己的Linux进程中。当应用程序中的任意代码开始执行时，Android启动一个进程，而当不再需要此进程而其它应用程序又需要系统资源时，则关闭这个进程。&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;每个进程都运行于自己的Java虚拟机（VM）中。所以应用程序代码实际上与其它应用程序的代码是隔绝的。&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;默认情况下，每个应用程序均被赋予一个唯一的Linux用户ID，并加以权限设置，使得应用程序的文件仅对这个用户、这个应用程序可见。当然，也有其它的方法使得这些文件同样能为别的应用程序所访问。&lt;/li&gt;  &lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;使两个应用程序共有同一个用户ID是可行的，这种情况下他们可以看到彼此的文件。从系统资源维护的角度来看，拥有同一个ID的应用程序也将在运行时使用同一个Linux进程，以及同一个虚拟机。&lt;/p&gt;  &lt;h2 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; "&gt;应用程序组件&lt;/h2&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  Android的核心功能之一就是一个应用程序可以使用其它应用程序的元素（如果那个应用程序允许的话）。比如说，如果你的应用程序需要一个图片卷动列表，而另一个应用程序已经&lt;a class="bodytag" href="articles/tag/开发" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(51, 85, 51); "&gt;开发&lt;/span&gt;&lt;/a&gt;了一个合用的而又允许别人使用的话，你可以直接调用那个卷动列表来完成工作，而不用自己再开发一个。你的应用程序并没有吸纳或链接其它应用程序的代码，它只是在有需求的时候启动了其它应用程序的那个功能部分。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;为达到这个目的，系统必须在一个应用程序的一部分被需要时启动这个应用程序，并将那个部分的Java对象实例化。与在其它系统上的应用程序不同，Android应用程序没有为应用准备一个单独的程序入口（比如说，没有&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;main()&lt;/code&gt;方法）， 而是为系统依照需求实例化提供了基本的组件。共有四种组件类型：&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;strong&gt;Activity&lt;/strong&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;&lt;/strong&gt;&lt;em style="font-style: italic; "&gt;Activity是为用户操作而展示的可视化用户界面。&lt;/em&gt;比如说，一个activity可以展示一个菜单项列表供用户选择，或者显示一些包含说明的照片。一个短消息应用程序可以包括一个用于显示做为发送对象的联系人的列表的activity，一个给选定的联系人写短信的activity以及翻阅以前的短信和改变设置的activity。尽管它们一起组成了一个内聚的用户界面，但其中每个activity都与其它的保持独立。每个都是以&lt;a href="http://developer.android.com/reference/android/app/Activity.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Activity&lt;/span&gt;&lt;/a&gt;类为基类的子类实现。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;一个应用程序可以只有一个activity，或者，如刚才提到的短信应用程序那样，包含很多个。每个activity的作用，以及其数目，自然取决于应用程序及其设计。一般情况下，总有一个应用程序被标记为用户在应用程序启动的时候第一个看到的。从一个activity转向另一个的方式是靠当前的activity启动下一个。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;每个activity都被给予一个默认的窗口以进行绘制。一般情况下，这个窗口是满屏的，但它也可以是一个小的位于其它窗口之上的浮动窗口。一个activity也可以使用超过一个的窗口──比如，在activity运行过程中弹出的一个供用户反应的小对话框，或是当用户选择了屏幕上特定项目后显示的必要信息。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;窗口显示的可视内容是由一系列视图构成的，这些视图均继承自&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/view/View.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;View&lt;/span&gt;&lt;/a&gt;&amp;nbsp;基类。每个视图均控制着窗口中一块特定的&lt;em style="font-style: italic; "&gt;矩形&lt;/em&gt;空间。父级视图包含并组织它子视图的布局。叶节点视图（位于视图层次最底端）在它们控制的矩形中进行绘制，并对用户对其直接操作做出响应。所以，视图是activity与用户进行交互的界面。比如说，视图可以显示一个小图片，并在用户指点它的时候产生动作。Android有很多既定的视图供用户直接使用，包括按钮、文本域、卷轴、菜单项、复选框等等。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;视图层次是由&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#setContentView%28android.view.View%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;Activity.setContentView()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法放入activity的窗口之中的。&lt;em style="font-style: italic; "&gt;上下文视图&lt;/em&gt;是位于视图层次根位置的视图对象。（参见&lt;a href="http://www.yeeyan.com/articles/view/37503/33890" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;用户界面&lt;/span&gt;&lt;/a&gt;章节获取关于视图及层次的更多信息。）&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;strong&gt;服务&lt;/strong&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;&lt;/strong&gt;服务没有可视化的用户界面，而是在一段时间内在后台运行。比如说，一个服务可以在用户做其它事情的时候在后台播放背景音乐、从网络上获取一些数据或者计算一些东西并提供给需要这个运算结果的activity使用。每个服务都继承自&lt;a href="http://developer.android.com/reference/android/app/Service.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Service&lt;/span&gt;&lt;/a&gt;基类。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;一个媒体播放器播放播放列表中的曲目是一个不错的例子。播放器应用程序可能有一个或多个activity来给用户选择歌曲并进行播放。然而，音乐播放这个任务本身不应该为任何activity所处理，因为用户期望在他们离开播放器应用程序而开始做别的事情时，音乐仍在继续播放。为达到这个目的，媒体播放器activity应该启用一个运行于后台的服务。而系统将在这个activity不再显示于屏幕之后，仍维持音乐播放服务的运行。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;你可以连接至（绑定）一个正在运行的服务（如果服务没有运行，则启动之）。连接之后，你可以通过那个服务暴露出来的接口与服务进行通讯。对于音乐服务来说，这个接口可以允许用户暂停、回退、停止以及重新开始播放。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如同activity和其它组件一样，服务运行于应用程序进程的主线程内。所以它不会对其它组件或用户界面有任何干扰，它们一般会派生一个新线程来进行一些耗时任务（比如音乐回放）。参见下述&amp;nbsp;&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#procthread" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;进程和线程&amp;nbsp;&lt;/span&gt;&lt;/a&gt;。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;strong&gt;广播接收器&lt;/strong&gt;&lt;strong&gt;&lt;/strong&gt;&lt;em style="font-style: italic; "&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;em style="font-style: italic; "&gt;广播接收器是一个专注于接收广播通知信息，并做出对应处理的组件。&lt;/em&gt;很多广播是源自于系统代码的──比如，通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以进行广播──比如说，通知其它应用程序一些数据下载完成并处于可用状态。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;br&gt;应用程序可以拥有任意数量的广播接收器以对所有它感兴趣的通知信息予以响应。所有的接收器均继承自&lt;a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;BroadcastReceiver&lt;/span&gt;&lt;/a&gt;基类。&lt;br&gt;  广播接收器没有用户界面。然而，它们可以启动一个activity来响应它们收到的信息，或者用&lt;a href="http://developer.android.com/reference/android/app/NotificationManager.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;NotificationManager&lt;/span&gt;&lt;/a&gt;来通知用户。通知可以用很多种方式来吸引用户的注意力──闪动背灯、震动、播放声音等等。一般来说是在状态栏上放一个持久的图标，用户可以打开它并获取消息。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;strong&gt;内容提供者&lt;/strong&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;em style="font-style: italic; "&gt;内容提供者&lt;/em&gt;将一些特定的应用程序数据供给其它应用程序使用。数据可以存储于文件系统、SQLite数据库或其它方式。内容提供者继承于&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;ContentProvider&lt;/span&gt;&lt;/a&gt;&amp;nbsp;基类，为其它应用程序取用和存储它管理的数据实现了一套标准方法。然而，应用程序并不直接调用这些方法，而是使用一个&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/content/ContentResolver.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;ContentResolver&lt;/span&gt;&lt;/a&gt;&amp;nbsp;对象，调用它的方法作为替代。ContentResolver可以与任意内容提供者进行会话，与其合作来对所有相关交互通讯进行管理。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;参阅独立的&lt;a href="http://developer.android.com/guide/topics/providers/content-providers.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;内容提供者&lt;/span&gt;&lt;/a&gt;章节获得更多关于使用内容提供者的内容。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;每当出现一个需要被特定组件处理的请求时，Android会确保那个组件的应用程序进程处于运行状态，或在必要的时候启动它。并确保那个相应组件的实例的存在，必要时会创建那个实例。&lt;/p&gt;  &lt;h3 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; "&gt;激活组件：intent&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当接收到ContentResolver发出的请求后，内容提供者被激活。而其它三种组件──activity、服务和广播接收器被一种叫做&lt;em style="font-style: italic; "&gt;intent&lt;/em&gt;的异步消息所激活。intent是一个保存着消息内容的&lt;a href="http://developer.android.com/reference/android/content/Intent.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Intent&lt;/span&gt;&lt;/a&gt;对象。对于activity和服务来说，它指明了请求的操作名称以及作为操作对象的数据的URI和其它一些信息。比如说，它可以承载对一个activity的请求，让它为用户显示一张图片，或者让用户编辑一些文本。而对于广播接收器而言，Intent对象指明了声明的行为。比如，它可以对所有感兴趣的对象声明照相按钮被按下。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;对于每种组件来说，激活的方法是不同的：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;通过传递一个Intent对象至&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#startActivity%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.startActivity()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;或&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Activity.startActivityForResult()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;以载入（或指定新工作给）一个activity。相应的activity可以通过调用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#getIntent%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;getIntent()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法来查看激活它的intent。Android通过调用activity的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onNewIntent()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法来传递给它继发的intent。&lt;br&gt;  一个activity经常启动了下一个。如果它期望它所启动的那个activity返回一个结果，它会以调用&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivityForResult()来取代&lt;/code&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivity()。比如说，如果它启动了另外一个activity以使用户挑选一张照片，它也许想知道哪张照片被选中了。结果将会被封装在一个Intent对象中，并传递给发出调用的activity的&lt;/code&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onActivityResult%28int,%20int,%20android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;onActivityResult()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;通过传递一个Intent对象至&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.startService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;将启动一个服务（或给予正在运行的服务以一个新的指令）。Android调用服务的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法并将Intent对象传递给它。&lt;br&gt;  与此类似，一个Intent可以被调用组件传递给&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.bindService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;以获取一个正在运行的目标服务的连接。这个服务会经由&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onBind%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onBind()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法的调用获取这个Intent对象（如果服务尚未启动，&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;bindService()会先启动它&lt;/code&gt;）。比如说，一个activity可以连接至前述的音乐回放服务，并提供给用户一个可操作的（用户界面）以对回放进行控制。这个activity可以调用&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;bindService()&lt;/code&gt;&amp;nbsp;来建立连接，然后调用服务中定义的对象来影响回放。&lt;br&gt;  后面一节：&lt;/span&gt;&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#rpc" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;远程方法调用&lt;/span&gt;&lt;/a&gt;将更详细的阐明如何绑定至服务。&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;应用程序可以凭借将Intent对象传递给&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#sendBroadcast%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.sendBroadcast()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;，&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#sendOrderedBroadcast%28android.content.Intent,%20java.lang.String%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.sendOrderedBroadcast()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;， 以及&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#sendStickyBroadcast%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.sendStickyBroadcast()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;和其它类似方法来产生一个广播。Android会调用所有对此广播有兴趣的广播接收器的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onReceive()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法，将intent传递给它们。&lt;/li&gt;  &lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;欲了解更多intent消息的信息，请参阅独立章节&amp;nbsp;&lt;a href="http://developer.android.com/guide/topics/intents/intents-filters.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Intent和Intent滤过器&lt;/span&gt;&lt;/a&gt;。&lt;/p&gt;  &lt;h3 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; "&gt;关闭组件&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  内容提供者仅在响应ContentResolver提出请求的时候激活。而一个广播接收器仅在响应广播信息的时候激活。所以，没有必要去显式的关闭这些组件。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  而activity则不同，它提供了用户界面，并与用户进行会话。所以只要会话依然持续，哪怕对话过程暂时停顿，它都会一直保持激活状态。与此相似，服务也会在很长一段时间内保持运行。所以Android为关闭activity和服务提供了一系列的方法。&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;可以通过调用它的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#finish%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;finish()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法来关闭一个activity。一个activity可以通过调用另外一个activity（它用&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivityForResult() 启动&lt;/code&gt;的）的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#finishActivity%28int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;finishActivity()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法来关闭它。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;服务可以通过调用它的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#stopSelf%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;stopSelf()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法来停止，或者调用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.stopService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;。&lt;/li&gt;  &lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;系统也会在组件不再被使用的时候或者Android需要为活动组件声明更多内存的时候关闭它。后面的&amp;nbsp;&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#lcycles" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;组件的生命周期&lt;/span&gt;&lt;/a&gt;一节，将对这种可能及附属情况进行更详细的讨论。&lt;/p&gt;  &lt;h3 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; "&gt;manifest文件&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当Android启动一个应用程序组件之前，它必须知道那个组件是存在的。所以，应用程序会在一个manifest文件中声明它的组件，这个文件会被打包到Android包中。这个.apk文件还将涵括应用程序的代码、文件以及其它资源。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  这个manifest文件以XML作为结构格式，而且对于所有应用程序，都叫做AndroidManifest.xml。为声明一个应用程序组件，它还会做很多额外工作，比如指明应用程序所需链接到的库的名称（除了默认的Android库之外）以及声明应用程序期望获得的各种权限。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  但manifest文件的主要功能仍然是向Android声明应用程序的组件。举例说明，一个activity可以如下声明：&lt;/p&gt;&lt;pre&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br&gt;&amp;lt;manifest . . . &amp;gt;&lt;br&gt;    &amp;lt;application . . . &amp;gt;&lt;br&gt;        &amp;lt;activity &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:name=&amp;quot;com.example.project.FreneticActivity&amp;quot;&lt;br&gt;                    &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:icon=&amp;quot;@drawable/small_pic.png&amp;quot;&lt;br&gt;                    &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:label=&amp;quot;@string/freneticLabel&amp;quot; &lt;br&gt;                    . . .  &amp;gt;&lt;br&gt;        &amp;lt;/activity&amp;gt;&lt;br&gt;        . . .&lt;br&gt;    &amp;lt;/application&amp;gt;&lt;br&gt;&amp;lt;/manifest&amp;gt;&lt;/pre&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;&amp;lt;activity&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;元素的name属性指定了实现了这个activity的&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/app/Activity.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Activity&lt;/span&gt;&lt;/a&gt;的子类。icon和label属性指向了包含展示给用户的此activity的图标和标签的资源文件。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;其它组件也以类似的方法声明──&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/service-element.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;&amp;lt;service&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;元素用于声明服务，&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/receiver-element.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;&amp;lt;receiver&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;元素用于声明广播接收器，而&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/provider-element.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;&amp;lt;provider&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;元素用于声明内容提供者。 manifest文件中未进行声明的activity、服务以及内容提供者将不为系统所见，从而也就不会被运行。然而，广播接收器既可以在manifest文件中声明，也可以在代码中进行动态的创建，并以调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.registerReceiver()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;的方式注册至系统。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;欲更多了解如何为你的应用程序构建manifest文件，请参阅&lt;a href="http://developer.android.com/guide/topics/manifest/manifest-intro.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;AndroidManifest.xml文件&lt;/span&gt;&lt;/a&gt;一章。&lt;/p&gt;  &lt;h3 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; "&gt;Intent过滤器&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  Intent对象可以被显式的指定目标组件。如果进行了这种指定，Android会找到这个组件（依据manifest文件中的声明）并激活它。但如果Intent没有进行显式的指定，Android就必须为它找到对于intent来说最合适的组件。这个过程是通过比较Intent对象和所有可能对象的&lt;em style="font-style: italic; "&gt;intent过滤器&lt;/em&gt;完成的。组件的intent过滤器会告知Android它所能处理的intent类型。如同其它相对于组件很重要的信息一样，这些是在manifest文件中进行声明的。这里是上面实例的一个扩展，其中加入了针对activity的两个intent过滤器声明：&lt;/p&gt;  &lt;pre&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br&gt;&amp;lt;manifest . . . &amp;gt;&lt;br&gt;    &amp;lt;application . . . &amp;gt;&lt;br&gt;        &amp;lt;activity &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:name=&amp;quot;com.example.project.FreneticActivity&amp;quot;&lt;br&gt;                    &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:icon=&amp;quot;@drawable/small_pic.png&amp;quot;&lt;br&gt;                    &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:label=&amp;quot;@string/freneticLabel&amp;quot; &lt;br&gt;                    . . .  &amp;gt;&lt;br&gt;            &amp;lt;intent-filter . . . &amp;gt;&lt;br&gt;                &amp;lt;action &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:name=&amp;quot;android.intent.action.MAIN&amp;quot; /&amp;gt;&lt;br&gt;                  &amp;lt;category &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:name=&amp;quot;android.intent.category.LAUNCHER&amp;quot; /&amp;gt;&lt;br&gt;              &amp;lt;/intent-filter&amp;gt;&lt;br&gt;            &amp;lt;intent-filter . . . &amp;gt;&lt;br&gt;                &amp;lt;action &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:name=&amp;quot;com.example.project.BOUNCE&amp;quot; /&amp;gt;&lt;br&gt;                  &amp;lt;data &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:type=&amp;quot;image/jpeg&amp;quot; /&amp;gt;&lt;br&gt;                  &amp;lt;category &lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(51, 85, 51); "&gt;android&lt;/span&gt;&lt;/a&gt;:name=&amp;quot;android.intent.category.DEFAULT&amp;quot; /&amp;gt;&lt;br&gt;              &amp;lt;/intent-filter&amp;gt;&lt;br&gt;        &amp;lt;/activity&amp;gt;&lt;br&gt;        . . .&lt;br&gt;    &amp;lt;/application&amp;gt;&lt;br&gt;&amp;lt;/manifest&amp;gt;&lt;/pre&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  示例中的第一个过滤器──action &amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(51, 85, 51); font-family: Verdana; "&gt;android&lt;/span&gt;&lt;/a&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;.intent.action.MAIN&lt;/span&gt;&lt;/code&gt;&amp;rdquo;和类别&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(51, 85, 51); font-family: Verdana; "&gt;android&lt;/span&gt;&lt;/a&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;.intent.category.LAUNCHER&lt;/span&gt;&lt;/code&gt;&amp;rdquo;的组合──是通常具有的。它标明了这个activity将在应用程序加载器中显示，就是用户在设备上看到的可供加载的应用程序列表。换句话说，这个activity是应用程序的入口，是用户选择运行这个应用程序后所见到的第一个activity。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;第二个过滤器声明了这个activity能被&lt;em style="font-style: italic; "&gt;赋予&lt;/em&gt;一种特定类型的数据。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;组件可以拥有任意数量的intent过滤器，每个都会声明一系列不同的能力。如果它没有包含任何过滤器，它将只能被显式声明了目标组件名称的intent激活。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;对于在代码中创建并注册的广播接收器来说，intent过滤器将被直接以&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/content/IntentFilter.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;IntentFilter&lt;/span&gt;&lt;/a&gt;对象实例化。其它过滤器则在manifest文件中设置。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;欲获得更多intent过滤器的信息，请参阅独立章节：&amp;nbsp;&lt;a href="http://developer.android.com/guide/topics/intents/intents-filters.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Intent和Intent过滤器&lt;/span&gt;&lt;/a&gt;。&lt;/p&gt;  &lt;h2 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; "&gt;Activity和任务&lt;/h2&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  如前所述，一个activity可以启动另外一个，甚至包括与它不处于同一应用程序之中的。举个例子说，假设你想让用户看到某个地方的街道地图。而已经存在一个具有此功能的activity了，那么你的activity所需要做的工作就是把请求信息放到一个Intent对象里面，并把它传递给&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivity()&lt;/code&gt;。于是地图浏览器就会显示那个地图。而当用户按下BACK键的时候，你的activity又会再一次的显示在屏幕上。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;对于用户来说，这看起来就像是地图浏览器是你activity所在的应用程序中的一个组成部分，其实它是在另外一个应用程序中定义，并运行在那个应用程序的进程之中的。Android将这两个activity放在&lt;em style="font-style: italic; "&gt;同一个任务&lt;/em&gt;中来维持一个完整的用户体验。简单的说，任务就是用户所体验到的&amp;ldquo;应用程序&amp;rdquo;。它是安排在一个堆栈中的一组相关的activity。堆栈中的根activity就是启动了这整个任务的那个──一般情况下，它就是用户在应用程序加载器中所选择的。而堆栈最上方的activity则是当前运行的──用户直接对其进行操作的。当一个activity启动另外一个的时候，新的activity就被压入堆栈，并成为当前运行的activity。而前一个activity仍保持在堆栈之中。当用户按下BACK键的时候，当前activity出栈，而前一个恢复为当前运行的activity。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;堆栈中保存的其实是对象，所以如果发生了诸如需要多个地图浏览器的情况，就会使得一个任务中出现多个同一Activity子类的实例同时存在，堆栈会为每个实例单独开辟一个入口。堆栈中的Activity永远不会重排，只会压入或弹出。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;任务其实就是activity的堆栈，而不是manifest文件中的一个类或者元素。所以你无法撇开activity而为一个任务设置一个值。而事实上整个任务使用的值是在根activity中设置的。比如说，下一节我们会谈及&amp;ldquo;任务的affinity&amp;rdquo;，从affinity中读出的值将会设置到任务的根activity之中。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;任务中的所有activity是作为一个整体进行移动的。整个的任务（即activity堆栈）可以移到前台，或退至后台。举个例子说，比如当前任务在堆栈中存有四个activity──三个在当前activity之下。当用户按下HOME键的时候，回到了应用程序加载器，然后选择了一个新的应用程序（也就是一个新&lt;em style="font-style: italic; "&gt;任务&lt;/em&gt;）。则当前任务遁入后台，而新任务的根activity显示出来。然后，过了一小会儿，用户再次回到了应用程序加载器而又选择了前一个应用程序（上一个任务）。于是那个任务，带着它堆栈中所有的四个activity，再一次的到了前台。&lt;em style="font-style: italic; "&gt;当用户按下BACK键的时候，屏幕不会显示出用户刚才离开的activity（上一个任务的根activity）。取而代之，当前任务的堆栈中最上面的activity被弹出，而同一任务中的上一个activity显示了出来。&lt;/em&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;上述的种种即是activity和任务的默认行为模式。但是有一些方法可以改变所有这一切。activity和任务的联系、任务中activity的行为方式都被启动那个activity的Intent对象中设置的一系列标记和manifest文件中那个activity中的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;&amp;lt;activity&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;元素的系列属性之间的交互所控制。无论是请求发出者和回应者在这里都拥有话语权。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;我们刚才所说的这些关键Intent标记如下：&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;  &lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_CLEAR_TOP&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_RESET_TASK_IF_NEEDED&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_SINGLE_TOP&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  而关键的&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;activity&amp;gt;&lt;/code&gt;属性是：&lt;/span&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;  &lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;taskAffinity&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;launchMode&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;allowTaskReparenting&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;clearTaskOnLaunch&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;alwaysRetainTaskState&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;finishOnTaskLaunch&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  接下来的一节会描述这些标记以及属性的作用，它们是如何互相影响的，以及控制它们的使用时必须考虑到的因素。&lt;/p&gt;&lt;h3 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; "&gt; Affinity（吸引力）和新任务&lt;/h3&gt; &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;默认情况下，一个应用程序中的activity相互之间会有一种Affinity──也就是说，它们首选都归属于一个任务。然而，可以在&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;activity&amp;gt;元素中把每个activity的&lt;/code&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;taskAffinity&lt;/code&gt;属性设置为一个独立的affinity。于是在不同的应用程序中定义的activity可以享有同一个affinity，或者在同一个应用程序中定义的activity有着不同的affinity。affinity在两种情况下生效：当加载activity的Intent对象包含了&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;&amp;nbsp;标记，或者当activity的&lt;code class="Code prettyprint" style="font-style: normal; "&gt;allowTaskReparenting&lt;/code&gt;属性设置为&amp;ldquo;true&amp;rdquo;。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;标记&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如前所述，在默认情况下，一个新activity被另外一个调用了&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivity()&lt;/code&gt;方法的activity载入了任务之中。并压入了调用者所在的堆栈。然而，如果传递给&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivity()&lt;/code&gt;的Intent对象包含了&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;标记，系统会为新activity安排另外一个任务。一般情况下，如同标记所暗示的那样，这会是一个新任务。然而，这并不是必然的。如果已经存在了一个与新activity有着同样affinity的任务，则activity会载入那个任务之中。如果没有，则启用新任务。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html#reparent" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;allowTaskReparenting&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;属性&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如果一个activity将&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;allowTaskReparenting&lt;/code&gt;属性设置为&amp;ldquo;true&amp;rdquo;。它就可以从初始的任务中转移到与其拥有同一个affinity并转向前台的任务之中。比如说，一个旅行应用程序中包含的预报所选城市的天气情况的activity。它与这个应用程序中其它的activity拥有同样的affinity（默认的affinity）而且允许重定父级。你的另一个activity启动了天气预报，于是它就会与这个activity共处与同一任务之中。然而，当那个旅行应用程序再次回到前台的时候，这个天气预报activity就会被再次安排到原先的任务之中并显示出来。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如果在用户的角度看来，一个.apk文件中包含了多于一个的&amp;ldquo;应用程序&amp;rdquo;，你可能会想要为它们所辖的activity安排不一样的affinity。&lt;/p&gt;  &lt;h3 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; "&gt;加载模式&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;activity&amp;gt;&lt;/code&gt;元素的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html#lmode" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;launchMode&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;属性可以设置四种不同的加载模式：&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;&amp;quot;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;quot; （默认值）&lt;br&gt;  &amp;quot;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;quot;&amp;nbsp;&lt;br&gt;&amp;quot;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;quot;&amp;nbsp;&lt;br&gt;&amp;quot;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;quot;&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;这些模式之间的差异主要体现在四个方面：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;strong&gt;哪个任务会把持&lt;/strong&gt;&lt;strong&gt;对intent做出响应的&lt;/strong&gt;&lt;strong&gt;activity。&lt;/strong&gt;对&amp;ldquo;&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;和&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;rdquo;模式而言，是产生intent（并调用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#startActivity%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;startActivity()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;）的任务──除非Intent对象包含&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;标记。而在这种情况下，如同上面&lt;/span&gt;&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#afftask" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;Affinitie和新任务&lt;/span&gt;&lt;/a&gt;一节所述，会是另外一个任务。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  相反，对&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;和&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;模式而言，activity总是位于任务的根部。正是它们定义了一个任务，所以它们绝不会被载入到其它任务之中。&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;activity是否可以存在多个实例。&lt;/strong&gt;一个&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;或&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;rdquo;的activity可以被多次初始化。它们可以归属于多个任务，而一个任务也可以拥有同一activity的多个实例。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;相反，对&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;和&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;的activity被限定于只能有一个实例。因为这些activity都是任务的起源，这种限制意味着在一个设备中同一时间只允许存在一个任务的实例。&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;在实例所在的任务中是否会有别的activity。&lt;/strong&gt;一个&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;模式的activity将会是它所在的任务中唯一的activity。如果它启动了别的activity，那个activity将会依据它自己的加载模式加载到其它的任务中去──如同在intent中设置了&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;&amp;nbsp;标记一样的效果。在其它方面，&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;模式的效果与&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;是一样的。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;剩下的三种模式允许一个任务中出现多个activity。&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;模式的activity将是任务的根activity，但它可以启动别的activity并将它们置入所在的任务中。&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;和&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;rdquo;activity则可以在堆栈的任意位置出现。&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;/ul&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;  &lt;strong&gt;是否要载入新的类实例以处理新的intent。&lt;/strong&gt;对默认的&amp;quot;&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;quot;模式来说，对于每个新intent都会创建一个新的实例以进行响应，每个实例仅处理一个intent。&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;rdquo;模式下，如果activity位于目的任务堆栈的最上面，则重用目前现存的activity来处理新的intent。如果它不是在堆栈顶部，则不会发生重用。而是创建一个新实例来处理新的intent并将其推入堆栈。&lt;/span&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  举例来说，假设一个任务的堆栈由根activityA和activity B、C和位于堆栈顶部的D组成，即堆栈A-B-C-D。一个针对D类型的activity的intent抵达的时候，如果D是默认的&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;加载模式，则创建并加载一个新的类实例，于是堆栈变为A-B-C-D-D。 然而，如果D的载入模式为&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;rdquo;，则现有的实例会对新intent进行处理（因为它位于堆栈顶部）而堆栈保持A-B-C-D的形态。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;换言之，如果新抵达的intent是针对B类型的activity，则无论B的模式是&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;还是&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTop&lt;/code&gt;&amp;rdquo; ，都会加载一个新的B的实例（因为B不位于堆栈的顶部），而堆栈的顺序变为A-B-C-D-B。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如前所述，&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;或&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;模式的activity永远不会存在多于一个实例。所以实例将处理所有新的intent。一个&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;模式的activity永远保持在堆栈的顶部（因为它是那个堆栈中唯一的一个activity），所以它一直坚守在处理intent的岗位上。然而，对一个&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;模式的activity来说，它上面可能有，也可能没有别的activity和它处于同一堆栈。&lt;code class="Code prettyprint" style="font-style: normal; "&gt;在有的情况下，它就不在能够处理intent的位置上，&lt;em style="font-style: italic; "&gt;则那个intent将被舍弃&lt;/em&gt;&lt;/code&gt;。（即便在intent被舍弃的情况下，它的抵达仍将使这个任务切换至前台，并一直保留）&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;当一个现存的activity被要求处理一个新的intent的时候，会调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onNewIntent()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法来将intent对象传递至activity。（启动activity的原始intent对象可以通过调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#getIntent%28%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;getIntent()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法获得。）&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;请注意，当一个新的activity实例被创建以处理新的intent的时候，用户总可以按下BACK键来回到前面的状态（回到前一个activity）。但当使用现存的activity来处理新intent的时候，用户是不能靠按下BACK键回到当这个新intent抵达之前的状态的。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;想获得更多关于加载模式的内容，请参阅&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;&amp;lt;activity&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;元素的描述。&lt;/p&gt;  &lt;h3 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; "&gt;清理堆栈&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  如果用户离开一个任务很长一段时间，系统会清理该任务中除了根activity之外的所有activity。当用户再次回到这个任务的时候，除了只剩下初始化activity尚存之外，其余都跟用户上次离开它的时候一样。这样做的原因是：在一段时间之后，用户再次回到一个任务的时候，他们更期望放弃他们之前的所作所为，做些新的事情。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  这些属于默认行为，另外，也存在一些activity的属性用以控制并改变这些行为：&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html#always" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;alwaysRetainTaskState&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;属性&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 30px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如果一个任务的根activity中此属性设置为&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;true&lt;/code&gt;&amp;rdquo;，则上述默认行为不会发生。任务将在很长的一段时间内保留它堆栈内的所有activity。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html#clear" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;clearTaskOnLaunch&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;属性&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 30px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如果一个任务的根activity中此属性设置为&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;true&lt;/code&gt;&amp;rdquo;，则每当用户离开这个任务和返回它的时候，堆栈都会被清空至只留下rootactivity。换句话说，这是&lt;code class="Code prettyprint" style="font-style: normal; "&gt;alwaysRetainTaskState&lt;/code&gt;的另一个极端。哪怕仅是过了一小会儿，用户回到任务时，也是见到它的初始状态。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/topics/manifest/activity-element.html#finish" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;finishOnTaskLaunch&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;属性&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 30px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;这个属性与&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;clearTaskOnLaunch&lt;/code&gt;属性相似，但它仅作用于单个的activity，而不是整个的task。而且它可以使任意activity都被清理，甚至根activity也不例外。当它设置为&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;true&lt;/code&gt;&amp;rdquo;的时候，此activity仅做为任务的一部分存在于当前回话中，一旦用户离开并再次回到这个任务，此activity将不复存在。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;此外，还有别的方式从堆栈中移除一个activity。如果一个intent对象包含&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;FLAG_ACTIVITY_CLEAR_TOP&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;标记，而且目标任务的堆栈中已经存在了一个能够响应此intent的activity类型的实例。则这个实例之上的所有activity都将被清理以使它位于堆栈的顶部来对intent做出响应。如果此时指定的activity的加载模式为&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;，则它本身也会从堆栈中移除，并加载一个新的实例来处理到来的intent。这是因为加载模式为&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;standard&lt;/code&gt;&amp;rdquo;的activity总会创建一个新实例来处理新的intent。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_CLEAR_TOP&lt;/code&gt;与&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;经常合并使用。这时，这些标记提供了一种定位其它任务中现存的activity并将它们置于可以对intent做出响应的位置的方法。&lt;/span&gt;&lt;/p&gt;  &lt;h3 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; "&gt;启动任务&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当一个activity被指定一个&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(51, 85, 51); font-family: Verdana; "&gt;android&lt;/span&gt;&lt;/a&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;.intent.action.MAIN&lt;/span&gt;&lt;/code&gt;&amp;rdquo;做为动作，以及&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;a class="bodytag" href="articles/tag/android" target="_blank" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(51, 85, 51); font-family: Verdana; "&gt;android&lt;/span&gt;&lt;/a&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;.intent.category.LAUNCHER&lt;/span&gt;&lt;/code&gt;&amp;rdquo;做为类别的intent过滤器之后（在前述&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#ifilters" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;intent过滤器&lt;/span&gt;&lt;/a&gt;一节中已经有了这个示例），它就被设置为一个任务的入口点。这样的过滤器设置会在应用程序加载器中为此activity显示一个图标和标签，以供用户加载任务或加载之后在任意时间回到这个任务。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;第二个能力相当重要：用户必须可以离开一个任务，并在一段时间后返回它。出于这个考虑，加载模式被设定为&amp;ldquo;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;和&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleInstance&lt;/code&gt;&amp;rdquo;的activity总是会初始化一个新任务，这样的activity仅能用于指定了一个&lt;code class="Code prettyprint" style="font-style: normal; "&gt;MAIN&lt;/code&gt;和&lt;code class="Code prettyprint" style="font-style: normal; "&gt;LAUNCHER&lt;/code&gt;过滤器的情况之下。我们来举例说明如果没指定过滤器的情况下会发生的事情：一个intent加载了一个&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;singleTask&lt;/code&gt;&amp;rdquo;的activity，初始化了一个新任务，用户在这个任务中花费了一些时间来完成工作。然后用户按下了HOME键。于是任务被要求转至后台并被主屏幕所掩盖。因为它并没有在应用程序加载器中显示图标，这将导致用户无法再返回它。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;类似的困境也可由&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;标记引起。如果此标记使一个activity启动了一个新任务继而用户按下了HOME键离开了它，则用户必须要有一些方法再次回到这个任务。一些实体（诸如通知管理器）总是在另外的任务中启动新activity，而不是做为它们自己的一部分，所以它们总是将&lt;code class="Code prettyprint" style="font-style: normal; "&gt;FLAG_ACTIVITY_NEW_TASK&lt;/code&gt;标记包含在intent里面并传递给&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startActivity()&lt;/code&gt;。如果你写了一个能被外部实体使用这个标记调用的activity，你必须注意要给用户留一个返回这个被外部实体启动的任务的方法。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;当你不想让用户再次返回一个activity的情况下，可以将&amp;nbsp;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;activity&amp;gt;&lt;/code&gt;&amp;nbsp;元素的&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;finishOnTaskLaunch&lt;/code&gt;设置为&amp;ldquo;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;true&lt;/code&gt;&amp;rdquo;。参见前述&lt;/span&gt;&lt;a href="http://www.yeeyan.com/articles/view/37503/34036#clearstack" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;清理堆栈&lt;/span&gt;&lt;/a&gt;。.&lt;/p&gt;  &lt;h2 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; "&gt;进程和线程&lt;/h2&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当一个应用程序开始运行它的第一个组件时，Android会为它启动一个Linux进程，并在其中执行一个单一的线程。默认情况下，应用程序所有的组件均在这个进程的这个线程中运行。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  然而，你也可以安排组件在其他进程中运行，而且可以为任意进程衍生出其它线程。&lt;/p&gt;&lt;h3 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; "&gt;进程&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  组件运行所在的进程由manifest文件所控制。组件元素&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;mdash;&amp;mdash;&amp;lt;activity&amp;gt;&lt;/code&gt;，&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;service&amp;gt;&lt;/code&gt;，&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;receiver&amp;gt;&lt;/code&gt;和&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;provider&amp;gt;&lt;/code&gt;&amp;mdash;&amp;mdash;都有一个&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;process&lt;/code&gt;&amp;nbsp;属性来指定组件应当运行于哪个进程之内。这些属性可以设置为使每个组件运行于它自己的进程之内，或一些组件共享一个进程而其余的组件不这么做。它们也可以设置为令不同应用程序的组件在一个进程中运行&amp;mdash;&amp;mdash;使应用程序的组成部分共享同一个Linux用户ID并赋以同样的权限。&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&amp;lt;application&amp;gt;&lt;/code&gt;元素也有一个&lt;code class="Code prettyprint" style="font-style: normal; "&gt;process&lt;/code&gt;属性，以设定所有组件的默认值。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;所有的组件实例都位于特定进程的主线程内，而对这些组件的系统调用也将由那个线程进行分发。一般不会为每个实例创建线程。因此，某些方法总是运行在进程的主线程内，这些方法包括诸如&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/view/View.html#onKeyDown%28int,%20android.view.KeyEvent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;View.onKeyDown()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;这样报告用户动作以及后面&amp;nbsp;&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#lcycles" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;组件生命周期&lt;/span&gt;&lt;/a&gt;一节所要讨论的生命周期通告的。这意味着组件在被系统调用的时候，不应该施行长时间的抑或阻塞的操作（诸如网络相关操作或是循环计算），因为这将阻塞同样位于这个进程的其它组件的运行。你应该如同下面&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#threads" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;线程&lt;/span&gt;&lt;/a&gt;一节所叙述的那样，为这些长时间操作衍生出一个单独的线程进行处理。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;在可用内存不足而又有一个正在为用户进行服务的进程需要更多内存的时候，Android有时候可能会关闭一个进程。而在这个进程中运行着的应用程序也因此被销毁。当再次出现需要它们进行处理的工作的时候，会为这些组件重新创建进程。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;在决定结束哪个进程的时候，Android会衡量它们对于用户的相对重要性。比如说，相对于一个仍有用户可见的activity的进程，它更有可能去关闭一个其activity已经不为用户所见的进程。也可以说，决定是否关闭一个进程主要依据在那个进程中运行的组件的状态。这些状态将在后续的一节&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#lcycles" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;组件生命周期&lt;/span&gt;&lt;/a&gt;中予以说明。&lt;/p&gt;  &lt;h3 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; "&gt;线程&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  尽管你可以把你的应用程序限制于一个单独的进程中，有时，你仍然需要衍生出一个线程以处理后台任务。因为用户界面必须非常及时的对用户操作做出响应，所以，控管activity的线程不应用于处理一些诸如网络下载之类的耗时操作。所有不能在瞬间完成的任务都应安排到不同的线程中去。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  线程在代码中是以标准Java&amp;nbsp;&lt;a href="http://developer.android.com/reference/java/lang/Thread.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Thread&lt;/span&gt;&lt;/a&gt;对象创建的。Android提供了很多便于管理线程的类：&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/os/Looper.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Looper&lt;/span&gt;&lt;/a&gt;用于在一个线程中运行一个消息循环，&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/os/Handler.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Handler&lt;/span&gt;&lt;/a&gt;用于处理消息，&lt;a href="http://developer.android.com/reference/android/os/HandlerThread.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;HandlerThread&lt;/span&gt;&lt;/a&gt;&amp;nbsp;用于使用一个消息循环启用一个线程。&lt;/p&gt;  &lt;h3 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; "&gt;远程过程调用&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  Android有一个轻量级的远程过程调用（RPC）机制：即在本地调用一个方法，但在远程（其它的进程中）进行处理，然后将结果返回调用者。这将方法调用及其附属的数据以系统可以理解的方式进行分离，并将其从本地进程和本地地址空间传送至远程过程和远程地址空间，并在那里重新装配并对调用做出反应。返回的结果将以相反的方向进行传递。Android提供了完成这些工作所需的所有的代码，以使你可以集中精力来实现RPC接口本身。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  RPC接口可以只包括方法。即便没有返回值，所有方法仍以同步的方式执行（本地方法阻塞直至远程方法结束）。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  简单的说，这套机制是这样工作的：一开始，你用简单的IDL（界面描绘语言）声明一个你想要实现的RPC接口。然后用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/guide/developing/tools/aidl.html" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;aidl&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;工具为这个声明生成一个Java接口定义，这个定义必须对本地和远程进程都可见。它包含两个内部类，如下图所示：&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;&lt;img src="http://developer.android.com/images/binder_rpc.png" alt="RPC mechanism." style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;内部类中有管理实现了你用IDL声明的接口的远程方法调用所需要的所有代码。两个内部类均实现了&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/os/IBinder.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;IBinder&lt;/span&gt;&lt;/a&gt;接口。一个用于系统在本地内部使用，你些的代码可以忽略它；另外一个，我们称为Stub，扩展了&lt;a href="http://developer.android.com/reference/android/os/Binder.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Binder&lt;/span&gt;&lt;/a&gt;类。除了实现了IPC调用的内部代码之外，它还包括了你声明的RPC接口中的方法的声明。你应该如上图所示的那样写一个Stub的子类来实现这些方法。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;一般情况下，远程过程是被一个服务所管理的（因为服务可以通知系统关于进程以及它连接到别的进程的信息）。它包含着&amp;nbsp;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;aidl&lt;/code&gt;工具产生的接口文件和实现了RPC方法的Stub的子类。而客户端只需要包括&lt;code class="Code prettyprint" style="font-style: normal; "&gt;aidl&lt;/code&gt;工具产生的接口文件。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;下面将说明服务与其客户端之间的连接是如何建立的：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;服务的客户端（位于本地）应该实现&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ServiceConnection.html#onServiceConnected%28android.content.ComponentName,%20android.os.IBinder%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onServiceConnected()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;和&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ServiceConnection.html#onServiceDisconnected%28android.content.ComponentName%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onServiceDisconnected()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法。这样，当至远程服务的连接成功建立或者断开的时候，它们会收到通知。这样它们就可以调用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;bindService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;来设置连接。&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;而服务则应该实现&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onBind%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onBind()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法以接受或拒绝连接。这取决于它收到的intent（intent将传递给&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;bindService()&lt;/code&gt;）。如果接受了连接，它会返回一个Stub的子类的实例。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;如果服务接受了连接，Android将会调用客户端的&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onServiceConnected()&lt;/code&gt;&amp;nbsp;方法，并传递给它一个IBinder对象，它是由服务所管理的Stub的子类的代理。通过这个代理，客户端可以对远程服务进行调用。&lt;/span&gt;&lt;/li&gt;  &lt;/ul&gt;&lt;h3 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; "&gt;线程安全方法&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  在一些情况下，你所实现的方法有可能会被多于一个的线程所调用，所以它们必须被写成线程安全的。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;对于我们上一节所讨论的RPC机制中的可以被远程调用的方法来说，这是必须首先考虑的。如果针对一个IBinder对象中实现的方法的调用源自这个IBinder对象所在的进程时，这个方法将会在调用者的线程中执行。然而，如果这个调用源自其它的进程，则这个方法将会在一个线程池中选出的线程中运行，这个线程池由Android加以管理，并与IBinder存在于同一进程内；这个方法不会在进程的主线程内执行。反过来说，一个服务的&amp;nbsp;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onBind()&lt;/code&gt;&amp;nbsp;方法应为服务进程的主线程所调用，而实现了由&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onBind()&lt;/code&gt;&amp;nbsp;返回的对象（比如说，一个实现了RPC方法的Stub的子类）的方法将为池中的线程所调用。因为服务可以拥有多于一个的客户端，而同一时间，也会有多个池中的线程调用同一个IBinder方法。因此IBinder方法必须实现为线程安全的。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;类似的，一个内容提供者能接受源自其它进程的请求数据。尽管ContentResolver和ContentProvider类隐藏了交互沟通过程的管理细节，ContentProvider会由&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html#query%28android.net.Uri,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;query()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;，&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html#insert%28android.net.Uri,%20android.content.ContentValues%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;insert()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;，&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html#delete%28android.net.Uri,%20java.lang.String,%20java.lang.String%5B%5D%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;delete()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;，&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html#update%28android.net.Uri,%20android.content.ContentValues,%20java.lang.String,%20java.lang.String%5B%5D%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;update()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;和&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/ContentProvider.html#getType%28android.net.Uri%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;getType()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法来相应这些请求，而这些方法也都是由那个内容提供者的进程中所包涵的线程池提供的，而不是进程的主线程本身。所以这些有可能在同一时间被很多线程调用的方法也必须被实现为线程安全的。&lt;/p&gt;  &lt;h2 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; "&gt;组件生命周期&lt;/h2&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  应用程序组件有其生命周期──由Android初始化它们以相应intent直到这个实例被摧毁。在此之间，它们有时是激活的有时则相反。或者，如果它是一个activity，则是可为用户所见或者不能。这一节讨论了activity、服务以及广播接收器的生命周期，包括它们在生命周期中的状态、在状态之间&lt;em style="font-style: italic; "&gt;转变&lt;/em&gt;时通知你的方法、以及当这些进程被关闭或实例被摧毁时，这些状态产生的效果。&lt;/p&gt;  &lt;h3 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; "&gt;Activity生命周期&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  一个activity主要有三个状态：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;  当在屏幕前台时（位于当前任务堆栈的顶部），它是&lt;em style="font-style: italic; "&gt;活跃&lt;/em&gt;或&lt;em style="font-style: italic; "&gt;运行&lt;/em&gt;的状态。它就是相应用户操作的activity。&lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;当它失去焦点但仍然对用户可见时，它处于&lt;em style="font-style: italic; "&gt;暂停&lt;/em&gt;状态。即是：在它之上有另外一个activity。这个activity也许是透明的，或者未能完全遮蔽全屏，所以被暂停的activity仍对用户可见。&lt;em style="font-style: italic; "&gt;暂停的activity仍然是存活状态&lt;/em&gt;（它保留着所有的状态和成员信息并连接至窗口管理器），但当系统处于极低内存的情况下，仍然可以杀死这个activity。&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  如果它完全被另一个activity覆盖是，它处于&lt;em style="font-style: italic; "&gt;停止&lt;/em&gt;状态。它仍然保留所有的状态和成员信息。然而它不在为用户可见，所以它的窗口将被隐藏，如果其它地方需要内存，则系统经常会杀死这个activity。&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  如果一个activity处于暂停或停止状态，系统可以通过要求它结束（调用它的&amp;nbsp;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#finish%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;finish()&lt;/span&gt;&lt;/a&gt;&amp;nbsp;方法）或直接杀死它的进程来将它驱出内存。当它再次为用户可见的时候，它只能完全重新启动并恢复至以前的状态。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;当一个activity从这个状态转变到另一个状态时，它被以下列protected方法所通知：&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;  &lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onCreate(Bundle&amp;nbsp;&lt;em style="font-style: italic; "&gt;savedInstanceState&lt;/em&gt;)&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onStart()&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onRestart()&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onResume()&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onPause()&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onStop()&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onDestroy()&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  你可以重载所有这些方法以在状态改变时进行合适的工作。所有的activity都必须实现&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onCreate()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;用以当对象第一次实例化时进行初始化设置。很多activity会实现&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onPause()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;以提交数据变化或准备停止与用户的交互。&lt;/p&gt;  &lt;h2 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; "&gt;调用父类&lt;/h2&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  所有activity生命周期方法的实现都必须先调用其父类的版本。比如说：&lt;/p&gt;&lt;div class="sidebox-wrapper"&gt;&lt;div class="sidebox-inner"&gt;&lt;pre&gt;protected void onPause() {&lt;br&gt;    super.onPause();&lt;br&gt;    . . .&lt;br&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  总得来说，这七个方法定义了一个activity完整的生命周期。实现这些方法可以帮助你监察三个嵌套的生命周期循环：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;一个activity&amp;nbsp;&lt;strong&gt;完整的生命周期&lt;/strong&gt;&amp;nbsp;自第一次调用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="text-decoration: underline; "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onCreate()&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;开始，直至调用&lt;code style="font-style: normal; "&gt;&lt;a style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="text-decoration: underline; "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onDestroy()&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;为止。activity在&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()&lt;/code&gt;中设置所有&amp;ldquo;全局&amp;rdquo;状态以完成初始化，而在&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;中释放所有系统资源。比如说，如果activity有一个线程在后台运行以从网络上下载数据，它会以&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()创建那个线程，而以&lt;/code&gt;&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;销毁那个线程。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  一个activity的&amp;nbsp;&lt;strong&gt;可视生命周期&lt;/strong&gt;自&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; text-decoration: underline; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;调用开始直到相应的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; text-decoration: underline; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStop()&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;调用。在此期间，用户可以在屏幕上看到此activity，尽管它也许并不是位于前台或者正在与用户做交互。在这两个方法中，你可以管控用来向用户显示这个activity的资源。比如说，你可以在&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;&amp;nbsp;中注册一个&lt;/span&gt;&lt;a style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; text-decoration: underline; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;BroadcastReceiver&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&amp;nbsp;来监控会影响到你UI的改变，而在&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;&amp;nbsp;中来取消注册，这时用户是无法看到你的程序显示的内容的。&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;&amp;nbsp;和&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;&amp;nbsp;方法可以随着应用程序是否为用户可见而被多次调用。&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  一个activity的&amp;nbsp;&lt;strong&gt;前台生命周期&lt;/strong&gt;&amp;nbsp;自&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; text-decoration: underline; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onResume()&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;调用起，至相应的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; text-decoration: underline; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onPause()&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;调用为止。在此期间，activity位于前台最上面并与用户进行交互。activity会经常在暂停和恢复之间进行状态转换──比如说当设备转入休眠状态或有新的activity启动时，将调用&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;&amp;nbsp;方法。当activity获得结果或者接收到新的intent的时候会调用&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;&amp;nbsp;方法。因此，在这两个方法中的代码应当是轻量级的。&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;下图展示了上述循环过程以及activity在这个过程之中历经的状态改变。着色的椭圆是activity可以经历的主要状态。矩形框代表了当activity在状态间发生改变的时候，你进行操作所要实现的回调方法。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;&lt;img src="http://developer.android.com/images/activity_lifecycle.png" alt="State diagram for an Android activity lifecycle." style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;下表详细描述了这些方法，并在activity的整个生命周期中定位了它们。&lt;/p&gt;&lt;table border="2" frame="hsides" rules="rows" style="width: 789px; "&gt;  &lt;colgroup align="left" span="3"&gt;&lt;/colgroup&gt;&lt;colgroup align="left" span="1"&gt;&lt;/colgroup&gt;&lt;colgroup align="center" span="1"&gt;&lt;/colgroup&gt;&lt;colgroup align="center" span="1"&gt;&lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt;&lt;th colspan="3"&gt;方法&lt;/th&gt;&lt;th&gt;描述&lt;/th&gt;  &lt;th&gt;可被杀死&lt;/th&gt;&lt;th&gt;下一个&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td colspan="3" align="left"&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onCreate()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;在activity第一次被创建的时候调用。这里是你做所有初始化设置的地方──创建视图、绑定数据至列表等。如果曾经有状态记录（参阅后述&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#actstate" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); "&gt;Saving Activity State&lt;/span&gt;&lt;/a&gt;。），则调用此方法时会传入一个包含着此activity以前状态的包对象做为参数。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  总继之以&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;。&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td align="center"&gt;否&lt;/td&gt;&lt;td align="center"&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;span style="font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td rowspan="5" style="border-right-width: medium; border-right-style: none; border-right-color: initial; border-left-width: medium; border-left-style: none; border-left-color: initial; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td colspan="2" align="left"&gt;  &lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onRestart%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onRestart()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;在activity停止后，在再次启动之前被调用。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;总继之以&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;。&lt;/span&gt;&lt;/p&gt;  &lt;/td&gt;&lt;td align="center"&gt;否&lt;/td&gt;&lt;td align="center"&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;span style="font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan="2" align="left"&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;当activity正要变得为用户所见时被调用。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;当activity转向前台时继以&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;，在activity变为隐藏时继以&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;。&lt;/span&gt;&lt;/p&gt;  &lt;/td&gt;&lt;td align="center"&gt;否&lt;/td&gt;&lt;td align="center"&gt;&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;&amp;nbsp;&lt;br&gt;or&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;&lt;/span&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td rowspan="2" style="border-left-width: medium; border-left-style: none; border-left-color: initial; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td align="left"&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onResume%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onResume()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;在activity开始与用户进行交互之前被调用。此时activity位于堆栈顶部，并接受用户输入。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;继之以&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;。&lt;/span&gt;&lt;/p&gt;  &lt;/td&gt;&lt;td align="center"&gt;否&lt;/td&gt;&lt;td align="center"&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;span style="font-family: Verdana; "&gt;onPause()&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="left"&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onPause()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;当系统将要启动另一个activity时调用。此方法主要用来将未保存的变化进行持久化，停止类似动画这样耗费CPU的动作等。这一切动作应该在短时间内完成，因为下一个activity必须等到此方法返回后才会继续。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当activity重新回到前台是继以&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;。当activity变为用户不可见时继以&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;。&lt;/span&gt;&lt;/p&gt;  &lt;/td&gt;&lt;td align="center"&gt;&lt;strong style="color: rgb(128, 0, 0); "&gt;是&lt;/strong&gt;&lt;/td&gt;&lt;td align="center"&gt;&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;&amp;nbsp;&lt;br&gt;or&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan="2" align="left"&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStop()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;当activity不再为用户可见时调用此方法。这可能发生在它被销毁或者另一个activity（可能是现存的或者是新的）回到运行状态并覆盖了它。&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  如果activity再次回到前台跟用户交互则继以&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onRestart()&lt;/code&gt;，如果关闭activity则继以&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;。&lt;/span&gt;&lt;/p&gt;  &lt;/td&gt;&lt;td align="center"&gt;&lt;strong style="color: rgb(128, 0, 0); "&gt;是&lt;/strong&gt;&lt;/td&gt;&lt;td align="center"&gt;&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onRestart()&lt;/code&gt;&amp;nbsp;&lt;br&gt;or&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan="3" align="left"&gt;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onDestroy()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&lt;/td&gt;  &lt;td&gt;在activity销毁前调用。这是activity接收的最后一个调用。这可能发生在activity结束（调用了它的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#finish%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;finish()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法）或者因为系统需要空间所以临时的销毁了此acitivity的实例时。你可以用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#isFinishing%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;isFinishing()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法来区分这两种情况。&lt;/td&gt;  &lt;td align="center"&gt;&lt;strong style="color: rgb(128, 0, 0); "&gt;是&lt;/strong&gt;&lt;/td&gt;&lt;td align="center"&gt;&lt;em style="font-style: italic; "&gt;nothing&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  请注意上表中&lt;strong&gt;可被杀死&lt;/strong&gt;一列。它标示了&lt;em style="font-style: italic; "&gt;在方法返回后，还没执行activity的其余代码的任意时间&lt;/em&gt;里，系统是否可以杀死包含此activity的进程。三个方法（&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;、&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;&lt;code style="font-style: normal; "&gt;和&lt;/code&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;）被标记为&amp;ldquo;是&amp;rdquo;。&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;是三个中的第一个，它也是唯一一个在进程被杀死之前必然会调用的方法──&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;&amp;nbsp;和&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;&amp;nbsp;有可能不被执行。因此你应该用&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;&amp;nbsp;来将所有持久性数据（比如用户的编辑结果）写入存储之中。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;在&lt;strong&gt;可被杀死&lt;/strong&gt;一列中标记为&amp;ldquo;否&amp;rdquo;的方法在它们被调用时将保护activity所在的进程不会被杀死。所以只有在&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()方法返回后到&lt;/code&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;&amp;nbsp;方法被调用时，一个activity才处于可被杀死的状态。在&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()再次被调用并返回之前，它不会被系统杀死。&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如后面一节&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#proclife" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;进程和生命周期&lt;/span&gt;&lt;/a&gt;所述，即使是在这里技术上没有被定义为&amp;ldquo;可杀死&amp;rdquo;的activity仍然有可能被系统杀死──但这仅会发生在实在没有其它方法的极端情况之下。&lt;/p&gt;  &lt;h4 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; "&gt;保存activity状态&lt;/h4&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当系统而不是用户自己出于回收内存的考虑，关闭了一个activity之后。用户会期望当他再次回到那个activity的时候，它仍保持着上次离开时的样子。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  为了获取activity被杀死前的状态，你应该为activity实现&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onSaveInstanceState()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法。Android在activity有可能被销毁之前（即&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;&amp;nbsp;调用之前）会调用此方法。它会将一个以名称-值对方式记录了activity动态状态的&lt;/span&gt;&lt;a href="http://developer.android.com/reference/android/os/Bundle.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Bundle&lt;/span&gt;&lt;/a&gt;&amp;nbsp;对象传递给该方法。当activity再次启动时，这个Bundle会传递给&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()&lt;/code&gt;方法和随着&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;方法调用的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;onRestoreInstanceState()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;，所以它们两个都可以恢复捕获的状态。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;与&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;或先前讨论的其它方法不同，&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onSaveInstanceState()&lt;/code&gt;&amp;nbsp;和&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onRestoreInstanceState()&lt;/code&gt;&amp;nbsp;并不是生命周期方法。它们并不是总会被调用。比如说，Android会在activity易于被系统销毁之前调用&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onSaveInstanceState()&lt;/code&gt;，但用户动作（比如按下了BACK键）造成的销毁则不调用。在这种情况下，用户没打算再次回到这个activity，所以没有保存状态的必要。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;因为&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onSaveInstanceState()&lt;/code&gt;不是总被调用，所以你应该只用它来为activity保存一些临时的状态，而不能用来保存持久性数据。而是应该用&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;来达到这个目的。&lt;/span&gt;&lt;/p&gt;  &lt;h4 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; "&gt;协调activity&lt;/h4&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  当一个activity启动了另外一个的时候，它们都会经历生命周期变化。一个会暂停乃至停止，而另一个则启动。这种情况下，你可能需要协调好这些activity：&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  生命周期回调顺序是已经定义好的，尤其是在两个activity在同一个进程内的情况下：&lt;/p&gt;&lt;ol style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;调用当前activity的&amp;nbsp;&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onPause()&lt;/code&gt;&amp;nbsp;方法。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;接着，顺序调用新启动activity的&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()&lt;/code&gt;、&amp;nbsp;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;和&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onResume()&lt;/code&gt;方法。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;然后，如果启动的activity不再于屏幕上可见，则调用它的&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;方法。&lt;/span&gt;&lt;/li&gt;  &lt;/ol&gt;&lt;h3 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; "&gt;服务生命周期&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  服务以两种方式使用：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;  它可以启动并运行，直至有人停止了它或它自己停止。在这种方式下，它以调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.startService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;启动，而以调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.stopService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;结束。它可以调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#stopSelf%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Service.stopSelf()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;或&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#stopSelfResult%28int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Service.stopSelfResult()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;来自己停止。不论调用了多少次&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService()&lt;/code&gt;方法，你只需要调用一次&lt;code class="Code prettyprint" style="font-style: normal; "&gt;stopService()&lt;/code&gt;来停止服务。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;它可以通过自己定义并暴露出来的接口进行程序操作。客户端建立一个到服务对象的连接，并通过那个连接来调用服务。连接以调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.bindService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法建立，以调用&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#unbindService%28android.content.ServiceConnection%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;Context.unbindService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;关闭。多个客户端可以绑定至同一个服务。如果服务此时还没有加载，&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;bindService()&lt;/code&gt;会先加载它。&lt;/span&gt;&lt;/li&gt;  &lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;这两种模式并不是完全分离的。你可以绑定至一个用&amp;nbsp;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService()&lt;/code&gt;启动的服务。比如说，一个后台音乐播放服务可以调用&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService()&lt;/code&gt;并传递给它一个包含欲播放的音乐列表的Intent对象来启动。不久，当用户想要对播放器进行控制或者查看当前播放曲目的详情时，会启用一个activity，调用&lt;code class="Code prettyprint" style="font-style: normal; "&gt;bindService()&lt;/code&gt;连接到服务来完成操作。在这种情况下，直到绑定连接关闭&lt;code class="Code prettyprint" style="font-style: normal; "&gt;stopService()&lt;/code&gt;&amp;nbsp;才会真正停止一个服务。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;与activity一样，服务也有一系列你可以实现以用于监控其状态变化的生命周期方法。但相对于activity要少一些，只有三个，而且，它们是public属性，并非protected：&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onCreate()&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onStart(Intent&amp;nbsp;&lt;em style="font-style: italic; "&gt;intent&lt;/em&gt;)&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onDestroy()&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;倚仗实现这些方法，你监控服务的两个嵌套的生命周期循环：&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;服务的&lt;strong&gt;完整生命周期&lt;/strong&gt;始于调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onCreate%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onCreate()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;而终于&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onDestroy%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onDestroy()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法返回。如同activity一样，服务在&lt;span style="font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()&lt;/code&gt;里面进行它自己的初始化，而在&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;里面释放所有资源。比如说，一个音乐回放服务可以在&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()&lt;/code&gt;中创建播放音乐的线程， 而在&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;中停止这个线程。&lt;/span&gt;&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: disc; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  服务的&lt;strong&gt;活跃生命周期&lt;/strong&gt;始于调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;。这个方法用于处理传递给&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService()&lt;/code&gt;的Intent对象。音乐服务会打开Intent来探明将要播放哪首音乐，并开始播放。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;服务停止时没有相应的回调方法──不存在&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStop()&lt;/code&gt;方法。&lt;/span&gt;&lt;/p&gt;  &lt;/li&gt;&lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onCreate()&lt;/code&gt;和&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onDestroy()&lt;/code&gt;方法在所有服务中都会被调用，不论它们是由&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Context.startService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;还是由&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Context.bindService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;所启动的。而&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onStart()&lt;/code&gt;仅会被&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService()&lt;/code&gt;所启用的服务调用。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;如果一个服务允许别的进程绑定，则它还会有以下额外的回调方法以供实现：&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;  &lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;IBinder onBind(Intent&amp;nbsp;&lt;em style="font-style: italic; "&gt;intent&lt;/em&gt;)&lt;/code&gt;&amp;nbsp;&lt;br&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;boolean onUnbind(Intent&amp;nbsp;&lt;em style="font-style: italic; "&gt;intent&lt;/em&gt;)&lt;/code&gt;&amp;nbsp;&lt;br&gt;  &lt;code class="Code prettyprint" style="font-style: normal; "&gt;void onRebind(Intent&amp;nbsp;&lt;em style="font-style: italic; "&gt;intent&lt;/em&gt;)&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  传递给&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;bindService&lt;/code&gt;的Intent的对象也会传递给&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onBind%28android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;onBind()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;回调方法，而传递给&lt;code class="Code prettyprint" style="font-style: normal; "&gt;unbindService()&lt;/code&gt;的Intent对象同样传递给&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onUnbind%28android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;onUnbind()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;。如果服务允许绑定，&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onBind()&lt;/code&gt;将返回一个供客户端与服务进行交互的通讯渠道。如果有新的客户端连接至服务，则&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onUnbind()&lt;/code&gt;方法可以要求调用&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onRebind%28android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;onRebind()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;下图描绘了服务的回调方法。尽管图中对由&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService&lt;/code&gt;&amp;nbsp;和&lt;code class="Code prettyprint" style="font-style: normal; "&gt;startService&lt;/code&gt;方法启动的服务做了区分，但要记住，不论一个服务是怎么启动的，它都可能允许客户端的连接，所以任何服务都可以接受&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onBind()&lt;/code&gt;和&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onUnbind()&lt;/code&gt;调用。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;&lt;img src="http://developer.android.com/images/service_lifecycle.png" alt="State diagram for Service callbacks." style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "&gt;&lt;/p&gt;  &lt;h3 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; "&gt;广播接收器生命周期&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  广播接收器只有一个回调方法：&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 2em; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;&lt;span style="line-height: 21px; font-family: Verdana; "&gt;void onReceive(Context&amp;nbsp;&lt;em style="font-style: italic; "&gt;curContext&lt;/em&gt;, Intent&amp;nbsp;&lt;em style="font-style: italic; "&gt;broadcastMsg&lt;/em&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;当广播消息抵达接收器时，Android调用它的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onReceive()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法并将包含消息的Intent对象传递给它。广播接收器仅在它执行这个方法时处于活跃状态。当&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onReceive()&lt;/code&gt;返回后，它即为失活状态。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;拥有一个活跃状态的广播接收器的进程被保护起来而不会被杀死。但仅拥有失活状态组件的进程则会在其它进程需要它所占有的内存的时候随时被杀掉。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;这种方式引出了一个问题：如果响应一个广播信息需要很长的一段时间，我们一般会将其纳入一个衍生的线程中去完成，而不是在主线程内完成它，从而保证用户交互过程的流畅。如果&lt;span style="line-height: 21px; font-family: Verdana; "&gt;&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onReceive()&lt;/code&gt;衍生了一个线程并且返回，则包涵新线程在内的整个进程都被会判为失活状态（除非进程内的其它应用程序组件仍处于活跃状态），于是它就有可能被杀掉。这个问题的解决方法是令&lt;code class="Code prettyprint" style="font-style: normal; "&gt;onReceive()&lt;/code&gt;启动一个新服务，并用其完成任务，于是系统就会知道进程中仍然在处理着工作。&lt;/span&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;下一节中，我们会讨论更多进程易误杀的问题。&lt;/p&gt;&lt;h3 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; "&gt;  进程与生命周期&lt;/h3&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;Android系统会尽可能长的延续一个应用程序进程，但在内存过低的时候，仍然会不可避免需要移除旧的进程。为决定保留或移除一个进程，Android将每个进程都放入一个&amp;ldquo;重要性层次&amp;rdquo;中，依据则是它其中运行着的组件及其状态。重要性最低的进程首先被消灭，然后是较低的，依此类推。重要性共分五层，依据重要性列表如下：&lt;/p&gt;  &lt;ol style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;  &lt;strong&gt;前台进程&lt;/strong&gt;是用户操作所必须的。当满足如下任一条件时，进程被认为是处于前台的：&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;它运行着正在与用户交互的activity（Activity对象的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onResume%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onResume()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法已被调用）。&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  一个正在与用户交互的activity使用着它提供的一个服务。&lt;/p&gt;&lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;它包含着一个正在执行生命周期回调方法（&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onCreate%28%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onCreate()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;、&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStart()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;或&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Service.html#onDestroy%28%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onDestroy()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;）的&lt;a href="http://developer.android.com/reference/android/app/Service.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;Service&lt;/span&gt;&lt;/a&gt;对象。&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  它包含着一个正在执行&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onReceive()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法的&lt;a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;BroadcastReceiver&lt;/span&gt;&lt;/a&gt;对象。&lt;/p&gt;  &lt;/li&gt;&lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;任一时间下，仅有少数进程会处于前台，仅当内存实在无法供给它们维持同时运行时才会被杀死。一般来说，在这种情况下，设备已然处于使用虚拟内存的状态，必须要杀死一些前台进程以用户界面保持响应。&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;可视进程&lt;/strong&gt;没有前台组件，但仍可被用户在屏幕上所见。当满足如下任一条件时，进程被认为是可视的：、&lt;/p&gt;&lt;ul style="margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; "&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;它包含着一个不在前台，但仍然为用户可见的activity（它的&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="color: rgb(46, 106, 177); font-family: Verdana; "&gt;onPause()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;方法被调用）。这种情况可能出现在以下情况：比如说，前台activity是一个对话框，而之前的activity位于其下并可以看到。&lt;/li&gt;  &lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  它包含了一个绑定至一个可视的activity的服务。&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;可视进程依然被视为是很重要的，非到不杀死它们便无法维持前台进程运行时，才会被杀死。&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;服务进程&lt;/strong&gt;是由&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;startService()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法启动的服务，它不会变成上述两类。尽管服务进程不会直接为用户所见，但它们一般都在做着用户所关心的事情（比如在后台播放mp3或者从网上下载东西）。所以系统会尽量维持它们的运行，除非系统内存不足以维持前台进程和可视进程的运行需要。&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;背景进程&lt;/strong&gt;包含目前不为用户所见的activity（Activity对象的&amp;nbsp;&lt;code style="font-style: normal; "&gt;&lt;a href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29" style="text-decoration: underline; color: rgb(213, 145, 57); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 6px; "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); font-family: Verdana; "&gt;onStop()&lt;/span&gt;&lt;/a&gt;&lt;/code&gt;&amp;nbsp;方法已被调用）。这些进程与用户体验没有直接的联系，可以在任意时间被杀死以回收内存供前台进程、可视进程以及服务进程使用。一般来说，会有很多背景进程运行，所以它们一般存放于一个LRU（最后使用）列表中以确保最后被用户使用的activity最后被杀死。如果一个activity正确的实现了生命周期方法，并捕获了正确的状态，则杀死它的进程对用户体验不会有任何不良影响。&lt;/p&gt;  &lt;/li&gt;&lt;li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; list-style-type: decimal; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  &lt;strong&gt;空进程&lt;/strong&gt;不包含任何活动应用程序组件。这种进程存在的唯一原因是做为缓存以改善组件再次于其中运行时的启动时间。系统经常会杀死这种进程以保持进程缓存和系统内核缓存之间的平衡。&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  Android会依据进程中当前活跃组件的重要程度来尽可能高的估量一个进程的级别。比如说，如果一个进程中同时有一个服务和一个可视的activity，则进程会被判定为可视进程，而不是服务进程。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  此外，一个进程的级别可能会由于其它进程依赖于它而升高。一个为其它进程提供服务的进程级别永远高于使用它服务的进程。比如说，如果A进程中的内容提供者为进程B中的客户端提供服务，或进程A中的服务为进程B中的组件所绑定，则A进程最低也会被视为与进程B拥有同样的重要性。&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;  因为运行着一个服务的进程重要级别总高于一个背景activity。所以一个activity以启动一个服务的方式启动一个长时间运行过程比简单的衍生一个线程来进行处理要好。尤其是当处理过程比activity本身存在时间要长的情况之下。我们以背景音乐播放和上传一个相机拍摄的图片至网站上为例。使用服务则不论activity发生何事，都至少可以保证操作拥有&amp;ldquo;服务进程&amp;rdquo;的权限。如上一节&lt;a href="http://developer.android.com/guide/topics/fundamentals.html#broadlife" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;span style="line-height: 21px; color: rgb(46, 106, 177); "&gt;广播接收器生命周期&lt;/span&gt;&lt;/a&gt;&amp;nbsp;所提到的，这也正是广播接收器使用服务，而不是使用线程来处理耗时任务的原因。&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;转自译言：&lt;a href="http://www.yeeyan.com/articles/view/37503/34036" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;http://www.yeeyan.com/articles/view/37503/34036&lt;/a&gt;&lt;/p&gt;  &lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; "&gt;&lt;a href="http://www.yeeyan.com/articles/view/37503/34036" style="text-decoration: none; color: rgb(51, 102, 153); "&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-size: 12px; "&gt;发表于 @ 2009年08月17日　22:03:00&amp;nbsp;|&amp;nbsp;&lt;a id="a_comment" href="#FeedBack" title="评论" style="text-decoration: underline; color: rgb(51, 102, 153); margin-top: 0px; margin-right: 3px; margin-bottom: 0px; margin-left: 3px; "&gt;评论(&amp;nbsp;&lt;span id="FeedbackCount_4457015" style="line-height: 18px; "&gt;1&lt;/span&gt;&amp;nbsp;)&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;  &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-5574025883390924077?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/5574025883390924077/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5574025883390924077'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5574025883390924077'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android.html' title='android应用程序基础(包括活动、服务、广播、内容提供等）'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-2920711296477228291</id><published>2010-07-12T20:52:00.001-07:00</published><updated>2010-07-12T20:52:30.687-07:00</updated><title type='text'>Android Content Provider</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, 宋体, sans-serif; font-size: 12px; color: rgb(50, 62, 50); "&gt;&lt;div class="articalTitle" style="clear: both; line-height: 20px; padding-bottom: 10px; "&gt;  &lt;h2 class="titName SG_txta" id="t_3f7f41d40100cnax" 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-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; color: rgb(46, 105, 72); font-size: 18px; font-family: 微软雅黑, 黑体; font-weight: 300; display: inline; "&gt;  Android基础&amp;nbsp;:&amp;nbsp;&amp;nbsp;Android&amp;nbsp;Content&amp;nbsp;Provider[转]&lt;/h2&gt;&lt;span class="img2" style="white-space: nowrap; padding-top: 4px; "&gt;&lt;/span&gt;&lt;span class="time SG_txtc" style="color: rgb(99, 113, 96); white-space: nowrap; font-family: Arial; font-size: 10px; margin-left: 5px; margin-right: 13px; "&gt;(2009-04-12 15:50:21)&lt;/span&gt;&lt;div class="turnBoxzz" style="float: right; "&gt;  &lt;a href="" id="quote_set_sign2" class="SG_aBtn SG_aBtn_ico SG_turn" style="text-decoration: none; color: rgb(50, 62, 50); cursor: pointer; font-size: 12px !important; padding-top: 4px; padding-right: 0px; padding-bottom: 6px; padding-left: 3px; overflow-x: hidden; overflow-y: hidden; white-space: nowrap; margin-right: 6px; background-image: url(http://simg.sinajs.cn/blog7newtpl/image/11/11_23/images/sg_btn.gif); position: relative; background-position: 0px 0px; background-repeat: no-repeat no-repeat; "&gt;&lt;cite style="font-style: normal; font-size: 12px !important; line-height: 23px; padding-top: 4px; padding-right: 12px; padding-bottom: 6px; padding-left: 26px; height: 23px; min-width: 1px; overflow-x: visible; white-space: nowrap; text-align: left; background-image: url(http://simg.sinajs.cn/blog7newtpl/image/11/11_23/images/sg_btn.gif); background-position: 100% -28px; background-repeat: no-repeat no-repeat; "&gt;&lt;img class="SG_icon SG_icon111" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" align="absmiddle" 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-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; background-image: url(http://simg.sinajs.cn/blog7style/images/common/sg_icon.png); position: absolute; left: 9px; top: 4px; background-position: 495px -60px; "&gt;转载&lt;/cite&gt;&lt;/a&gt;&lt;/div&gt;  &lt;/div&gt;&lt;div class="articalTag" id="sina_keyword_ad_area" style="width: 690px; clear: both; word-break: break-all; line-height: 20px; "&gt;&lt;table 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; "&gt;  &lt;tbody&gt;&lt;tr&gt;&lt;td class="blog_tag" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: 宋体; vertical-align: top; "&gt;  &lt;span class="SG_txtb" style="color: rgb(99, 113, 96); font-family: 宋体; "&gt;标签：&lt;/span&gt;&lt;h3 style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; font-size: 12px; font-weight: normal; "&gt;  &lt;a href="http://uni.sina.com.cn/c.php?t=blog&amp;amp;k=it&amp;amp;ts=bpost&amp;amp;stype=tag" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; white-space: nowrap; "&gt;it&lt;/a&gt;&lt;/h3&gt;&lt;h3 style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; font-size: 12px; font-weight: normal; "&gt;  &lt;a href="http://uni.sina.com.cn/c.php?t=blog&amp;amp;k=android&amp;amp;ts=bpost&amp;amp;stype=tag" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; white-space: nowrap; "&gt;android&lt;/a&gt;&lt;/h3&gt;&lt;h3 style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; font-size: 12px; font-weight: normal; "&gt;  &lt;a href="http://uni.sina.com.cn/c.php?t=blog&amp;amp;k=java&amp;amp;ts=bpost&amp;amp;stype=tag" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; white-space: nowrap; "&gt;java&lt;/a&gt;&lt;/h3&gt;&lt;/td&gt;&lt;td class="blog_class" 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; font-size: 12px; font-family: 宋体; vertical-align: top; width: 220px; white-space: nowrap; "&gt;  &lt;span class="SG_txtb" style="color: rgb(99, 113, 96); font-family: 宋体; "&gt;分类：&lt;/span&gt;&lt;a href="http://blog.sina.com.cn/s/articlelist_1065304532_4_1.html" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; "&gt;Java,Android,Lucene&lt;/a&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div class="articalContent" id="sina_keyword_ad_area2" style="clear: both; padding-top: 18px; font-size: 14px; line-height: 21px; padding-bottom: 30px; word-wrap: normal; word-break: normal; overflow-x: hidden; overflow-y: hidden; font-family: simsun; "&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Android应用程序可以使用文件或SqlLite数据库来存储数据。Content Provider提供了一种多应用间数据共享的方式，比如：联系人信息可以被多个应用程序访问。Content Provider是个实现了一组用于提供其他应用程序存取数据的标准方法的类。&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  应用程序可以在Content Provider中执行如下操作:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;br&gt;&lt;strong style="font-weight: bold; "&gt;查询数据&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;修改数据&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;添加数据&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;删除数据&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;br&gt;&lt;strong style="font-weight: bold; "&gt;标准的Content Provider:&lt;/strong&gt;&lt;br&gt;Android提供了一些已经在系统中实现的标准Content Provider，比如联系人信息，图片库等等，你可以用这些Content Provider来访问设备上存储的联系人信息，图片等等。&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  查询记录:&lt;br&gt;在Content Provider中使用的查询字符串有别于标准的SQL查询。很多诸如select, add, delete, modify等操作我们都使用一种特殊的URI来进行，这种URI由3个部分组成， &amp;ldquo;content://&amp;rdquo;, 代表数据的路径，和一个可选的标识数据的ID。以下是一些示例URI:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;br&gt;&lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;content://media/internal/images&amp;nbsp;&amp;nbsp;这个URI将返回设备上存储的所有图片&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;content://contacts/people/&amp;nbsp;&amp;nbsp;这个URI将返回设备上的所有联系人信息&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;content://contacts/people/45 这个URI返回单个结果（联系人信息中ID为45的联系人记录）&lt;br&gt;  &lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  尽管这种查询字符串格式很常见，但是它看起来还是有点令人迷惑。为此，Android提供一系列的帮助类（在android.provider包下），里面包含了很多以类变量形式给出的查询字符串，这种方式更容易让我们理解一点，参见下例:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;MediaStore.Images.Media.INTERNAL_CONTENT_URI&lt;br&gt;Contacts.People.CONTENT_URI&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  因此，如上面content://contacts/people/45这个URI就可以写成如下形式：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  Uri person = ContentUris.withAppendedId(People.CONTENT_URI,&amp;nbsp;&amp;nbsp;45);&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  然后执行数据查询:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  Cursor cur = managedQuery(person, null, null, null);&lt;br&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  这个查询返回一个包含所有数据字段的游标，我们可以通过迭代这个游标来获取所有的数据：&lt;br&gt;&lt;br&gt;&lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;package com.wissen.testApp;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;public class ContentProviderDemo extends Activity {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onCreate(Bundle savedInstanceState) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;super.onCreate(savedInstanceState);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;setContentView(R.layout.main);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;displayRecords();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void displayRecords() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//该数组中包含了所有要返回的字段&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String columns[] = new String[] { People.NAME, People.NUMBER };&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri mContacts = People.CONTENT_URI;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Cursor cur = managedQuery(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mContacts,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;columns,&amp;nbsp;&amp;nbsp;// 要返回的数据字段&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;null,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// WHERE子句&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;null,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// WHERE 子句的参数&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;null&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Order-by子句&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;);&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (cur.moveToFirst()) {&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String name = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String phoneNo = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;do {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// 获取字段的值&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;name = cur.getString(cur.getColumnIndex(People.NAME));&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;phoneNo = cur.getString(cur.getColumnIndex(People.NUMBER));&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Toast.makeText(this, name + &amp;rdquo; &amp;rdquo; + phoneNo, Toast.LENGTH_LONG).show();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} while (cur.moveToNext());&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;}&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  上例示范了一个如何依次读取联系人信息表中的指定数据列name和number。&lt;br&gt;&lt;br&gt;&lt;strong style="font-weight: bold; "&gt;修改记录:&lt;/strong&gt;&lt;br&gt;我们可以使用ContentResolver.update()方法来修改数据，我们来写一个修改数据的方法:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;private void updateRecord(int recNo, String name) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, recNo);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ContentValues values = new ContentValues();&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.put(People.NAME, name);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;getContentResolver().update(uri, values, null, null);&lt;br&gt;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  现在你可以调用上面的方法来更新指定记录：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  updateRecord(10, &amp;rdquo;XYZ&amp;rdquo;);&amp;nbsp;&amp;nbsp;&amp;nbsp;//更改第10条记录的name字段值为&amp;ldquo;XYZ&amp;rdquo;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;添加记录:&lt;br&gt;&lt;/strong&gt;要增加记录，我们可以调用ContentResolver.insert()方法，该方法接受一个要增加的记录的目标URI，以及一个包含了新记录值的Map对象，调用后的返回值是新记录的URI，包含记录号。&lt;br&gt;上面的例子中我们都是基于联系人信息簿这个标准的Content Provider，现在我们继续来创建一个insertRecord() 方法以对联系人信息簿中进行数据的添加：&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;private void insertRecords(String name, String phoneNo) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ContentValues values = new ContentValues();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.put(People.NAME, name);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri uri = getContentResolver().insert(People.CONTENT_URI, values);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Log.d(&amp;rdquo;ANDROID&amp;rdquo;, uri.toString());&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.clear();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.put(People.NUMBER, phoneNo);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;getContentResolver().insert(numberUri, values);&lt;br&gt;}&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  这样我们就可以调用insertRecords(name, phoneNo)的方式来向联系人信息簿中添加联系人姓名和电话号码。&lt;br&gt;&lt;br&gt;&lt;strong style="font-weight: bold; "&gt;删除记录:&lt;/strong&gt;&lt;br&gt;Content Provider中的getContextResolver.delete()方法可以用来删除记录，下面的记录用来删除设备上所有的联系人信息：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  private void deleteRecords() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri uri = People.CONTENT_URI;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;getContentResolver().delete(uri, null, null);&lt;br&gt;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  你也可以指定WHERE条件语句来删除特定的记录：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  getContentResolver().delete(uri, &amp;ldquo;NAME=&amp;rdquo; + &amp;ldquo;&amp;lsquo;XYZ XYZ&amp;rsquo;&amp;rdquo;, null);&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  这将会删除name为&amp;lsquo;XYZ XYZ&amp;rsquo;的记录。&lt;br&gt;&lt;br&gt;&lt;strong style="font-weight: bold; "&gt;创建Content Provider:&lt;/strong&gt;&lt;br&gt;至此我们已经知道如何使用Content Provider了，现在让我们来看下如何自己创建一个Content Provider。&lt;br&gt;&lt;br&gt;要创建我们自己的Content Provider的话，我们需要遵循以下几步：&lt;br&gt;1. 创建一个继承了ContentProvider父类的类&lt;br&gt;  &lt;br&gt;2. 定义一个名为CONTENT_URI，并且是public static final的Uri类型的类变量，你必须为其指定一个唯一的字符串值，最好的方案是以类的全名称， 如:&lt;br&gt;public static final Uri CONTENT_URI = Uri.parse( &amp;ldquo;content://com.google.android.MyContentProvider&amp;rdquo;);&lt;br&gt;&lt;br&gt;3. 创建你的数据存储系统。大多数Content Provider使用Android文件系统或SQLite数据库来保持数据，但是你也可以以任何你想要的方式来存储。&lt;br&gt;  &lt;br&gt;4. 定义你要返回给客户端的数据列名。如果你正在使用Android数据库，则数据列的使用方式就和你以往所熟悉的其他数据库一样。但是，你必须为其定义一个叫_id的列，它用来表示每条记录的唯一性。&lt;br&gt;&lt;br&gt;5. 如果你要存储字节型数据，比如位图文件等，那保存该数据的数据列其实是一个表示实际保存文件的URI字符串，客户端通过它来读取对应的文件数据，处理这种数据类型的Content Provider需要实现一个名为_data的字段，_data字段列出了该文件在Android文件系统上的精确路径。这个字段不仅是供客户端使用，而且也可以供ContentResolver使用。客户端可以调用ContentResolver.openOutputStream()方法来处理该URI指向的文件资源，如果是ContentResolver本身的话，由于其持有的权限比客户端要高，所以它能直接访问该数据文件。&lt;br&gt;  &lt;br&gt;6. 声明public static String型的变量，用于指定要从游标处返回的数据列。&lt;br&gt;&lt;br&gt;7. 查询返回一个Cursor类型的对象。所有执行写操作的方法如insert(), update() 以及delete()都将被监听。我们可以通过使用ContentResover().notifyChange()方法来通知监听器关于数据更新的信息。&lt;br&gt;&lt;br&gt;8. 在AndroidMenifest.xml中使用&amp;lt;provider&amp;gt;标签来设置Content Provider。&lt;br&gt;  &lt;br&gt;9. 如果你要处理的数据类型是一种比较新的类型，你就必须先定义一个新的MIME类型，以供ContentProvider.geType(url)来返回。MIME类型有两种形式:一种是为指定的单个记录的，还有一种是为多条记录的。这里给出一种常用的格式：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  vnd.android.cursor.item/vnd.yourcompanyname.contenttype （单个记录的MIME类型）&lt;br&gt;比如, 一个请求列车信息的URI如content://com.example.transportationprovider/trains/122 可能就会返回typevnd.android.cursor.item/vnd.example.rail这样一个MIME类型。&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  vnd.android.cursor.dir/vnd.yourcompanyname.contenttype （多个记录的MIME类型）&lt;br&gt;比如, 一个请求所有列车信息的URI如content://com.example.transportationprovider/trains 可能就会返回vnd.android.cursor.dir/vnd.example.rail这样一个MIME 类型。&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  下列代码将创建一个Content Provider，它仅仅是存储用户名称并显示所有的用户名称（使用 SQLLite数据库存储这些数据）：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;package com.wissen.testApp;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;public class MyUsers {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static final String AUTHORITY&amp;nbsp;&amp;nbsp;= &amp;ldquo;com.wissen.MyContentProvider&amp;rdquo;;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// BaseColumn类中已经包含了 _id字段&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static final class User implements BaseColumns {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static final Uri CONTENT_URI&amp;nbsp;&amp;nbsp;= Uri.parse(&amp;rdquo;content://com.wissen.MyContentProvider&amp;rdquo;);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// 表数据列&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static final String&amp;nbsp;&amp;nbsp;USER_NAME&amp;nbsp;&amp;nbsp;= &amp;ldquo;USER_NAME&amp;rdquo;;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  上面的类中定义了Content Provider的CONTENT_URI，以及数据列。下面我们将定义基于上面的类来定义实际的Content Provider类：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;package com.wissen.testApp.android;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;public class MyContentProvider extends ContentProvider {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private SQLiteDatabase&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sqlDB;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private DatabaseHelper&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbHelper;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private static final String&amp;nbsp;&amp;nbsp;DATABASE_NAME&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= &amp;ldquo;Users.db&amp;rdquo;;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private static final int&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DATABASE_VERSION&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= 1;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private static final String TABLE_NAME&amp;nbsp;&amp;nbsp;&amp;nbsp;= &amp;ldquo;User&amp;rdquo;;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private static final String TAG = &amp;ldquo;MyContentProvider&amp;rdquo;;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private static class DatabaseHelper extends SQLiteOpenHelper {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DatabaseHelper(Context context) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;super(context, DATABASE_NAME, null, DATABASE_VERSION);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onCreate(SQLiteDatabase db) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//创建用于存储数据的表&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;db.execSQL(&amp;rdquo;Create table &amp;rdquo; + TABLE_NAME + &amp;ldquo;( _id INTEGER PRIMARY KEY AUTOINCREMENT, USER_NAME TEXT);&amp;rdquo;);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;db.execSQL(&amp;rdquo;DROP TABLE IF EXISTS &amp;rdquo; + TABLE_NAME);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;onCreate(db);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int delete(Uri uri, String s, String[] as) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return 0;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public String getType(Uri uri) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Uri insert(Uri uri, ContentValues contentvalues) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sqlDB = dbHelper.getWritableDatabase();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;long rowId = sqlDB.insert(TABLE_NAME, &amp;ldquo;&amp;rdquo;, contentvalues);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (rowId &amp;gt; 0) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri rowUri = ContentUris.appendId(MyUsers.User.CONTENT_URI.buildUpon(), rowId).build();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;getContext().getContentResolver().notifyChange(rowUri, null);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return rowUri;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new SQLException(&amp;rdquo;Failed to insert row into &amp;rdquo; + uri);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public boolean onCreate() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbHelper = new DatabaseHelper(getContext());&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return (dbHelper == null) ? false : true;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SQLiteQueryBuilder qb = new SQLiteQueryBuilder();&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SQLiteDatabase db = dbHelper.getReadableDatabase();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;qb.setTables(TABLE_NAME);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Cursor c = qb.query(db, projection, selection, null, null, null, sortOrder);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.setNotificationUri(getContext().getContentResolver(), uri);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return c;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;&lt;em style="font-style: italic; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return 0;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;}&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  一个名为MyContentProvider的Content Provider创建完成了，它用于从Sqlite数据库中添加和读取记录。&lt;br&gt;Content Provider的入口需要在AndroidManifest.xml中配置:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;lt;provider android:name=&amp;rdquo;MyContentProvider&amp;rdquo; android:authorities=&amp;rdquo;com.wissen.MyContentProvider&amp;rdquo; /&amp;gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  之后，让我们来使用这个定义好的Content Provider:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;package com.wissen.testApp;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;public class MyContentDemo extends Activity {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;protected void onCreate(Bundle savedInstanceState) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;super.onCreate(savedInstanceState);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;insertRecord(&amp;rdquo;MyUser&amp;rdquo;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;displayRecords();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void insertRecord(String userName) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ContentValues values = new ContentValues();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.put(MyUsers.User.USER_NAME, userName);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;getContentResolver().insert(MyUsers.User.CONTENT_URI, values);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;em style="font-style: italic; "&gt;&lt;strong style="font-weight: bold; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private void displayRecords() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String columns[] = new String[] { MyUsers.User._ID, MyUsers.User.USER_NAME };&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Uri myUri = MyUsers.User.CONTENT_URI;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Cursor cur = managedQuery(myUri, columns,null, null, null );&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (cur.moveToFirst()) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String id = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String userName = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;do {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;id = cur.getString(cur.getColumnIndex(MyUsers.User._ID));&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;userName = cur.getString(cur.getColumnIndex(MyUsers.User.USER_NAME));&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Toast.makeText(this, id + &amp;rdquo; &amp;rdquo; + userName, Toast.LENGTH_LONG).show();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} while (cur.moveToNext());&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;}&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  上面的类将先向数据库中添加一条用户数据，然后显示数据库中所有的用户数据。&lt;br&gt;&lt;br&gt;至此，我们已经明白如何来使用和创建Content Provider了。&lt;/p&gt;&lt;/div&gt;&lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-2920711296477228291?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/2920711296477228291/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-content-provider.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2920711296477228291'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/2920711296477228291'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-content-provider.html' title='Android Content Provider'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-4932178197854385432</id><published>2010-07-12T16:42:00.001-07:00</published><updated>2010-07-12T16:42:48.781-07:00</updated><title type='text'>Android基础 : Android Service</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, 宋体, sans-serif; font-size: 12px; color: rgb(50, 62, 50); "&gt;&lt;div class="articalTitle" style="clear: both; line-height: 20px; padding-bottom: 10px; "&gt;  &lt;h2 class="titName SG_txta" id="t_3f7f41d40100cnnm" 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-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; color: rgb(46, 105, 72); font-size: 18px; font-family: 微软雅黑, 黑体; font-weight: 300; display: inline; "&gt;  Android基础&amp;nbsp;:&amp;nbsp;Android&amp;nbsp;Service[转]&lt;/h2&gt;&lt;span class="img2" style="white-space: nowrap; padding-top: 4px; "&gt;&lt;/span&gt;&lt;span class="time SG_txtc" style="color: rgb(99, 113, 96); white-space: nowrap; font-family: Arial; font-size: 10px; margin-left: 5px; margin-right: 13px; "&gt;(2009-04-13 16:38:10)&lt;/span&gt;&lt;div class="turnBoxzz" style="float: right; "&gt;  &lt;a href="" id="quote_set_sign2" class="SG_aBtn SG_aBtn_ico SG_turn" style="text-decoration: none; color: rgb(50, 62, 50); cursor: pointer; font-size: 12px !important; padding-top: 4px; padding-right: 0px; padding-bottom: 6px; padding-left: 3px; overflow-x: hidden; overflow-y: hidden; white-space: nowrap; margin-right: 6px; background-image: url(http://simg.sinajs.cn/blog7newtpl/image/11/11_23/images/sg_btn.gif); position: relative; background-position: 0px 0px; background-repeat: no-repeat no-repeat; "&gt;&lt;cite style="font-style: normal; font-size: 12px !important; line-height: 23px; padding-top: 4px; padding-right: 12px; padding-bottom: 6px; padding-left: 26px; height: 23px; min-width: 1px; overflow-x: visible; white-space: nowrap; text-align: left; background-image: url(http://simg.sinajs.cn/blog7newtpl/image/11/11_23/images/sg_btn.gif); background-position: 100% -28px; background-repeat: no-repeat no-repeat; "&gt;&lt;img class="SG_icon SG_icon111" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" align="absmiddle" 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-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; background-image: url(http://simg.sinajs.cn/blog7style/images/common/sg_icon.png); position: absolute; left: 9px; top: 4px; background-position: 495px -60px; "&gt;转载&lt;/cite&gt;&lt;/a&gt;&lt;/div&gt;  &lt;/div&gt;&lt;div class="articalTag" id="sina_keyword_ad_area" style="width: 690px; clear: both; word-break: break-all; line-height: 20px; "&gt;&lt;table 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; "&gt;  &lt;tbody&gt;&lt;tr&gt;&lt;td class="blog_tag" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: 宋体; vertical-align: top; "&gt;  &lt;span class="SG_txtb" style="color: rgb(99, 113, 96); font-family: 宋体; "&gt;标签：&lt;/span&gt;&lt;h3 style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; font-size: 12px; font-weight: normal; "&gt;  &lt;a href="http://uni.sina.com.cn/c.php?t=blog&amp;amp;k=it&amp;amp;ts=bpost&amp;amp;stype=tag" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; white-space: nowrap; "&gt;it&lt;/a&gt;&lt;/h3&gt;&lt;h3 style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; font-size: 12px; font-weight: normal; "&gt;  &lt;a href="http://uni.sina.com.cn/c.php?t=blog&amp;amp;k=android&amp;amp;ts=bpost&amp;amp;stype=tag" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; white-space: nowrap; "&gt;android&lt;/a&gt;&lt;/h3&gt;&lt;h3 style="margin-top: 0px; margin-right: 5px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; font-size: 12px; font-weight: normal; "&gt;  &lt;a href="http://uni.sina.com.cn/c.php?t=blog&amp;amp;k=java&amp;amp;ts=bpost&amp;amp;stype=tag" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; white-space: nowrap; "&gt;java&lt;/a&gt;&lt;/h3&gt;&lt;/td&gt;&lt;td class="blog_class" 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; font-size: 12px; font-family: 宋体; vertical-align: top; width: 220px; white-space: nowrap; "&gt;  &lt;span class="SG_txtb" style="color: rgb(99, 113, 96); font-family: 宋体; "&gt;分类：&lt;/span&gt;&lt;a href="http://blog.sina.com.cn/s/articlelist_1065304532_4_1.html" target="_blank" style="text-decoration: none; color: rgb(46, 105, 72); font-family: 宋体; "&gt;Java,Android,Lucene&lt;/a&gt;&lt;/td&gt;  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div class="articalContent" id="sina_keyword_ad_area2" style="clear: both; padding-top: 18px; font-size: 14px; line-height: 21px; padding-bottom: 30px; word-wrap: normal; word-break: normal; overflow-x: hidden; overflow-y: hidden; font-family: simsun; "&gt;  很多情况下，一些与用户很少需要产生交互的应用程序，我们一般让它们在后台运行就行了，而且在它们运行期间我们仍然能运行其他的应用。&lt;br&gt;&lt;br&gt;为了处理这种后台进程，Android引入了Service的概念。Service在Android中是一种长生命周期的组件，它不实现任何用户界面。最常见的例子如：媒体播放器程序，它可以在转到后台运行的时候仍然能保持播放歌曲；或者如文件下载程序，它可以在后台执行文件的下载。&lt;br&gt;&lt;br&gt;让我们来看下如何创建Service：&lt;br&gt;  &lt;strong style="font-weight: bold; "&gt;创建一个Service&lt;/strong&gt;&lt;br&gt;Android中已经定义了一个 &amp;lsquo;Service&amp;rsquo;类，所有其他的Service都继承于该类。Service类中定义了一系列的生命周期相关的方法，如： onCreate(), onStart(), onDestroy()。参见下例：&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  package com.wissen.testApp.service;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  public class MyService extends Service {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public IBinder onBind(Intent intent) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onCreate() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;super.onCreate();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Toast.makeText(this, &amp;ldquo;Service created&amp;hellip;&amp;rdquo;, Toast.LENGTH_LONG).show();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onDestroy() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;super.onDestroy();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Toast.makeText(this, &amp;ldquo;Service destroyed&amp;hellip;&amp;rdquo;, Toast.LENGTH_LONG).show();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  上例中的这个Service主要做了这些事：当服务创建和销毁时通过界面消息提示用户。&lt;br&gt;如Android中的其它部件一样, Service也会和一系列Intent关联。Service的运行入口需要在AndroidManifest.xml中进行配置,如下:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;lt;service class=&amp;rdquo;.service.MyService&amp;rdquo;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;intent-filter&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;action android:value=&amp;rdquo;com.wissen.testApp.service.MY_SERVICE&amp;rdquo; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/intent-filter&amp;gt;&lt;br&gt;&amp;lt;/service&amp;gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;之后我们的Service就可以被其他代码所使用了。&lt;br&gt;&lt;br&gt;&lt;strong style="font-weight: bold; "&gt;使用Service:&lt;/strong&gt;&lt;br&gt;应用程序可以通过调用 Context.startService方法来启动Service。如果当前没有指定的Service实例被创建，则该方法会调用 Service的onCreate方法来创建一个实例；否则调用Service的onStart方法。参见下列代码：&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  ..&lt;br&gt;Intent serviceIntent = new Intent();&lt;br&gt;serviceIntent.setAction(&amp;rdquo;com.wissen.testApp.service.MY_SERVICE&amp;rdquo;);&lt;br&gt;startService(serviceIntent);&lt;br&gt;..&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  以上代码调用了startService方法，Service会持续运行，直到调用stopService()或stopSelf()方法。&lt;br&gt;还有另一种绑定Service的方式：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;hellip;&lt;br&gt;ServiceConnection conn = new ServiceConnection() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onServiceConnected(ComponentName name, IBinder service) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Log.i(&amp;rdquo;INFO&amp;rdquo;, &amp;ldquo;Service bound &amp;ldquo;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onServiceDisconnected(ComponentName arg0) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Log.i(&amp;rdquo;INFO&amp;rdquo;, &amp;ldquo;Service Unbound &amp;ldquo;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;};&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  bindService(new Intent(&amp;rdquo;com.wissen.testApp.service.MY_SERVICE&amp;rdquo;), conn, Context.BIND_AUTO_CREATE);&lt;br&gt;&amp;hellip;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  当应用程序绑定一个Service后，该应用程序和Service之间就能进行互相通信，通常，这种通信的完成依靠于我们定义的一些接口，请看下例:&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  package com.wissen.testApp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  public interface IMyService {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int getStatusCode();&lt;br&gt;}&lt;/p&gt;&lt;div&gt;&lt;div&gt;&lt;div style="margin-left: 2em; "&gt;这里定义了一个方法来获取Service的状态，但是Service是如何来使它起作用的呢？之前我们看到Service中有个返回IBinder对象的onBind方法，这个方法会在Service被绑定到其他程序上时被调用，而这个IBinder对象和之前看到的onServiceConnected方法中传入的那个IBinder是同一个东西。应用和Service间就依靠这个IBinder对象进行通信:&lt;/div&gt;  &lt;div style="margin-left: 2em; "&gt;public class MyService extends Service {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private int statusCode;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private MyServiceBinder myServiceBinder = new MyServiceBinder();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public IBinder onBind(Intent intent) {&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return myServiceBinder;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div style="margin-left: 2em; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public class MyServiceBinder extends Binder implements IMyService {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int getStatusCode() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return statusCode;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;hellip;&lt;br&gt;}&lt;/div&gt;&lt;div style="margin-left: 2em; "&gt;&amp;nbsp;下列代码是说明getStatusCode是如何被调用的:&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style="margin-left: 2em; "&gt;ServiceConnection conn = new ServiceConnection() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onServiceConnected(ComponentName name, IBinder service) {&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IMyService myService = (IMyService) service;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;statusCode = myService.getStatusCode();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Log.i(&amp;rdquo;INFO&amp;rdquo;, &amp;ldquo;Service bound &amp;ldquo;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;hellip;&lt;br&gt;};&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;或者，你也可以通过使用ServiceListener接口来达成相同的目的。&lt;br&gt;  &lt;br&gt;&lt;strong style="font-weight: bold; "&gt;与远程Service通信（进程间Service通信）:&lt;/strong&gt;&lt;br&gt;如何两个进程间的Service需要进行通信，则需要把对象序列化后进行互相发送。&lt;br&gt;Android提供了一个 AIDL (Android接口定义语言)工具来处理序列化和通信。这种情况下Service需要以aidl文件的方式提供服务接口，AIDL工具将生成一个相应的java接口，并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个 stub服务桩类。Service的onBind方法会返回实现类的对象，之后你就可以使用它了，参见下例：&lt;br&gt;  先创建一个IMyRemoteService.aidl文件，内容如下:&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  package com.wissen.testApp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  interface IMyRemoteService {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int getStatusCode();&lt;br&gt;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  如果你正在使用eclipse的 Android插件，则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类，你要做的事就是去继承该Stub类：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  package com.wissen.testApp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  class RemoteService implements Service {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int statusCode;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public IBinder onBind(Intent arg0) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return myRemoteServiceStub;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int getStatusCode() throws RemoteException {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return 0;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;hellip;&lt;br&gt;}&lt;/p&gt;  &lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  当客户端应用连接到这个Service时，onServiceConnected方法将被调用，客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;hellip;&lt;br&gt;ServiceConnection conn = new ServiceConnection() {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Override&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void onServiceConnected(ComponentName name, IBinder service) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IMyRemoteService myRemoteService = IMyRemoteService.Stub.asInterface(service);&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;statusCode = myRemoteService.getStatusCode();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} catch(RemoteException e) {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// handle exception&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Log.i(&amp;rdquo;INFO&amp;rdquo;, &amp;ldquo;Service bound &amp;ldquo;);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;hellip;&lt;br&gt;};&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &lt;strong style="font-weight: bold; "&gt;权限:&lt;/strong&gt;&lt;br&gt;我们可以在AndroidManifest.xml文件中使用&amp;lt;service&amp;gt;标签来指定Service访问的权限：&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;lt;service class=&amp;rdquo;.service.MyService&amp;rdquo; android:permission=&amp;rdquo;com.wissen.permission.MY_SERVICE_PERMISSION&amp;rdquo;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;intent-filter&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;action android:value=&amp;rdquo;com.wissen.testApp.service.MY_SERVICE&amp;rdquo; /&amp;gt;&lt;br&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/intent-filter&amp;gt;&lt;br&gt;&amp;lt;/service&amp;gt;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;nbsp;&lt;/p&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  之后应用程序要访问该Service的话就需要使用&amp;lt;user-permission&amp;gt;标签来指定相应的权限：&lt;/p&gt;&lt;div&gt;&lt;p style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; word-wrap: normal; word-break: normal; line-height: 21px; "&gt;  &amp;lt;uses-permission android:name=&amp;rdquo;com.wissen.permission.MY_SERVICE_PERMISSION&amp;rdquo;&amp;gt;&lt;br&gt;&amp;lt;/uses-permission&amp;gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-4932178197854385432?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/4932178197854385432/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-android-service.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4932178197854385432'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/4932178197854385432'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-android-service.html' title='Android基础 : Android Service'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-8937142588238266891</id><published>2010-07-12T16:34:00.000-07:00</published><updated>2010-07-12T16:35:17.282-07:00</updated><title type='text'>Android虚拟机 Dalvik</title><content type='html'>&lt;img src="http://www.oschina.net/uploads/img/200906/12164216_CwR2.jpg" alt="12164216_CwR2.jpg" title="12164216_CwR2.jpg"&gt;Android虚拟机&amp;nbsp;Dalvik&lt;br&gt;&lt;br&gt;Dalvik虚拟机是Google的用于移动设备的Android平台的一个主要部分。虚拟机可运行Java平台应用程序，这些应用程序被转换成紧凑的Dalvik可执行格式（.dex），该格式适合内存和处理器速度受限的系统。&lt;br&gt;  &lt;br&gt;Dalvik虚拟机的作者是丹伯恩斯坦（Dan Bornstein）。&lt;br&gt;&lt;br&gt;与 大多数虚拟机和真正的Java虚拟机不同，前者是栈机（stack machine），而Dalvik VM是基于寄存器的架构。就像CISC与RISC的争论，这两种方式的相对优点是一个不断争论的话题，且有时技术界限会变得模糊不清。此外，两种方法的相 对优势取决于所选择的解释/编译策略。但是，总的来说，基于stack的机器必须使用指令来载入stack上的数据，或使用指令来操纵数据，因此与基于寄 存器的机器相比，需要的指令更多。然而，在寄存器的指令必须编码源和目的地寄存器，因此往往指令更大。&lt;br&gt;  &lt;br&gt;一个名为dx的工具，它用于转换 Java的.class文件到.dex格式。多个类文件可包含到单个的.dex文件中。重复的、可用于多个类的字符串和其它常量在转换到.dex格式时输 出到保留空间。Java字节码还可转换成可选择的、Delvik VM使用的指令集。一个未压缩的.dex文件在文件大小方面往往比从同样的.class文件压缩成的.jar文件更小。&lt;br&gt;&lt;br&gt;当Dalvik可执行文件安装到移动设备时，它们是可以被修改的。为了进一步的优化，在某些数据、简单数据结构和内联的函数库中的字节顺序可以互换，例如空类对象被短路。&lt;br&gt;  &lt;br&gt;为满足低内存要求而不断优化， Dalvik虚拟机有一些独特的、有别于其它标准虚拟机的特征：&lt;br&gt;&lt;br&gt;（1）虚拟机很小，使用的空间也小；&lt;br&gt;（2）Dalvik没有JIT编译器；&lt;br&gt;（3）常量池已被修改为只使用32位的索引，以简化解释器；&lt;br&gt;（4）它使用自己的字节码，而非Java字节码。&lt;br&gt;&lt;br&gt;此外， Dalvik被设计来满足可高效运行多种虚拟机实例。&lt;br&gt;&lt;br&gt;Dalvik虚拟机在Android架构中的位置&lt;br&gt;  &lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.oschina.net/uploads/img/200906/12164216_CwR2.jpg"&gt;http://www.oschina.net/uploads/img/200906/12164216_CwR2.jpg&lt;/a&gt;&lt;br&gt;&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-8937142588238266891?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/8937142588238266891/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-dalvik.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/8937142588238266891'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/8937142588238266891'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/android-dalvik.html' title='Android虚拟机 Dalvik'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-668043004802205788</id><published>2010-07-12T16:32:00.001-07:00</published><updated>2010-07-12T16:32:42.418-07:00</updated><title type='text'>Android的虚拟机Dalvik 介绍</title><content type='html'>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, 宋体, sans-serif; font-size: 13px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;h2 class="BlogType_4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 2px; font-size: 12pt; "&gt;  &lt;a href="" 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; color: rgb(170, 0, 0); text-decoration: underline; "&gt;Android的虚拟机Dalvik 介绍&lt;/a&gt;&lt;/h2&gt;  &lt;div class="outline" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; color: rgb(153, 153, 153); font-size: 9pt; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(204, 204, 204); "&gt;  &lt;span class="time" style="margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;发表于 2010年07月07日 17:23&lt;/span&gt;&amp;nbsp;&lt;span class="catalog" style="margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;分类:&amp;nbsp;&lt;a href="http://my.oschina.net/zhijie/blog?catalog=37030" 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; color: rgb(153, 153, 153); "&gt;工作日志&lt;/a&gt;&lt;/span&gt;&amp;nbsp;&lt;span class="stat" style="margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;统计: 0评/14阅&lt;/span&gt;	&lt;span class="favorite" style="margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;&lt;em id="p_attention_count" 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; "&gt;1&lt;/em&gt;人关注此文章，&amp;nbsp;&lt;span id="attention_it" style="margin-top: 0px; margin-right: 10px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;&lt;a href="" 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; color: rgb(62, 98, 166); "&gt;关注此文章&lt;/a&gt;(&lt;a href="http://my.oschina.net/zhijie/help?topic=favorite&amp;amp;modal=true&amp;amp;height=300" title="什么是关注" class="thickbox" 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; color: rgb(153, 153, 153); "&gt;?&lt;/a&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;  &lt;div class="content TextContent" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 10px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; line-height: 26px; color: rgb(68, 68, 68); font-size: 10.5pt; "&gt;  随着上周Google的Android SDK的发布，关于它的API以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举。不过，其中的一个话题在Java社区是一石激起千层浪，这就是 Android平台的基础&amp;mdash;&amp;mdash;Dalvik虚拟机。&lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;  　　Dalvik和标准Java虚拟机（JVM）之间的首要差别之一，就是Dalvik基于寄存器，而JVM基于栈。一直以来都有人在猜测，选择基 于寄存器的方式是因为它对提前优化（ahead-of-time optimization）提供了更好的支持，而这对类似于移动电话这样的受限环境是颇有裨益的。另一份针对基于寄存器虚拟机和基于栈虚拟机更深入的比较 分析指出，基于寄存器的虚拟机对于更大的程序来说，在它们编译的时候，花费的时间更短。&lt;/p&gt;&lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;  　　Dalvik和Java之间的另外一大区别就是运行环境&amp;mdash;&amp;mdash;Dalvik经过优化，允许在有限的内存中同时运行多个虚拟机的实例，并且每一个 Dalvik应用作为一个独立的Linux进程执行。Neil Bartlett指出，给每一个应用赋予独立的进程可以允许动态安装、激活和去激活，但是他对Dalvik为什么要选择这种方式而没有使用OSGi在单一 进程中实现表示疑问&amp;mdash;&amp;mdash;Radoslav Gerganov回复说，独立的进程可以防止在虚拟机崩溃的时候所有程序都被关闭。Carl Rosenberger也指出OSGi也可以被移植到Android平台，而Jilles van Gurp对Google为何选择重新实现若干组件，如跨进程通信，表示疑问。&lt;/p&gt;  &lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;　　此外，Java也已经不再是人们在Dalvik上开发所选择的唯一语言了&amp;mdash;&amp;mdash;已经有人在Dalvik上运行Scala取得了成功，并且Hecl 也已经被成功移植了。另外更有人对运行Groovy做了一次尝试，不过目前为止还不怎么成功。Mono项目的创始人Miguel de Icaza也对在Dalvik源码公开之后将Mono整合到Dalvik上表示了兴趣，而且也已经有人猜测如何用多种方式来实现整合了，包括与随 Android SDK提供的Java到Dalvik重编译器类似的CIL（Common Intermediate Language，通用中间语言）到Dalvik重编译器。&lt;/p&gt;  &lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;　　Dalvik的诞生也导致人们开始忧虑Java平台的第一次大规模的分道扬镳或许已经是进行时了&amp;mdash;&amp;mdash;有人已经把Davlik和微软的JVM以及 Sun 对微软的诉讼联系起来，等着看Google身上是否也会发生类似事情；另外一些人则指出，Google并没有宣称Dalvik是一个Java实现，而微软 却是这样做的。Sun也对可能带来的阵营分裂表达了忧虑情绪，并提出和Google合作来保证Dalvik和JVM之间的兼容性&amp;mdash;&amp;mdash;Google对此的解 释是，Dalvik是对解决目前Java ME平台上分裂的一次尝试，也是为了提供一个拥有较少限制许可证的平台。甚至还有人怀疑这是否是Sun和Google两大阵营对Java之未来的一次大规 模较量。Ian Skerret认为，Dalvik的诞生是对Sun尝试控制和保护来自Java ME收入来源的一次反应，以及对建立OpenJDK统辖理事会迟迟未果的回答。这也导致Dalibor Topic怀疑Google是否要重履Sun走过的路：&lt;/p&gt;  &lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;　　当然，一个很有意思的问题是，为什么没人有勇气拿Google关于OpenJDK的问题反过来问Google呢？&lt;/p&gt;  &lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;　　虽然Android号称开源，但它仍是专有产品。Android做过兼容性保证，是在秘密会议室中签署和保管的。Android不具备任何治理 模型，也没有证据指出将来会出现治理模型。Android没有规范，并且它的许可证禁止任何替代实现的开发，因为这并非Google在SDK许可证中授权 许可的使用权。Android完全在Google的掌控之下，一旦有竞争性应用在财政上损害了Google的利益，Google是保有一刀抹杀这些应用的 权利的。从设计伊始，Android就收到限制，只能在Google的财务利益允许的条件内开放。专有的Java也不是什么好货色，旧瓶装新酒而已。&lt;/p&gt;  &lt;p style="margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0em; "&gt;　　这就好像我们在见证JCP的重生一样，人们排着队把开源社区的&amp;ldquo;街头信誉&amp;rdquo;在一个单一的、专有的实现的基础上借给另外一个封闭的厂商垄断集团。 只不过这次的大头改姓Google，而不是Sun了。&lt;br 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; "&gt;  Stefano Mazzocchi发布了一篇分析报告，深切入里地探讨了围绕Java ME和Dalvik的许可证问题，他得出结论说，Dalvik的市场定位良好，足以给移动电话市场带来冲击。尽管Google一直都很小心避免引起诉讼的 几个关键点，但Mazzocchi相信Sun还是会起草知识产权案的状告书（IBM也有可能）。他还指出，由于在JCP之外操作，Google可以非常快 地对Android进行更改，而且可以避开Sun对任何JCP更动的否决权&amp;mdash;&amp;mdash;这样他们也可以为诸如USB和蓝牙这样的组件加入接口，而这些组件在基础 Java ME实现中是不可用的。最后，通过在Apache许可证下授权许可Dalvik的源码，移动电话运营商更有可能采用Dalvik，因为运营商可以在不花费 许可费用的情况下使用和修改它。&lt;/p&gt;  &lt;/div&gt;&lt;/span&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-668043004802205788?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/668043004802205788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/androiddalvik.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/668043004802205788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/668043004802205788'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/androiddalvik.html' title='Android的虚拟机Dalvik 介绍'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-5614112134726912179</id><published>2010-07-11T23:25:00.000-07:00</published><updated>2010-07-11T23:26:15.356-07:00</updated><title type='text'>iPad 与平板</title><content type='html'>iPad就是平板&amp;nbsp;&lt;div&gt;最近iPad发布后，许多人言必称iPad。虽然我有iPhone以及ipod touch，而且我也热情地赞美苹果的产品。但是，iPad就是一切吗？ 作为一个Ubuntu用户，对我来说，iPhone等对我来说有一个极大的问题，就是同步问题。每次同步都要连上iTune，这让无法安装iTune的Ubuntu用户情何以堪啊。所以，我不得不安装一个虚拟机，来进行软件的安装以及音乐的同步等功能。每次启动虚拟机，我都要问候一下乔教主的家人及十八代祖宗。Mac和Linux的血缘关系比和Window近多了，就为什么不给一个Ubuntu用户同步的机会呢？&lt;/div&gt;  &lt;div&gt;&amp;nbsp;容量问题是困扰我的第二个问题。也许现在可以移动存储的东西越来越多了，所以，iPhone的8、16、32G空间虽然没安装什么东西，但是也只能让它空着。因为根本不能向里面安装任何东西，除了使用iTune同步以外。苹果可以将它的IOS作为宝贝隐藏，但也应该隔离出一点空间来让我们自由使用吧。&amp;nbsp;&lt;/div&gt;&lt;div&gt;Appstore问题也同样操蛋。非要使用Mac机才可以。我的Ubuntu，你的Windows都不行。对于业余开发者而言，安装一个Eclipse和ADT都是很简单的，在任何计算机上都可以简单实现的。而且，将它放到菜市场中，也不要什么费用。审查，对于像我这样，对极权体制有深刻体会的人来说，对于审查的敏感程度绝不亚于小处女对于露阴狂器官的感觉。我看到苹果粉一方面在说长尾理论，批评联想的乐Phone忽略了长尾的后面部分，说这些尾巴才是苹果的成功根源，一方面又对Android的审查大加鞭挞，说是造成了大量低级的程序进入。但这应该是长尾的很重要的部分啊，对不对，达人们。你觉得低质，你觉得有色情，有暴力，对不起，只要法律允许，这些也许都是我幸福的来源呢？&amp;nbsp;&lt;/div&gt;  &lt;div&gt;价格，这就更是我们的切肤之痛了。我们辛辛苦苦赚来的钱，要交房贷、车贷，要准备孩子高昂的教育费用，要支付老人以及我们自己未来的医辽费用，还要向各级官僚进贡，每个月微薄的薪水其实剩不了几个。对于大多数人而言，￥4500起的费用绝不是轻易就可挥霍的。现在国产的山寨iPad7寸的只要500到1000等，而10寸的似乎也只要1100左右。这两个比起来，差得就比较远了。&lt;/div&gt;&lt;div&gt;&amp;nbsp;屏幕，也是果粉的一个重要精神支柱。苹果的屏幕是比较漂亮，但是业界最好的吗，不至于。有许多更优秀的屏幕被淹没在尘埃中了。这也是苹果的营销策略的成功而已。一个电容屏而已，很多山寨厂家也可以做到。如果Android发展到3.0,那么其GUI与多点触摸支持的程度，绝不比iPad差多少。 市场支持度，现在Android的app数量应该超过10万个了，这个app的发展速度应该比苹果快吧。它的开放度好，对于远程医辽、办公、点菜、数据采集之类的应用，成本绝对要比苹果低很多吧。加上各大厂商的支持，达到商业运用的强度，发展潜力我觉得绝对胜过苹果。&amp;nbsp;&lt;/div&gt;  &lt;div&gt;呵呵，我不是Google中人，也离Android有点远，只是祖国边疆的服务于政府的一个小公务员而已。 &amp;nbsp;&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5952752232226176368-5614112134726912179?l=ubuntuwriter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ubuntuwriter.blogspot.com/feeds/5614112134726912179/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/ipad.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5614112134726912179'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5952752232226176368/posts/default/5614112134726912179'/><link rel='alternate' type='text/html' href='http://ubuntuwriter.blogspot.com/2010/07/ipad.html' title='iPad 与平板'/><author><name>小城好人</name><uri>http://www.blogger.com/profile/00066468025748785860</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5952752232226176368.post-649534092565682526</id><published>2010-06-01T22:48:00.000-07:00</published><updated>2010-06-01T22:49:15.156-07:00</updated><title type='text'>get datetime and listen 13 port server</title><content type='html'>原来看TCP/IP详解的书的时候，TCP/IP的知识比较缺乏，第一个程序始终得不到任何响应。使用的是send和recvfrom函数。&lt;br&gt;&lt;br&gt;后来，看UNIX网络编程，使用了第一个程序，然后使用其后附带的ip206.168.112.96,很快就等到了一个日期字符串。&lt;br&gt;在其后还有一个提供日期服务的程序，在本机上运行了一个。然后直接调用本机的日期服务，很快就得到数据。&lt;br&gt;下面这个是监听程序。编译后运行在本机，由于其有一个无限循环函数，会一直运行。&lt;br&gt;  /*&lt;br&gt;&amp;nbsp;* =====================================================================================&lt;br&gt;&amp;nbsp;*&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Filename:&amp;nbsp; time_server.c&lt;br&gt;&amp;nbsp;*&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp; Description:&amp;nbsp; listen 13 port,and supply date time info&lt;br&gt;&amp;nbsp;*&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Version:&amp;nbsp; 1.0&lt;br&gt;  &amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Created:&amp;nbsp; 06/02/2010 12:53:54 PM&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Revision:&amp;nbsp; none&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Compiler:&amp;nbsp; gcc&lt;br&gt;&amp;nbsp;*&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Author:&amp;nbsp; YOUR NAME (),&lt;br&gt;&amp;nbsp;*&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Company:&lt;br&gt;&amp;nbsp;*&lt;br&gt;&amp;nbsp;* =====================================================================================&lt;br&gt;  &amp;nbsp;*/&lt;br&gt;#include &amp;lt;sys/types.h&amp;gt;&lt;br&gt;#include &amp;lt;sys/socket.h&amp;gt;&lt;br&gt;#include &amp;lt;netinet/in.h&amp;gt;&lt;br&gt;#include &amp;lt;arpa/inet.h&amp;gt;&lt;br&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br&gt;//#include&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;netinet/in.h&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* sockaddr_in{} and other Internet defns */&lt;br&gt;  #include&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;arpa/inet.h&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* inet(3) functions */&lt;br&gt;#include&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;fcntl.h&amp;gt;&amp;nbsp;
