<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>侯剑凯的博客[ithouge]</title>
	<atom:link href="http://bg135.com/feed" rel="self" type="application/rss+xml" />
	<link>http://bg135.com</link>
	<description>共同探讨学习网站SEO及网络推广,Linux,PHP,Nginx,Mysql,Java,Android编程开发，Wordpress、Supesite、Discuz! X二次开发</description>
	<lastBuildDate>Wed, 11 Jan 2012 09:24:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Android开发环境搭建</title>
		<link>http://bg135.com/android%e5%bc%80%e5%8f%91%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba.html</link>
		<comments>http://bg135.com/android%e5%bc%80%e5%8f%91%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba.html#comments</comments>
		<pubDate>Wed, 11 Jan 2012 09:22:56 +0000</pubDate>
		<dc:creator>root</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=229</guid>
		<description><![CDATA[最近经常搭建android开发环境，在此记录下来仅供参考，搭建的系统环境：Ubuntu 11.04  32位。 首先，要准备的文件有： JDK，下载地址 Android SDK   下载地址 Eclipse   下载地址 &#160; 下载对应的包到本地，首先安装jdk，进入jdk所在目录，如： cd  /home/ithouge/Android root@ithouge-OptiPlex-780:/home/ithouge/Android# ll 总用量 206464 drwxr-xr-x 11 root       root        4096 2011-12-31 15:10 ./ drwxr-xr-x 39 ithouge    ithouge     4096 2011-12-31 14:27 ../ drwxr-xr-x  9      28587    5000     4096 2011-12-27 &#8230;]]></description>
			<content:encoded><![CDATA[<p>最近经常搭建android开发环境，在此记录下来仅供参考，搭建的系统环境：Ubuntu 11.04  32位。</p>
<p>首先，要准备的文件有：</p>
<p>JDK，<a title="http://www.oracle.com/technetwork/java/javase/downloads/index.html" href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank">下载地址</a></p>
<p>Android SDK   <a title="http://developer.android.com/sdk/index.html" href="http://developer.android.com/sdk/index.html" target="_blank">下载地址</a></p>
<p>Eclipse   <a title="http://www.eclipse.org/downloads/" href="http://www.eclipse.org/downloads/" target="_blank">下载地址</a></p>
<p>&nbsp;</p>
<p>下载对应的包到本地，首先安装jdk，进入jdk所在目录，如：</p>
<p>cd  /home/ithouge/Android</p>
<p>root@ithouge-OptiPlex-780:/home/ithouge/Android# ll<br />
总用量 206464<br />
drwxr-xr-x 11 root       root        4096 2011-12-31 15:10 ./<br />
drwxr-xr-x 39 ithouge    ithouge     4096 2011-12-31 14:27 ../<br />
drwxr-xr-x  9      28587    5000     4096 2011-12-27 18:27 android-ndk-r6b/<br />
drwxrwxr-x 12      53856    5000     4096 2011-12-29 10:35 android-sdk_r12-linux_x86/<br />
-rw-r&#8211;r&#8211;  1 root       root    30034243 2011-09-03 18:40 android-sdk_r12-linux_x86.tgz<br />
drwxr-x&#8212; 12      12275    5000     4096 2011-12-31 15:30 android-sdk_r16-linux/<br />
drwxr-xr-x 14 root       root        4096 2011-12-22 18:54 cocos2d-1.0.1-x-0.9.2/<br />
drwxrwsr-x  9 libuuid    users       4096 2012-01-10 13:56 eclipse/<br />
drwxrwsr-x  9 libuuid    users       4096 2011-12-31 14:19 eclipse3.5/<br />
drwxrwsr-x  9 messagebus users       4096 2012-01-11 11:54 eclipse3.6/<br />
-rwxrwxrwx  1        777 ithouge 96870084 2011-01-13 12:33 eclipse-java-galileo-SR2-linux-gtk.tar(1).gz*<br />
drwxr-xr-x 10 root       root        4096 2011-09-03 18:09 jdk1.6.0_23/<br />
-rwxrwxrwx  1        777 ithouge 84461758 2011-01-13 12:50 jdk-6u23-linux-i586.bin*<br />
drwxr-xr-x  2 root       root        4096 2011-10-13 11:45 kernel/</p>
<p>确保jdk-6u23-linux-i586.bin具有可执行权限后，如果没有则执行</p>
<p>chmod a+x  jdk-6u23-linux-i586.bin</p>
<p>输入命令：</p>
<p>./jdk-6u23-linux-i586.bin</p>
<p>执行安装jdk,</p>
<p>安装成功之后解压缩android-sdk包，得到android-sdk-linux，进入对应的tools目录，双击android可执行文件，点击运行即可，</p>
<p>此时仅仅是运行了sdk的外壳，其没有安装android任何的api,可以根据自身需求选择安装sdk不同版本的api,</p>
<p>&nbsp;</p>
<p>至此最好配置下环境变量，ubuntu配置方法之一：</p>
<p>gedit /etc/profile</p>
<p>&nbsp;</p>
<p>在末尾加上：</p>
<p>export JAVA_HOME=/home/ithouge/Android/jdk1.6.0_23<br />
export PATH=$PATH:$JAVA_HOME/bin:/home/ithouge/Android/android-sdk_r16-linux/tools:/home/ithouge/Android/android-sdk_r16-linux/platform-tools:/home/ithouge/Android/android-ndk-r6b:/home/ithouge/Android/cocos2d-1.0.1-x-0.9.2</p>
<p>路径因人而已，只需要替换更改即可，保存之后，执行</p>
<p>source /etc/profile</p>
<p>使得之前配置的环境变量生效</p>
<p>时刻你可以尝试执行</p>
<p>root@ithouge-OptiPlex-780:/home/ithouge/Android# java -version<br />
java version &#8220;1.6.0_23&#8243;<br />
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)<br />
Java HotSpot(TM) Server VM (build 19.0-b09, mixed mode)</p>
<p>如果看到类似以上的信息，则说明jdk安装成功，否则再次安装</p>
<p>&nbsp;</p>
<p>接下来解压eclipse,</p>
<p>双击eclipse可执行文件，点击运行，启动eclipse后配置android插件，Help-&gt;Install New SoftWare  弹出一个Install对话框，</p>
<p>点击Add,在name选项中输入随意名称，在Location选项处填入:https://dl-ssl.google.com/android/eclipse/  点击ok</p>
<p>稍等片刻出现Developer Tools</p>
<p>展开，选中Android DDMS ,Android Development Tool  一路next,直至安装完毕。</p>
<p>安装完成之后，在eclipse选项栏，选择Window进入preferences项，弹出一个对话框后，选择左侧的Android,配置SDK的路径，点击Browse找到之前解压的sdk的路径，选择Apply之后，列表里面显示之前下载安装的sdk，点击ok.</p>
<p>&nbsp;</p>
<p>至此，Android开发环境搭建完成，开启你的android开发之旅吧。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/android%e5%bc%80%e5%8f%91%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SWIG Tutorial</title>
		<link>http://bg135.com/swig-tutorial.html</link>
		<comments>http://bg135.com/swig-tutorial.html#comments</comments>
		<pubDate>Tue, 20 Dec 2011 07:11:57 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[JAVA/JSP]]></category>
		<category><![CDATA[swig]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=226</guid>
		<description><![CDATA[关于SWIG官方的一个Demo,参见：http://www.swig.org/tutorial.html，有些地方讲解的不是很详细，初次尝试编译的时候，往往编译不通过，下面结合我编译的过程做一个分析。 首先我的编译环境是： &#160; root@ithouge-OptiPlex-780:/home/ithouge/Android/jdk1.6.0_23/include/linux# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.04 Release: 11.04 Codename: natty gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) 1,新建一个测试用的C文件：/ithouge/test/SWIG/Demo2/example.c &#160; #include double My_variable = 3.0; &#160; int fact&#40;int n&#41; &#123; if &#40;n &#8230;]]></description>
			<content:encoded><![CDATA[<p>关于SWIG官方的一个Demo,参见：<a href="http://www.swig.org/tutorial.html">http://www.swig.org/tutorial.html</a>，有些地方讲解的不是很详细，初次尝试编译的时候，往往编译不通过，下面结合我编译的过程做一个分析。</p>
<p>首先我的编译环境是：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">root@ithouge<span style="color: #339933;">-</span>OptiPlex<span style="color: #339933;">-</span><span style="color: #cc66cc;">780</span><span style="color: #339933;">:/</span>home<span style="color: #339933;">/</span>ithouge<span style="color: #339933;">/</span>Android<span style="color: #339933;">/</span>jdk1.6.0_23<span style="color: #339933;">/</span>include<span style="color: #339933;">/</span>linux# lsb_release <span style="color: #339933;">-</span>a
No LSB modules are available.
<span style="color: #006633;">Distributor</span> ID<span style="color: #339933;">:</span>	Ubuntu
Description<span style="color: #339933;">:</span>	Ubuntu <span style="color: #cc66cc;">11.04</span>
Release<span style="color: #339933;">:</span>	<span style="color: #cc66cc;">11.04</span>
Codename<span style="color: #339933;">:</span>	natty</pre></div></div>

<p>gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)</p>
<p>1,新建一个测试用的C文件：/ithouge/test/SWIG/Demo2/example.c</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include</span>
 <span style="color: #993333;">double</span> My_variable <span style="color: #339933;">=</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #993333;">int</span> fact<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> n<span style="color: #339933;">*</span>fact<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #993333;">int</span> my_mod<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>x<span style="color: #339933;">%</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>get_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
     time_t ltime<span style="color: #339933;">;</span>
     time<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>ime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">return</span> ctime<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>ime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>2,编写相应的接口文件：/ithouge/test/SWIG/Demo2/example.i</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* example.i */</span>
 <span style="color: #339933;">%</span>module example
 <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
 <span style="color: #808080; font-style: italic;">/* Put header files here or function declarations like below */</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">double</span> My_variable<span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> fact<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> my_mod<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>get_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">double</span> My_variable<span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> fact<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> my_mod<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>get_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>3,建立java,jni,c模块</p>
<p>首先要确保已经正确安装：swig,可以用以下命令查看。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">root@ithouge<span style="color: #339933;">-</span>OptiPlex<span style="color: #339933;">-</span><span style="color: #0000dd;">780</span><span style="color: #339933;">:/</span>ithouge<span style="color: #339933;">/</span>test<span style="color: #339933;">/</span>SWIG<span style="color: #339933;">/</span>Demo2<span style="color: #339933;"># swig -version</span>
&nbsp;
SWIG Version 1.3.40
&nbsp;
Compiled with g<span style="color: #339933;">++</span> <span style="color: #009900;">&#91;</span>i686<span style="color: #339933;">-</span>pc<span style="color: #339933;">-</span>linux<span style="color: #339933;">-</span>gnu<span style="color: #009900;">&#93;</span>
Please see http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//www.swig.org for reporting bugs and further information</span>
&nbsp;
root@ithouge<span style="color: #339933;">-</span>OptiPlex<span style="color: #339933;">-</span><span style="color: #0000dd;">780</span><span style="color: #339933;">:/</span>ithouge<span style="color: #339933;">/</span>test<span style="color: #339933;">/</span>SWIG<span style="color: #339933;">/</span>Demo2<span style="color: #339933;"># whereis swig</span>
swig<span style="color: #339933;">:</span> <span style="color: #339933;">/</span>usr<span style="color: #339933;">/</span>bin<span style="color: #339933;">/</span>swig <span style="color: #339933;">/</span>usr<span style="color: #339933;">/</span>share<span style="color: #339933;">/</span>swig1.3 <span style="color: #339933;">/</span>usr<span style="color: #339933;">/</span>share<span style="color: #339933;">/</span>man<span style="color: #339933;">/</span>man1<span style="color: #339933;">/</span>swig.1.<span style="color: #202020;">gz</span></pre></div></div>

<p>如果显示信息如上则说明已经安装swig,否则通过apt-get install swig进行安装。</p>
<p>现在，进入到exexample.i所在目录：cd /ithouge/test/SWIG/Demo2</p>
<p>在终端执行：swig -java example.i</p>
<p>执行完成，在当前目录下生成三个新文件：example.java  exampleJNI.java   example_wrap.c</p>
<p>接下来编译.c文件：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">gcc <span style="color: #339933;">-</span>c example.<span style="color: #202020;">c</span> example_wrap.<span style="color: #202020;">c</span> <span style="color: #339933;">-</span>I <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>ithouge<span style="color: #339933;">/</span>Android<span style="color: #339933;">/</span>jdk1.6.0_23<span style="color: #339933;">/</span>include<span style="color: #339933;">/</span> <span style="color: #339933;">-</span>I <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>ithouge<span style="color: #339933;">/</span>Android<span style="color: #339933;">/</span>jdk1.6.0_23<span style="color: #339933;">/</span>include<span style="color: #339933;">/</span>linux<span style="color: #339933;">/</span></pre></div></div>

<p>其中：<br />
/home/ithouge/Android/jdk1.6.0_23/include/ 是jni.h文件所在路径，</p>
<p>查找jni.h所在路径：locate jni.h<br />
/home/ithouge/Android/jdk1.6.0_23/include/linux/ 是jni_mb.h文件所在路径，<br />
执行完以上命令则生成两个.o文件，分别为：example.o  example_wrap.o<br />
最后生成共享库：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"> gcc <span style="color: #339933;">-</span>shared example.<span style="color: #202020;">o</span> example_wrap.<span style="color: #202020;">o</span> <span style="color: #339933;">-</span>o libexample.<span style="color: #202020;">so</span></pre></div></div>

<p>这样在当前目录下生成libexample.so文件，</p>
<p>&nbsp;</p>
<p>4， 编写java运行程序</p>
<p>至此已经生成动态库，接下来便是编写java程序，实现功能函数。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">public class main <span style="color: #009900;">&#123;</span>
   public <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span>String argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//     System.loadLibrary(&quot;example&quot;);</span>
      System.<span style="color: #202020;">load</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/ithouge/test/SWIG/Demo2/libexample.so&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     System.<span style="color: #202020;">out</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>example.<span style="color: #202020;">getMy_variable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     System.<span style="color: #202020;">out</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>example.<span style="color: #202020;">fact</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     System.<span style="color: #202020;">out</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>example.<span style="color: #202020;">get_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>System.load/System.loadLibrary作用是加载动态链接库。<br />
之后编译java: javac main.java<br />
运行函数:java main<br />
得到结果如下：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">root@ithouge<span style="color: #339933;">-</span>OptiPlex<span style="color: #339933;">-</span><span style="color: #0000dd;">780</span><span style="color: #339933;">:/</span>ithouge<span style="color: #339933;">/</span>test<span style="color: #339933;">/</span>SWIG<span style="color: #339933;">/</span>Demo2<span style="color: #339933;"># java main</span>
<span style="color:#800080;">3.0</span>
<span style="color: #0000dd;">120</span>
Tue Dec <span style="color: #0000dd;">20</span> <span style="color: #0000dd;">14</span><span style="color: #339933;">:</span><span style="color: #0000dd;">31</span><span style="color: #339933;">:</span><span style="color: #0000dd;">20</span> <span style="color: #0000dd;">2011</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://bg135.com/swig-tutorial.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android开发经验积累</title>
		<link>http://bg135.com/android%e5%bc%80%e5%8f%91%e7%bb%8f%e9%aa%8c%e7%a7%af%e7%b4%af.html</link>
		<comments>http://bg135.com/android%e5%bc%80%e5%8f%91%e7%bb%8f%e9%aa%8c%e7%a7%af%e7%b4%af.html#comments</comments>
		<pubDate>Tue, 13 Dec 2011 07:30:41 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=223</guid>
		<description><![CDATA[随着移动平台的发展和其应用的不断改善，质量成为决定成败的关键。用户要求他们选择安装的应用响应快、性能好，如果某个应用不能提供卓越的功能和稳定的用户体验，那这样的应用注定会被很快卸载。 尽管现在Android智能手机和平板电脑的运行速率越来越快，但开发者仍需牢记，应用的运行环境仍受到电池和处理器等诸多资源的限制。以下是如何为应用“瘦身”的十条建议，以便能在当前和以后的所有Android设备都能运行出最佳效果。 1）首先要有良好的编码习惯 一个优秀的开发者应该善于运用常识、完善的算法和标准设计模式。要有资源意识，打开了就要记得关闭，尽量做到晚获取，早释放。这些由来已久的编码准则同样适用Android应用开发，尤其是使用基础设备服务时。 2）让阻塞操作远离主UI线程 通过使用AsyncTask、线程、IntentService和自定义后台服务，保证应用的灵活性。使用加载工具简化游标等长时间加载数据的状态管理。当有其他程序运行时，不能让你的应用滞后或中止。 如果一个操作需要消耗较多时间和资源时，取消该操作，换成异步处理，这样应用就能保持响应，用户可以继续各种操作。该方法适用磁盘读写、访问内容提供方、数据库和互联网，以及解析和其他需要花费较长时间的任务。 3）使用最新的Android SDK版本和API 使用Android平台的最新产品，保证你的应用紧跟Android的更新步伐。随着Android平台的不断发展，部分功能可能被弃用或被更好的功能 取代，核心API接收了bug修复和性能改进，新API有助于开发者编写出更稳定的应用。要明白最佳的做法总是随着时间的推移而变，聪明的开发者应该总是 站在整个平台的最前沿。 4）考虑使用StrictMode 从Android 2.3开始提供了一个新的类StrictMode，该类可以用于捕捉发生在应用程序主线程中耗时的磁盘、网络访问或函数调用，可以帮助开发者改进程序，使主线程处理UI和动画在磁盘读写和网络操作时变得更平滑，避免主线程被阻塞。 5）发布前禁用或尽量减少调试 如果开发周期较长，你很可能在应用中内置了一些日志或调试代码，在发布前确保这些功能已经最小化或完全禁用。 6）确保UI布局简单优雅 简单的屏幕不仅方便阅读，还能加快加载速度。与其在一个单一屏幕上堆砌太多不必要的功能，不如花时间去开发优雅的用户界面。简单优雅的UI不仅能提高应用性能，还能提高用户使用该应用时的效率。 7）根据目标设备调整应用资源 为尽可能高效地被加载，需要根据具体设备的配置调整相应资源，尤其是图片资源。为使应用包文件合理适用不同设备，首先可只添加运行该应用需要的核心资源，然后再根据具体设备下载相关内容。 8）使用Hierachy Viewer可视化调试工具 Hierachy Viewer能很方便地在开发者设计，调试和调整界面时，快速定位问题，解决问题，提高开发效率。 9）使用layoutopt进行布局优化 Layoutopt是一款简单的命令行工具，可帮助找到不必要的控件嵌套以及缩减布局资源，从而使应用变得可能“苗条”。控件越少、布局层次越浅，性能就越好。 10）使用Traceview及其他Android工具进行分析 Android SDK随带了很多用于应用分析的工具，其中最受欢迎的是Traceview，这款图形工具可以帮助调试和找到应用中的性能瓶颈。 结语 提升Android应用性能的方法有很多，有些需要使用特定的算法，有些依赖切实可行的调试和性能监测技术。幸运的是，Android平台随带了众多免费的实用工具，可以帮助跟踪和解决应用程序中的性能问题。]]></description>
			<content:encoded><![CDATA[<p>随着移动平台的发展和其应用的不断改善，质量成为决定成败的关键。用户要求他们选择安装的应用响应快、性能好，如果某个应用不能提供卓越的功能和稳定的用户体验，那这样的应用注定会被很快卸载。<br />
尽管现在Android智能手机和平板电脑的运行速率越来越快，但开发者仍需牢记，应用的运行环境仍受到电池和处理器等诸多资源的限制。以下是如何为应用“瘦身”的十条建议，以便能在当前和以后的所有Android设备都能运行出最佳效果。</p>
<h3>1）首先要有良好的编码习惯</h3>
<p>一个优秀的开发者应该善于运用常识、完善的算法和标准设计模式。要有资源意识，打开了就要记得关闭，尽量做到晚获取，早释放。这些由来已久的编码准则同样适用Android应用开发，尤其是使用基础设备服务时。</p>
<h3>2）让阻塞操作远离主UI线程</h3>
<p>通过使用AsyncTask、线程、IntentService和自定义后台服务，保证应用的灵活性。使用加载工具简化游标等长时间加载数据的状态管理。当有其他程序运行时，不能让你的应用滞后或中止。</p>
<p>如果一个操作需要消耗较多时间和资源时，取消该操作，换成异步处理，这样应用就能保持响应，用户可以继续各种操作。该方法适用磁盘读写、访问内容提供方、数据库和互联网，以及解析和其他需要花费较长时间的任务。</p>
<h3>3）使用最新的Android SDK版本和API</h3>
<p>使用Android平台的最新产品，保证你的应用紧跟Android的更新步伐。随着Android平台的不断发展，部分功能可能被弃用或被更好的功能 取代，核心API接收了bug修复和性能改进，新API有助于开发者编写出更稳定的应用。要明白最佳的做法总是随着时间的推移而变，聪明的开发者应该总是 站在整个平台的最前沿。</p>
<h3>4）考虑使用StrictMode</h3>
<p>从Android 2.3开始提供了一个新的类StrictMode，该类可以用于捕捉发生在应用程序主线程中耗时的磁盘、网络访问或函数调用，可以帮助开发者改进程序，使主线程处理UI和动画在磁盘读写和网络操作时变得更平滑，避免主线程被阻塞。</p>
<h3>5）发布前禁用或尽量减少调试</h3>
<p>如果开发周期较长，你很可能在应用中内置了一些日志或调试代码，在发布前确保这些功能已经最小化或完全禁用。</p>
<h3>6）确保UI布局简单优雅</h3>
<p>简单的屏幕不仅方便阅读，还能加快加载速度。与其在一个单一屏幕上堆砌太多不必要的功能，不如花时间去开发优雅的用户界面。简单优雅的UI不仅能提高应用性能，还能提高用户使用该应用时的效率。</p>
<h3>7）根据目标设备调整应用资源</h3>
<p>为尽可能高效地被加载，需要根据具体设备的配置调整相应资源，尤其是图片资源。为使应用包文件合理适用不同设备，首先可只添加运行该应用需要的核心资源，然后再根据具体设备下载相关内容。</p>
<h3>8）使用Hierachy Viewer可视化调试工具</h3>
<p>Hierachy Viewer能很方便地在开发者设计，调试和调整界面时，快速定位问题，解决问题，提高开发效率。</p>
<h3>9）使用layoutopt进行布局优化</h3>
<p>Layoutopt是一款简单的命令行工具，可帮助找到不必要的控件嵌套以及缩减布局资源，从而使应用变得可能“苗条”。控件越少、布局层次越浅，性能就越好。</p>
<h3>10）使用Traceview及其他Android工具进行分析</h3>
<p>Android SDK随带了很多用于应用分析的工具，其中最受欢迎的是Traceview，这款图形工具可以帮助调试和找到应用中的性能瓶颈。</p>
<p>结语</p>
<p>提升Android应用性能的方法有很多，有些需要使用特定的算法，有些依赖切实可行的调试和性能监测技术。幸运的是，Android平台随带了众多免费的实用工具，可以帮助跟踪和解决应用程序中的性能问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/android%e5%bc%80%e5%8f%91%e7%bb%8f%e9%aa%8c%e7%a7%af%e7%b4%af.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>资源库</title>
		<link>http://bg135.com/resource_library.html</link>
		<comments>http://bg135.com/resource_library.html#comments</comments>
		<pubDate>Tue, 27 Sep 2011 02:25:09 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[资源共享]]></category>
		<category><![CDATA[powermanager]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=220</guid>
		<description><![CDATA[各类资源共享 文章整理 android linux sqlite java]]></description>
			<content:encoded><![CDATA[<h2>Android</h2>
<h2 style="padding-left: 30px;"><a href="http://www.cnblogs.com/keyindex/articles/1819504.html" target="_blank">android的PowerManager和PowerManager.WakeLock</a></h2>
<h2 style="padding-left: 30px;"><a id="ctl01_TitleUrl" href="http://www.cnblogs.com/playing/archive/2011/04/23/2025419.html" target="_blank">Android Google Map API 开发基础知识</a></h2>
<h2 style="padding-left: 30px;"><a id="ctl02_TitleUrl" href="http://www.cnblogs.com/gyflyx/archive/2011/07/09/2101912.html">Android &#8212;spannableStringBuilde</a></h2>
<h4 style="padding-left: 150px;">——在文字中夹杂一些图片，比如QQ中使用表情图片，以及如何将文字高亮显示等等</h4>
<h2 style="padding-left: 30px;"><a id="ctl02_TitleUrl" title="android屏幕截图" href="http://www.cnblogs.com/-OYK/archive/2011/05/31/2064775.html" target="_blank">android屏幕截图</a></h2>
<h2 style="padding-left: 30px;"><a href="http://www.iteye.com/topic/1097916">Android:将View的内容映射成Bitmap</a></h2>
<h2 style="padding-left: 30px;"><a href="http://www.iteye.com/topic/1098839">Android:getDrawingCache() = null的解决方法</a></h2>
<h2 style="padding-left: 30px;"><a rel="bookmark" href="http://www.pocketdigi.com/20111010/529.html" target="_blank">Android 调用指定的程序打开指定文件</a></h2>
<h2 style="padding-left: 30px;"><a rel="bookmark" href="http://www.pocketdigi.com/20110906/464.html" target="_blank">SurfaceView的使用 自制视频播放器</a></h2>
<h2 style="padding-left: 30px;"><a rel="bookmark" href="http://www.pocketdigi.com/20110719/408.html" target="_blank">Android 获取root权限 执行Linux命令 检测是否有ROOT权限</a></h2>
<h2 style="padding-left: 30px;"><a rel="bookmark" href="http://www.pocketdigi.com/20110718/399.html" target="_blank">Android在程序中捕捉HOME键的方法</a></h2>
<h2 style="padding-left: 30px;">A<a href="http://stephen830.iteye.com/blog/1141532">ndroid listview 连续调用 getview问题分析及解决</a></h2>
<h2 style="padding-left: 30px;"><a href="http://hi.baidu.com/blogofivan/blog/item/e572728206260ec39123d925.html">Android ListView反复调用getView和getCount</a></h2>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/resource_library.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>android contacts</title>
		<link>http://bg135.com/android-contacts.html</link>
		<comments>http://bg135.com/android-contacts.html#comments</comments>
		<pubDate>Sat, 17 Sep 2011 04:52:25 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[contacts]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=219</guid>
		<description><![CDATA[android contacts insert delete update query]]></description>
			<content:encoded><![CDATA[<p>本章和大家分享的是手机通讯录方面的经验，需求背景:对手机通讯录进行增、删、改、查，或者有时候App需要与手机 contacts进行交互或共享contacts。</p>
<p>我们都知道android系统没有完全将contacts向developer开放，开发人员只能通过android提供的有限的Api对contacts进行操作，我们只能通过ContentProvider对contacts的数据进行增、删、改、查等操作，并不能改变到其表结构。</p>
<p>首先，我们来看下如何通过ContentProvider查找contacts，以下我以查找其姓、名、公司、家庭电话、工作电话、移动电话、传真、邮箱、城市、省份、国家、出生年月为例：</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 取得family_name和given_name
	 */</span>
	<span style="color: #003399;">Cursor</span> nameCur<span style="color: #339933;">=</span>cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>Data.<span style="color: #006633;">CONTACT_ID</span>, StructuredName.<span style="color: #006633;">DISPLAY_NAME</span>, StructuredName.<span style="color: #006633;">FAMILY_NAME</span>, StructuredName.<span style="color: #006633;">GIVEN_NAME</span>, StructuredName.<span style="color: #006633;">MIDDLE_NAME</span><span style="color: #009900;">&#125;</span>,
			 ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;='&quot;</span> <span style="color: #339933;">+</span>  ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id<span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nameCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		firstName<span style="color: #339933;">=</span>nameCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>nameCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">FAMILY_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		lastName<span style="color: #339933;">=</span>nameCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>nameCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">GIVEN_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	nameCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003399;">Cursor</span> companyCur<span style="color: #339933;">=</span>cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>CommonDataKinds.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">COMPANY</span><span style="color: #009900;">&#125;</span>,
			 ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;='&quot;</span> <span style="color: #339933;">+</span>  ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id<span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>companyCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			company<span style="color: #339933;">=</span>companyCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>companyCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>CommonDataKinds.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">COMPANY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	companyCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得HomePhone号码</span>
	<span style="color: #003399;">Cursor</span> homePhoneCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_HOME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>homePhoneCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		homePhone<span style="color: #339933;">=</span>homePhoneCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>homePhoneCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	homePhoneCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得WorkPhone号码</span>
	<span style="color: #003399;">Cursor</span> workPhoneCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>workPhoneCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		workPhone<span style="color: #339933;">=</span>workPhoneCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>workPhoneCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	workPhoneCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得MobilePhone号码</span>
	<span style="color: #003399;">Cursor</span> mobilePhoneCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_MOBILE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mobilePhoneCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		mobilePhone<span style="color: #339933;">=</span>mobilePhoneCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>mobilePhoneCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	mobilePhoneCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得MobilePhone号码</span>
	<span style="color: #003399;">Cursor</span> faxPhoneCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_FAX_HOME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>faxPhoneCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		faxPhone<span style="color: #339933;">=</span>faxPhoneCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>faxPhoneCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	faxPhoneCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得MobilePhone号码</span>
	<span style="color: #003399;">Cursor</span> emailCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">DATA</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emailCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		email<span style="color: #339933;">=</span>emailCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>emailCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">DATA</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	emailCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得MobilePhone号码</span>
	<span style="color: #003399;">Cursor</span> addressCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CITY</span>,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">REGION</span>,
			ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">COUNTRY</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
        <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>addressCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		city<span style="color: #339933;">=</span>addressCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>addressCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CITY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		state<span style="color: #339933;">=</span>addressCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>addressCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">REGION</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		country<span style="color: #339933;">=</span>addressCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>addressCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">COUNTRY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	addressCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 根据contact_ID取得MobilePhone号码</span>
	<span style="color: #003399;">Cursor</span> birthdayCur <span style="color: #339933;">=</span> cr.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span>,
			<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">DATA</span><span style="color: #009900;">&#125;</span>,
        ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; AND &quot;</span>
          <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=? &quot;</span>,
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">TYPE_BIRTHDAY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>birthdayCur.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		birthday<span style="color: #339933;">=</span>birthdayCur.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>birthdayCur.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">DATA</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	birthdayCur.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>以上只是查找了contacts的部分字段，同鞋们可以自行拓展，比如说查找其IM,Organization,Nickname,WebSite等等.</p>
<p>得到contacts联系人之后，我们可以对其做相应的修改，修改完成后，我们要将数据更新到android contacts数据库中，以下就是更新的范例:</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  	<span style="color: #003399;">ArrayList</span> ops <span style="color: #339933;">=</span>
	          <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// 更新family_name和given_name</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">FAMILY_NAME</span>, firstN<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">GIVEN_NAME</span>, lastN<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #666666; font-style: italic;">// 更新comany</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                       .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">COMPANY</span>, com<span style="color: #009900;">&#41;</span>
                       .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #666666; font-style: italic;">// 更新homePhone</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_HOME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                      .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>, hp<span style="color: #009900;">&#41;</span>
                      .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #666666; font-style: italic;">// 更新workPhone</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                     .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>,wp<span style="color: #009900;">&#41;</span>
                     .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// 更新mobilePhone</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_MOBILE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>,mp<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// 更新faxPhone</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_FAX_HOME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>,fp<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// 更新email</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                   .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">DATA</span>,em<span style="color: #009900;">&#41;</span>
                   .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// 更新city,state,country</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">TYPE_HOME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CITY</span>, city<span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">REGION</span>, state<span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">COUNTRY</span>,cou<span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// 更新birthday</span>
	 ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newUpdate</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
	          .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTACT_ID</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; AND &quot;</span>
	        		  <span style="color: #339933;">+</span> ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">TYPE</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=?&quot;</span>,
	        		  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>android_id,ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span>,<span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">TYPE_BIRTHDAY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                 .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">DATA</span>, bir<span style="color: #009900;">&#41;</span>
                 .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000000; font-weight: bold;">try</span>
     <span style="color: #009900;">&#123;</span>
         ContentProviderResult<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> res <span style="color: #339933;">=</span> context.<span style="color: #006633;">getContentResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">applyBatch</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">AUTHORITY</span>, ops<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//  Log.i(&quot;contact&quot;, &quot;android_id=&quot;+android_id+&quot;,firstName=&quot;+firstName+&quot;,lastName=&quot;+lastName);</span>
	<span style="color: #666666; font-style: italic;">//  appState.db.insertContactToTag(tagId,String.valueOf(contactId));</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">RemoteException</span> e<span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// error</span>
     <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>OperationApplicationException e<span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// error</span>
     <span style="color: #009900;">&#125;</span></pre></div></div>

<p>同样以上更新也仅是对contact的部分字段进行了更新，我相信聪明的你，仔细阅读，细细推敲一番，定能找到其中的规律，从而对犹如IM,Nickname,Event,Organization等字段进行更新。</p>
<p>以上只是对已经存在的contacts进行操作，那么我们一开始该如何向里面插入一条数据呢，以下代码是实现向数据库中插入一条数据的操作。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #003399;">ArrayList</span> ops <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> rawContactInsertIndex <span style="color: #339933;">=</span> ops.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">RawContacts</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">RawContacts</span>.<span style="color: #006633;">ACCOUNT_TYPE</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">RawContacts</span>.<span style="color: #006633;">ACCOUNT_NAME</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Names</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>firstN <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> lastN<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">DISPLAY_NAME</span>, firstN<span style="color: #339933;">+</span>lastN<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">FAMILY_NAME</span>, firstN<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredName</span>.<span style="color: #006633;">GIVEN_NAME</span>, lastN<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>  
&nbsp;
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Organization</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>com<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">COMPANY</span>, com<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE</span>, ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Organization</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Home Numbers</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>hp<span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>, hp<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_HOME</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Work Numbers</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>wp <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>, wp<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Mobile Number</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mp <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>, mp<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_MOBILE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Fax Number</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>fp <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">NUMBER</span>, fp<span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE</span>,
                            ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Phone</span>.<span style="color: #006633;">TYPE_FAX_HOME</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Email</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>em <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
             ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                                ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">DATA</span>, em<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">TYPE</span>, ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">Email</span>.<span style="color: #006633;">TYPE_WORK</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ City,state,country</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>city <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
             ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                                ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">CITY</span>, city<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">REGION</span>, state<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">COUNTRY</span>,cou<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">TYPE</span>,
                        		ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #006633;">StructuredPostal</span>.<span style="color: #006633;">TYPE_HOME</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//------------------------------------------------------ Birthday</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>bir <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
             ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newInsert</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValueBackReference</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">RAW_CONTACT_ID</span>, rawContactInsertIndex<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">Data</span>.<span style="color: #006633;">MIMETYPE</span>,
                                ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">CONTENT_ITEM_TYPE</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">DATA</span>, bir<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">withValue</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">TYPE</span>,
                        		ContactsContract.<span style="color: #006633;">CommonDataKinds</span>.<span style="color: #003399;">Event</span>.<span style="color: #006633;">TYPE_BIRTHDAY</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            ContentProviderResult<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> res <span style="color: #339933;">=</span> context.<span style="color: #006633;">getContentResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">applyBatch</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">AUTHORITY</span>, ops<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">return</span> res<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">RemoteException</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// error</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>OperationApplicationException e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// error</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>实现以上代码便可将一条数据插入到contacts数据库中，同样，你也可以插入其他的字段，而且你也可以细细研究下ContentProviderResult[]，看看进行这些操作后，其返回的结果都是什么。</p>
<p>以上实现了对android contacts 增、删、改、查中的三种，下面的代码就是实现删的，删除一个contacts相对来说比较简单些。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">       <span style="color: #003399;">String</span> where <span style="color: #339933;">=</span> ContactsContract.<span style="color: #006633;">Data</span>._ID <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; = ? &quot;</span><span style="color: #339933;">;</span>
    	<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> params <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>android_id<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">ArrayList</span> ops <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ops.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ContentProviderOperation.<span style="color: #006633;">newDelete</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">RawContacts</span>.<span style="color: #006633;">CONTENT_URI</span><span style="color: #009900;">&#41;</span>
    	        .<span style="color: #006633;">withSelection</span><span style="color: #009900;">&#40;</span>where, params<span style="color: #009900;">&#41;</span>
    	        .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		cr.<span style="color: #006633;">applyBatch</span><span style="color: #009900;">&#40;</span>ContactsContract.<span style="color: #006633;">AUTHORITY</span>, ops<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">RemoteException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
		e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>OperationApplicationException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated catch block</span>
		e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>删除一个contacts有很多的选择，你可以只删除电话这一块，也可以删除联系人信息这一块，而且你不仅可以将id做作为条件进行删除，你也可以将联系电话做为条件进行删除，你也还可以以用户名做为条件进行删除。</p>
<p>&nbsp;</p>
<p>想要更详细的了解通讯录的数据库内部结构及操作请访问:<a href="http://blog.csdn.net/xys289187120/article/details/6730957" target="_blank">http://blog.csdn.net/xys289187120/article/details/6730957</a></p>
<p>以上代码便可以实现对android contacts的增、删、改、查等操作。最后不要忘了赋予APP 对contacts读写的权限：</p>
<p>&nbsp;</p>
<p style="text-align: center;">Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.</p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/android-contacts.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C2DM(Android Cloud to Device Messaging)</title>
		<link>http://bg135.com/c2dm-android-cloud-to-device-messaging.html</link>
		<comments>http://bg135.com/c2dm-android-cloud-to-device-messaging.html#comments</comments>
		<pubDate>Sat, 20 Aug 2011 07:20:27 +0000</pubDate>
		<dc:creator>root</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=209</guid>
		<description><![CDATA[Android Cloud to Device Messaging (C2DM) is a service that helps developers send data from servers to their applications on Android devices. The service provides a simple, lightweight mechanism that servers can use to tell mobile applications to contact the &#8230;]]></description>
			<content:encoded><![CDATA[<p>Android Cloud to Device Messaging (C2DM) is  a   service that helps developers  send data from servers to their applications on  Android devices. The  service provides a simple, lightweight mechanism that servers can use  to tell mobile applications to contact the server directly, to fetch  updated application or user data. The C2DM service handles all aspects  of queueing of messages and delivery to the target application running  on the target device.</p>
<p>这个是C2DM官方对其的一个解释，自我感觉英文不错的童鞋可以去<a href="http://code.google.com/intl/zh-CN/android/c2dm/">http://code.google.com/intl/zh-CN/android/c2dm/</a>仔细研读一番，这里面包括了对C2DM的介绍以及实现步骤和具体实现的部分代码。当然了如果你对英文很头疼，也或懒得去看鸟语，那就请看小弟下面的心得体会吧，先预言，以下方法在本人开环境中测试是ok的。</p>
<p>按小编的理解，C2DM就是通过Server向mobile device发送message,并且获得message后由applications，值得注意的是发送的message是限制长度的字符串，它适用于传送间断的消息，或是告诉applications去进行某些操作。</p>
<p>首先我们要有一个邮箱，最好是google mail(Gmail)，至于有什么用，后面的步骤会提及到。</p>
<p>其次我们要去注册(Sign Up for Android Cloud to Device Messaging),注册的网址:<a href="http://code.google.com/intl/zh-CN/android/c2dm/signup.html">http://code.google.com/intl/zh-CN/android/c2dm/signup.html</a>，其中有两项:Contact email,Role (sender) account email是必填的，而且都是输入email，Contact email是google与你联系的方式，可以随意写入一个email address.比如说当你注册成功后，google会发送一个email给你，告诉你恭喜你。。。。。.Role (sender) account email则是作为Google Account ID来发送message到C2DM的，这个email最好唯一，而且应该是我们第一步注册的那个邮箱(Gmail)。注册成功后会获得一串:APA91bENnaUdTRJTyDX45BE33Ta0tQUwwv2gkC4xDqG3YX7GR76qs-epWvXDfEErkZb_osAKBBbbPN_7QPKXxRLF1RREj1eSF1vmVr4MvkEgmWZQu9IrHx0 。将其保存好，传给local server，local server会用它来和Google server进行验证、通讯。</p>
<p>&nbsp;</p>
<p>到这里可以说是万事具备，只欠东风了，接下来我们要在application中注册一个广播，用于接收local server所发送过来的message，</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>receiver android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.gigatalk.service.C2DMReceiver&quot;</span> android<span style="color: #339933;">:</span>permission<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.permission.SEND&quot;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;!--</span> Receive the actual message <span style="color: #339933;">--&gt;</span>
          <span style="color: #339933;">&lt;</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span>
              <span style="color: #339933;">&lt;</span>action android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span> <span style="color: #339933;">/&gt;</span>
              <span style="color: #339933;">&lt;</span>category android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.gigatalk&quot;</span> <span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;/</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;!--</span> Receive the registration id <span style="color: #339933;">--&gt;</span>
          <span style="color: #339933;">&lt;</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span>
              <span style="color: #339933;">&lt;</span>action android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span> <span style="color: #339933;">/&gt;</span>
              <span style="color: #339933;">&lt;</span>category android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.gigatalk&quot;</span> <span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;/</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>receiver<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>注册好了广播之后，我们就开始静静的等待接收广播，那么我们要先定义一个广播接收器，用来接收广播和对广播进行处理，</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Broadcast receiver that handles Android Cloud to Data Messaging (AC2DM) messages, initiated
 * by the JumpNote App Engine server and routed/delivered by Google AC2DM servers. The
 * only currently defined message is 'sync'.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2DMReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        handleRegistration<span style="color: #009900;">&#40;</span>context, intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        handleMessage<span style="color: #009900;">&#40;</span>context, intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> handleRegistration<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003399;">String</span> registration <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;registration_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        <span style="color: #666666; font-style: italic;">// Registration failed, should try again later.</span>
	    	Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c2dm&quot;</span>,<span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unregistered&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        <span style="color: #666666; font-style: italic;">// unregistration done, new messages from the authorized sender will be rejected</span>
	    	Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c2dm&quot;</span>,<span style="color: #0000ff;">&quot;unregistered&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>registration <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	       <span style="color: #666666; font-style: italic;">// Send the registration ID to the 3rd party site that is sending the messages.</span>
	       <span style="color: #666666; font-style: italic;">// This should be done in a separate thread.</span>
	       <span style="color: #666666; font-style: italic;">// When done, remember that all registration is done. </span>
	    	Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c2dm&quot;</span>,registration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> handleMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	    Bundle extras <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>extras <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        	<span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c2dm&quot;</span>, message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>在这里你可以接收到注册时的信息，以及接收到得广播信息，并且你可以完全自定义的对其进行相应的处理。</p>
<p>当然请务必不要忘记，在接收、处理广播之前，我们要事先向C2DM服务器注册，说明接收谁发送过来的message以及由谁来接收。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//向C2DM服务器注册         </span>
           Intent registrationIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTER&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            registrationIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;app&quot;</span>, PendingIntent.<span style="color: #006633;">getBroadcast</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// boilerplate</span>
            registrationIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sender&quot;</span>, <span style="color: #0000ff;">&quot;houjiankai@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            startService<span style="color: #009900;">&#40;</span>registrationIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</pre>
<p>&nbsp;</p>
<p>其中<span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: 12px; line-height: 18px; white-space: pre;">sender就是之前第二步注册的时候注册的那个Role (sender) account email。</span></p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/c2dm-android-cloud-to-device-messaging.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PopupWindow</title>
		<link>http://bg135.com/popupwindow.html</link>
		<comments>http://bg135.com/popupwindow.html#comments</comments>
		<pubDate>Sat, 20 Aug 2011 06:31:03 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[技术大类]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=205</guid>
		<description><![CDATA[PopupWindow有点类似于Dialog，相同点在于都是弹出窗口，并且都可以对其进行自定义显示，并且里面的监听组件，进行相应的操作，但它与Dialog又有很大的区别，PopupWindow只是弹出窗口，不会使宿主Activity组件失去焦点，也就是说PopupWindow弹出后，你仍可以与宿主Activity进行交互，Dialog却不能做到这一点。 &#160; 本章我将介绍的是一个自定义的PopupWindow，它实现的效果是固定显示在一定大小的区域内，并根据点击按钮的位置来显示箭头，用于指示当前选中的按钮，并自定义其布局，对布局中组件进行相应的监听，相应相应的操作,而且自定义其弹出和淡去的动画效果，弹出:自左向右滑动淡入，淡出:自右向左淡出。效果图如下: 图1： &#160; 图2 如图，当点击左侧的的按钮时，其箭头指向当前按钮，并点击相应的按钮color、Sort、Save、Cancel、Delete等响应响应的动作。以下是实现的部分核心代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 &#8230;]]></description>
			<content:encoded><![CDATA[<p>PopupWindow有点类似于Dialog，相同点在于都是弹出窗口，并且都可以对其进行自定义显示，并且里面的监听组件，进行相应的操作，但它与Dialog又有很大的区别，PopupWindow只是弹出窗口，不会使宿主Activity组件失去焦点，也就是说PopupWindow弹出后，你仍可以与宿主Activity进行交互，Dialog却不能做到这一点。</p>
<p>&nbsp;</p>
<p>本章我将介绍的是一个自定义的PopupWindow，它实现的效果是固定显示在一定大小的区域内，并根据点击按钮的位置来显示箭头，用于指示当前选中的按钮，并自定义其布局，对布局中组件进行相应的监听，相应相应的操作,而且自定义其弹出和淡去的动画效果，弹出:自左向右滑动淡入，淡出:自右向左淡出。效果图如下:</p>
<p>图1：</p>
<p><a rel="attachment wp-att-206" href="http://bg135.com/popupwindow.html/device-2011-08-20-141252"><img class="alignnone size-full wp-image-206" title="popupwindow" src="http://bg135.com/wp-content/uploads/2011/08/device-2011-08-20-141252.png" alt="" width="480" height="800" /></a></p>
<p>&nbsp;</p>
<p>图2</p>
<p><a rel="attachment wp-att-207" href="http://bg135.com/popupwindow.html/device-2011-08-20-142445"><img class="alignnone size-full wp-image-207" title="popupwindow two" src="http://bg135.com/wp-content/uploads/2011/08/device-2011-08-20-142445.png" alt="" width="480" height="800" /></a></p>
<p>如图，当点击左侧的的按钮时，其箭头指向当前按钮，并点击相应的按钮color、Sort、Save、Cancel、Delete等响应响应的动作。以下是实现的部分核心代码:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">       <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> QuickActionWindow <span style="color: #000000; font-weight: bold;">extends</span> PopupWindow <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">Callback</span>,OnClickListener,OnColorChangedListener<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> THIS_FILE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;QuickActionWindow&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Context</span> mContext<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> LayoutInflater mInflater<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> WindowManager mWindowManager<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003399;">View</span> contentView<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> mScreenWidth<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> mScreenHeight<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> mShadowHoriz,nine,Hundred<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//	private int mShadowVert;</span>
<span style="color: #666666; font-style: italic;">//	private int mShadowTouch;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ImageView mArrowUp<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> ImageView mArrowDown<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> EditText catName<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Button</span> yesButton,cancelButton,deleteButton,colorButton,sortButton<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> marginTop<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> colorGiga<span style="color: #339933;">=</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Contants appState<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> nameStr,tag_str<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> popUpWidth,popUpHeight<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * private View mHeader; private HorizontalScrollView mTrackScroll;
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> ViewGroup mTrack<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * private View mFooter; private View mFooterDisambig; private ListView
	 * mResolveList; private CheckBox mSetPrimaryCheckBox;
	 */</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">View</span> mPView<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Rect mAnchor,listAnchor<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// context为传入的上下文,pView为对应的父级得视图</span>
	<span style="color: #000000; font-weight: bold;">public</span> QuickActionWindow<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, <span style="color: #003399;">View</span> pView,<span style="color: #003399;">String</span> name,Rect rect,Rect listRect<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		mPView <span style="color: #339933;">=</span> pView<span style="color: #339933;">;</span>
		nameStr<span style="color: #339933;">=</span>name<span style="color: #339933;">;</span>
&nbsp;
		mAnchor <span style="color: #339933;">=</span> rect<span style="color: #339933;">;</span>
		listAnchor<span style="color: #339933;">=</span>listRect<span style="color: #339933;">;</span>
&nbsp;
		marginTop<span style="color: #339933;">=</span>listAnchor.<span style="color: #006633;">top</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>nameStr.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// 如果传入的名称不为空,则为修改Tag</span>
			tag_str<span style="color: #339933;">=</span>mPView.<span style="color: #006633;">getTag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		mContext <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
		appState<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>Contants<span style="color: #009900;">&#41;</span> mContext.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		mWindowManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>WindowManager<span style="color: #009900;">&#41;</span> mContext.<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">WINDOW_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		mInflater <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Activity<span style="color: #009900;">&#41;</span> mContext<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLayoutInflater</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">quickaction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		mScreenWidth <span style="color: #339933;">=</span> mWindowManager.<span style="color: #006633;">getDefaultDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//获取屏幕的宽度</span>
		mScreenHeight <span style="color: #339933;">=</span> mWindowManager.<span style="color: #006633;">getDefaultDisplay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		setWindowLayoutMode<span style="color: #009900;">&#40;</span>ViewGroup.<span style="color: #006633;">LayoutParams</span>.<span style="color: #006633;">FILL_PARENT</span>, ViewGroup.<span style="color: #006633;">LayoutParams</span>.<span style="color: #006633;">FILL_PARENT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">final</span> Resources res <span style="color: #339933;">=</span> mContext.<span style="color: #006633;">getResources</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		mShadowHoriz <span style="color: #339933;">=</span> res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">quickaction_shadow_horiz</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 0dip</span>
		nine<span style="color: #339933;">=</span>res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">quickaction_nine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 9dip</span>
		Hundred<span style="color: #339933;">=</span>res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">quickaction_one_Hundred</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 100dip</span>
&nbsp;
		setWidth<span style="color: #009900;">&#40;</span>mScreenWidth<span style="color: #339933;">-</span>listAnchor.<span style="color: #006633;">left</span><span style="color: #339933;">+</span>nine<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 设置PopupWindow的宽度</span>
		setHeight<span style="color: #009900;">&#40;</span>listAnchor.<span style="color: #006633;">bottom</span><span style="color: #339933;">-</span>listAnchor.<span style="color: #006633;">top</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		setBackgroundDrawable<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ColorDrawable<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		mArrowUp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ImageView<span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">arrow_up</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		mArrowDown <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ImageView<span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">arrow_down</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		mTrack <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ViewGroup<span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">quickaction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//中间</span>
&nbsp;
		catName<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Tag名称</span>
		catName.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>nameStr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//		catName.setText(title);</span>
    	yesButton <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_save</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Save Button</span>
    	yesButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	cancelButton <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_cancel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Cancel Button</span>
    	cancelButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	deleteButton<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_delete</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Delete Button</span>
    	deleteButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>tag_str.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span>tag_str.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> nameStr.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">// 如果tag_id为1或2,则表明为All、Favorite</span>
    		deleteButton.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">GONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
    		deleteButton.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">VISIBLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
    	colorButton<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Color Button</span>
    	colorButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	sortButton<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> contentView.<span style="color: #006633;">findViewById</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_sort</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Sort Button</span>
    	sortButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nameStr.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// 如果传入的tag名称为空,则表明为新建Tag</span>
    		sortButton.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">GONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
    		sortButton.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">VISIBLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">/*
		 * mTrackScroll = (HorizontalScrollView)
		 * contentView.findViewById(R.id.scroll);
		 *
		 * mFooter = contentView.findViewById(R.id.footer); mFooterDisambig =
		 * contentView.findViewById(R.id.footer_disambig); mResolveList =
		 * (ListView) contentView.findViewById(android.R.id.list);
		 * mSetPrimaryCheckBox = (CheckBox)
		 * contentView.findViewById(android.R.id.checkbox);
		 */</span>
&nbsp;
		setFocusable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//是否可以获取焦点</span>
		setTouchable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 是否可以触摸</span>
		setOutsideTouchable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setContentView<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> resId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		contentView <span style="color: #339933;">=</span> mInflater.<span style="color: #006633;">inflate</span><span style="color: #009900;">&#40;</span>resId, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setContentView</span><span style="color: #009900;">&#40;</span>contentView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Show the correct call-out arrow based on a {@link R.id} reference.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> showArrow<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> whichArrow, <span style="color: #000066; font-weight: bold;">int</span> requestedX<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">View</span> showArrow <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>whichArrow <span style="color: #339933;">==</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">arrow_up</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> mArrowUp <span style="color: #339933;">:</span> mArrowDown<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">View</span> hideArrow <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>whichArrow <span style="color: #339933;">==</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">arrow_up</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> mArrowDown <span style="color: #339933;">:</span> mArrowUp<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Dirty hack to get width, might cause memory leak</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> arrowHeight <span style="color: #339933;">=</span> mContext.<span style="color: #006633;">getResources</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDrawable</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">quickaction_arrow_up</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getIntrinsicHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		showArrow.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">VISIBLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ViewGroup.<span style="color: #006633;">MarginLayoutParams</span> param <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ViewGroup.<span style="color: #006633;">MarginLayoutParams</span><span style="color: #009900;">&#41;</span> showArrow.<span style="color: #006633;">getLayoutParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		param.<span style="color: #006633;">topMargin</span> <span style="color: #339933;">=</span> requestedX <span style="color: #339933;">-</span> arrowHeight<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//  设置箭头的 margin_top,它的值为 Button的中心点的y - 箭头高度二分之一</span>
		<span style="color: #666666; font-style: italic;">// Log.d(&quot;QuickActionWindow&quot;,</span>
		<span style="color: #666666; font-style: italic;">// &quot;ArrowWidth: &quot;+arrowWidth+&quot;; LeftMargin for Arrow: &quot;+param.leftMargin);</span>
&nbsp;
		hideArrow.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">INVISIBLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onKeyUp<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> keyCode, <span style="color: #003399;">KeyEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>keyCode <span style="color: #339933;">==</span> <span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">KEYCODE_BACK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			onBackPressed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> onBackPressed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		dismiss<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onKeyDown<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> keyCode, <span style="color: #003399;">KeyEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onKeyMultiple<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> keyCode, <span style="color: #000066; font-weight: bold;">int</span> count, <span style="color: #003399;">KeyEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> show<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		show<span style="color: #009900;">&#40;</span>mAnchor.<span style="color: #006633;">centerY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> show<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> requestedX<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">// 传入Button的中心点的y方向坐标</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mAnchor <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">showAtLocation</span><span style="color: #009900;">&#40;</span>mPView, Gravity.<span style="color: #006633;">NO_GRAVITY</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Calculate properly to position the popup the correctly based on</span>
		<span style="color: #666666; font-style: italic;">// height of popup</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isShowing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> x, y, windowAnimations<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getContentView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">measure</span><span style="color: #009900;">&#40;</span>ViewGroup.<span style="color: #006633;">LayoutParams</span>.<span style="color: #006633;">FILL_PARENT</span>, ViewGroup.<span style="color: #006633;">LayoutParams</span>.<span style="color: #006633;">FILL_PARENT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 设置popUp的content的width 和height ?</span>
			<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> blockWidth <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getContentView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMeasuredWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 取得popUp的width</span>
&nbsp;
			y <span style="color: #339933;">=</span> mShadowHoriz<span style="color: #339933;">+</span>marginTop<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 0</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mAnchor.<span style="color: #006633;">left</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> blockWidth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//如果当前按钮的上方有足够的空间可以放下PopupWindow时,则将PopUpWindow放在上方。</span>
				<span style="color: #666666; font-style: italic;">// Show downwards callout when enough room, aligning bottom</span>
				<span style="color: #666666; font-style: italic;">// block</span>
				<span style="color: #666666; font-style: italic;">// edge with top of anchor area, and adjusting to inset arrow.</span>
				showArrow<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">arrow_down</span>, requestedX<span style="color: #339933;">-</span>marginTop<span style="color: #339933;">+</span>mShadowHoriz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				x <span style="color: #339933;">=</span> mAnchor.<span style="color: #006633;">left</span> <span style="color: #339933;">-</span> blockWidth<span style="color: #339933;">+</span>nine<span style="color: #339933;">;</span>
				windowAnimations <span style="color: #339933;">=</span> R.<span style="color: #006633;">style</span>.<span style="color: #006633;">QuickActionRightAnimation</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// Otherwise show upwards callout, aligning block top with</span>
				<span style="color: #666666; font-style: italic;">// bottom of</span>
				<span style="color: #666666; font-style: italic;">// anchor area, and adjusting to inset arrow.</span>
				showArrow<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">arrow_up</span>, requestedX<span style="color: #339933;">-</span>marginTop<span style="color: #339933;">+</span>mShadowHoriz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//显示箭头的位置</span>
				x <span style="color: #339933;">=</span> mAnchor.<span style="color: #006633;">right</span><span style="color: #339933;">-</span>nine<span style="color: #339933;">;</span>
				windowAnimations <span style="color: #339933;">=</span> R.<span style="color: #006633;">style</span>.<span style="color: #006633;">QuickActionLeftAnimation</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			setAnimationStyle<span style="color: #009900;">&#40;</span>windowAnimations<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;selector&quot;</span>, x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;--&quot;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>x, y, <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onKeyLongPress<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> keyCode, <span style="color: #003399;">KeyEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeAllItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		mTrack.<span style="color: #006633;">removeViews</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, mTrack.<span style="color: #006633;">getChildCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
		<span style="color: #000066; font-weight: bold;">int</span> view_id <span style="color: #339933;">=</span> v.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>view_id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_save</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					ContentValues values<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> ContentValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>tagsInfo.<span style="color: #006633;">TAG_NAME</span>,catName.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>tagsInfo.<span style="color: #006633;">TAG_COLOUR</span>,colorGiga<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>nameStr.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// 如果传入的title为空,则为新增加Tag</span>
						<span style="color: #000066; font-weight: bold;">int</span> orderId<span style="color: #339933;">=</span>appState.<span style="color: #006633;">prefsDevice</span>.<span style="color: #006633;">getPreferenceIntValue</span><span style="color: #009900;">&#40;</span>DBAdapter.<span style="color: #006633;">TAG_COUNT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						values.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>tagsInfo.<span style="color: #006633;">TAG_ORDER_INDEX</span>,orderId<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						appState.<span style="color: #006633;">db</span>.<span style="color: #006633;">insertTag</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						appState.<span style="color: #006633;">prefsDevice</span>.<span style="color: #006633;">setPreferenceFloatValue</span><span style="color: #009900;">&#40;</span>DBAdapter.<span style="color: #006633;">TAG_COUNT</span>, orderId<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// 如果传入的title不为空,则为修改tag</span>
						appState.<span style="color: #006633;">db</span>.<span style="color: #006633;">updateTag</span><span style="color: #009900;">&#40;</span>values,tag_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					Intent publishIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>SipManager.<span style="color: #006633;">ACTION_GIGATALK_TAG_CHANGED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					mContext.<span style="color: #006633;">sendBroadcast</span><span style="color: #009900;">&#40;</span>publishIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dismiss</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_cancel</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dismiss</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_delete</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					appState.<span style="color: #006633;">db</span>.<span style="color: #006633;">deleteTagById</span><span style="color: #009900;">&#40;</span>tag_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					Intent publishIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>SipManager.<span style="color: #006633;">ACTION_GIGATALK_TAG_CHANGED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					mContext.<span style="color: #006633;">sendBroadcast</span><span style="color: #009900;">&#40;</span>publishIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dismiss</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_color</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">new</span> ColorPickerDialog<span style="color: #009900;">&#40;</span>mContext,<span style="color: #000000; font-weight: bold;">this</span>,<span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">case</span> R.<span style="color: #006633;">id</span>.<span style="color: #006633;">cat_sort</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
					Intent intent<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>mContext,DragDemo.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					mContext.<span style="color: #006633;">startActivity</span><span style="color: #009900;">&#40;</span>intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dismiss</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 回调函数，获取选择颜色
	 */</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> colorChanged<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> color<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
		colorGiga<span style="color: #339933;">=</span>color<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>如上代码关键在于继承了PopupWindow，难点在于确定箭头和PopupWindow的位置，以及PopupWindow的大小和布局。使用方法是实现该类，并传入相应的参数:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">                <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> xy <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		v.<span style="color: #006633;">getLocationInWindow</span><span style="color: #009900;">&#40;</span>xy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Rect r <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Rect<span style="color: #009900;">&#40;</span>xy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>, xy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>, xy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> v.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, xy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> v.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> listxy<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		searchList.<span style="color: #006633;">getLocationInWindow</span><span style="color: #009900;">&#40;</span>listxy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Rect listr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Rect<span style="color: #009900;">&#40;</span>listxy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>, listxy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>, listxy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> searchList.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, listxy<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> searchList.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		QuickActionWindow quickAction <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> QuickActionWindow<span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,v,title,r,listr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		quickAction.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/popupwindow.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Custom Button</title>
		<link>http://bg135.com/custom-button.html</link>
		<comments>http://bg135.com/custom-button.html#comments</comments>
		<pubDate>Sat, 20 Aug 2011 06:10:09 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=202</guid>
		<description><![CDATA[本章我将和大家一起分享的是Custom Button（自定义组件）,实现的效果是组件颜色要有渐变，看起来有种立体的感觉，当组件没有被点击时，其字体颜色是黑色，但组件被点击时，其背景色变为白色，且字体颜色变为黑色，效果图如下: 图1： &#160; 图2： &#160; 图中左侧颜色各异的按钮就是本章要实现的Custom button,下面是实现的部分核心代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 &#8230;]]></description>
			<content:encoded><![CDATA[<p>本章我将和大家一起分享的是Custom Button（自定义组件）,实现的效果是组件颜色要有渐变，看起来有种立体的感觉，当组件没有被点击时，其字体颜色是黑色，但组件被点击时，其背景色变为白色，且字体颜色变为黑色，效果图如下:</p>
<p>图1：</p>
<p><a rel="attachment wp-att-203" href="http://bg135.com/custom-button.html/device-2011-08-20-134857"><img class="alignnone size-full wp-image-203" title="custom button one" src="http://bg135.com/wp-content/uploads/2011/08/device-2011-08-20-134857.png" alt="" width="480" height="800" /></a></p>
<p>&nbsp;</p>
<p>图2：</p>
<p><a rel="attachment wp-att-204" href="http://bg135.com/custom-button.html/device-2011-08-20-134950"><img class="alignnone size-full wp-image-204" title="custom button two" src="http://bg135.com/wp-content/uploads/2011/08/device-2011-08-20-134950.png" alt="" width="480" height="800" /></a></p>
<p>&nbsp;</p>
<p>图中左侧颜色各异的按钮就是本章要实现的Custom button,下面是实现的部分核心代码:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleView <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399; font-weight: bold;">View</span> <span style="color: #000000; font-weight: bold;">implements</span> OnLongClickListener,OnTouchListener<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Drawable mDrawable,mDrawable2<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Paint</span> mPaint<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//    private Paint mPaint2;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">float</span> mPaintTextOffset<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> PorterDuff.<span style="color: #006633;">Mode</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> mModes<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">int</span> mModeIndex,intColor<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">String</span> title<span style="color: #339933;">;</span>
    OnDialKeyListener onDialKeyListener<span style="color: #339933;">;</span>
    OnDialTouchListener onDialTouchListener<span style="color: #339933;">;</span>
    Bitmap bit<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">int</span> height,width,textSize,alpha<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> SampleView<span style="color: #009900;">&#40;</span>Activity activity,<span style="color: #003399; font-weight: bold;">String</span> t,<span style="color: #006600; font-weight: bold;">int</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>activity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399; font-weight: bold;">Context</span> context = activity<span style="color: #339933;">;</span>
&nbsp;
        setFocusable<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setLongClickable<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setPressed<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setClickable<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        setOnLongClickListener<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setOnTouchListener<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        title=t<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//按钮 标题</span>
        intColor=c<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//按钮 颜色</span>
&nbsp;
        Resources res = SampleView.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getResources</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        height = res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">normal_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        width = res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">normal_width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        textSize = res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">normal_text_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        alpha = res.<span style="color: #006633;">getDimensionPixelSize</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">dimen</span>.<span style="color: #006633;">normal_alpha</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        mDrawable = context.<span style="color: #006633;">getResources</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDrawable</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">hhhhhh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mDrawable.<span style="color: #006633;">setBounds</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,width,height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//相当于设置其宽和高</span>
        mDrawable.<span style="color: #006633;">setDither</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//抖动   </span>
&nbsp;
        mDrawable2 = context.<span style="color: #006633;">getResources</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDrawable</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mDrawable2.<span style="color: #006633;">setBounds</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,width,height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//相当于设置其宽和高</span>
        mDrawable2.<span style="color: #006633;">setDither</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//抖动   </span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*bit = ((BitmapDrawable)mDrawable).getBitmap();
        bit= BitmapFactory.decodeResource(context.getResources(),
                R.drawable.w);*/</span>
&nbsp;
        mPaint = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Paint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mPaint.<span style="color: #006633;">setAntiAlias</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// 消除锯齿</span>
        mPaint.<span style="color: #006633;">setTextSize</span><span style="color: #009900;">&#40;</span>textSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mPaint.<span style="color: #006633;">setTextAlign</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Paint</span>.<span style="color: #006633;">Align</span>.<span style="color: #006633;">CENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//设置文本居中</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*mPaint2 = new Paint(mPaint);
        mPaint2.setAlpha(alpha);//设置透明度
*/</span>
        <span style="color: #003399; font-weight: bold;">Paint</span>.<span style="color: #003399; font-weight: bold;">FontMetrics</span> fm = mPaint.<span style="color: #006633;">getFontMetrics</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mPaintTextOffset = <span style="color: #009900;">&#40;</span>fm.<span style="color: #006633;">descent</span> + fm.<span style="color: #006633;">ascent</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> 0.5f<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//中间位置</span>
&nbsp;
        mModes = <span style="color: #000000; font-weight: bold;">new</span> PorterDuff.<span style="color: #006633;">Mode</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//            PorterDuff.Mode.SRC_ATOP,</span>
        	PorterDuff.<span style="color: #006633;">Mode</span>.<span style="color: #006633;">DST_ATOP</span>,<span style="color: #666666; font-style: italic;">// 隐藏</span>
<span style="color: #666666; font-style: italic;">//            PorterDuff.Mode.MULTIPLY,</span>
            PorterDuff.<span style="color: #006633;">Mode</span>.<span style="color: #006633;">MULTIPLY</span>,<span style="color: #666666; font-style: italic;">//显示凸起</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//字体显示效果，外凸、下陷等</span>
        mModeIndex = <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> SampleView<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Context</span> context, <span style="color: #003399; font-weight: bold;">AttributeSet</span> attrs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>context, attrs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Interface definition for a callback to be invoked when a tab is triggered
	 * by moving it beyond a target zone.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> OnDialKeyListener <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Called when the user make an action
		 * 只是定义了一个方法,具体的实现还要在调用这个方法的地方实现
		 * @param keyCode keyCode pressed
		 * @param dialTone corresponding dialtone
		 */</span>
		<span style="color: #006600; font-weight: bold;">void</span> onTrigger<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> title,<span style="color: #003399; font-weight: bold;">View</span> v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> OnDialTouchListener <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Called when the user make an action
		 * 只是定义了一个方法,具体的实现还要在调用这个方法的地方实现
		 * @param keyCode keyCode pressed
		 * @param dialTone corresponding dialtone
		 */</span>
		<span style="color: #006600; font-weight: bold;">void</span> onTouchTrigger<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">View</span> v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setIndex<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	mModeIndex=index<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setColor<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">int</span> c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	intColor=c<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	title=t<span style="color: #339933;">;</span>
    	invalidate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">/*public void setPiantColor(int paintC){
    	paintColor=paintC;
    	Log.i(&quot;SampleView&quot;,&quot;setPiantColor==&quot;+ Integer.toHexString(paintColor));
//    	mPaint2.setColor(paintColor);
    }*/</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #006600; font-weight: bold;">void</span> onDraw<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Canvas</span> canvas<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        canvas.<span style="color: #006633;">drawColor</span><span style="color: #009900;">&#40;</span>0x00000000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
        ColorFilter filter<span style="color: #339933;">;</span>
        filter = <span style="color: #000000; font-weight: bold;">new</span> PorterDuffColorFilter<span style="color: #009900;">&#40;</span>intColor,mModes<span style="color: #009900;">&#91;</span>mModeIndex<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//        drawSample(canvas,filter);</span>
&nbsp;
        Rect r = mDrawable.<span style="color: #006633;">getBounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//取得图片的(left, top, right bottom)信息.</span>
        <span style="color: #006600; font-weight: bold;">float</span> x = <span style="color: #009900;">&#40;</span>r.<span style="color: #006633;">left</span> + r.<span style="color: #006633;">right</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> 0.5f<span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//横向中点</span>
        <span style="color: #006600; font-weight: bold;">float</span> y = <span style="color: #009900;">&#40;</span>r.<span style="color: #006633;">top</span> + r.<span style="color: #006633;">bottom</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> 0.5f - mPaintTextOffset<span style="color: #339933;">;</span>    
&nbsp;
        mDrawable2.<span style="color: #006633;">draw</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        mDrawable.<span style="color: #006633;">setColorFilter</span><span style="color: #009900;">&#40;</span>filter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mDrawable.<span style="color: #006633;">draw</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//      canvas.drawText(title, x+1, y+1, mPaint2);</span>
        <span style="color: #000000;  font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mModeIndex==<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        	mPaint.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>0xFF000000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000;  font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
        	mPaint.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>0xFFFFFFFF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        canvas.<span style="color: #006633;">drawText</span><span style="color: #009900;">&#40;</span>title, x, y, mPaint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Registers a callback to be invoked when the user triggers an event.
	 *
	 * @param listener
	 *            the OnTriggerListener to attach to this view
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setOnDialKeyListener<span style="color: #009900;">&#40;</span>OnDialKeyListener listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		onDialKeyListener = listener<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setOnDialTouchListener<span style="color: #009900;">&#40;</span>OnDialTouchListener touchListener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		onDialTouchListener = touchListener<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">void</span> dispatchDialKeyEvent<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>onDialKeyListener <span style="color: #339933;">!</span>= <span style="color: #006600; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			onDialKeyListener.<span style="color: #006633;">onTrigger</span><span style="color: #009900;">&#40;</span>title,v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">void</span> dispatchDialTouchEvent<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>onDialTouchListener <span style="color: #339933;">!</span>= <span style="color: #006600; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			onDialTouchListener.<span style="color: #006633;">onTouchTrigger</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@<span style="color: #003399; font-weight: bold;">Override</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">boolean</span> onLongClick<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
		dispatchDialKeyEvent<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@<span style="color: #003399; font-weight: bold;">Override</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">boolean</span> onTouch<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">View</span> v, MotionEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
		 <span style="color: #000000;  font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000;  font-weight: bold;">case</span> MotionEvent.<span style="color: #006633;">ACTION_DOWN</span>:
             <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
         <span style="color: #000000;  font-weight: bold;">case</span> MotionEvent.<span style="color: #006633;">ACTION_MOVE</span>:
             <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
         <span style="color: #000000;  font-weight: bold;">case</span> MotionEvent.<span style="color: #006633;">ACTION_UP</span>:
             <span style="color: #666666; font-style: italic;">// update mode every other time we change paint colors</span>
        	 mModeIndex=<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        	 dispatchDialTouchEvent<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>这里创建了一个类继承View，算是完全自定义组件，然后再onDraw方法中将所需要的内容画在Canvas上。有了这个类，只要去实现这个类，并传入相应的参数便可以得到如上效果的Custom Button.</p>
<p>&nbsp;</p>
<p>那么我们该如何去使用这个类，并且点击显示相应的效果呢？</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>pre<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">private</span> SampleView myView<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Map</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>Integer, SampleView<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> mView=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">HashMap</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>Integer,SampleView<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Map</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>Integer, <span style="color: #003399; font-weight: bold;">Integer</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> mColor=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">HashMap</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>Integer,<span style="color: #003399; font-weight: bold;">Integer</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//取得所有的分类信息</span>
catCursor=appState.<span style="color: #006633;">db</span>.<span style="color: #006633;">getAlltags</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
linearLayoutForDetail.<span style="color: #006633;">removeAllViews</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mView.<span style="color: #006633;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mColor.<span style="color: #006633;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
preTag=<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>catCursor.<span style="color: #006633;">moveToFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">!</span>catCursor.<span style="color: #006633;">isAfterLast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> catCursor.<span style="color: #006633;">moveToNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//根据数据库中分类的个数信息</span>
&nbsp;
        	name=catCursor.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>catCursor.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>tagsInfo.<span style="color: #006633;">TAG_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        	tag_id=catCursor.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span>catCursor.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>tagsInfo.<span style="color: #006633;">TAG_ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        	colorGiga=catCursor.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span>catCursor.<span style="color: #006633;">getColumnIndex</span><span style="color: #009900;">&#40;</span>tagsInfo.<span style="color: #006633;">TAG_COLOUR</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	myView=<span style="color: #000000; font-weight: bold;">new</span> SampleView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>,name,colorGiga<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        	myView.<span style="color: #006633;">setTag</span><span style="color: #009900;">&#40;</span>tag_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	mView.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>tag_id,myView<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            mColor.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>tag_id,colorGiga<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            myView.<span style="color: #006633;">setOnDialKeyListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            myView.<span style="color: #006633;">setOnDialTouchListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    		linearLayoutForDetail.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>myView,lp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>实现以上代码便得到图1所示的效果，那么我们该如何实现单击变化背景色以及字体颜色呢？</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java5" style="font-family:monospace;">         <span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 触摸事件
	 */</span>
	@<span style="color: #003399; font-weight: bold;">Override</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> onTouchTrigger<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// TODO Auto-generated method stub</span>
&nbsp;
		nowView=<span style="color: #009900;">&#40;</span>SampleView<span style="color: #009900;">&#41;</span> v<span style="color: #339933;">;</span>
		nowView.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>0x00000000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		nowView.<span style="color: #006633;">setIndex</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		nowView.<span style="color: #006633;">postInvalidate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000;  font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>v.<span style="color: #006633;">getTag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>preTag<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//如果当前SampleView和之前点击的不一样,则将之前的SampleView颜色还原</span>
			preView=mView.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>preTag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			preView.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>mColor.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>preTag<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			preView.<span style="color: #006633;">setIndex</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			preView.<span style="color: #006633;">postInvalidate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		preTag=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Integer</span><span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">getTag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		updateList<span style="color: #009900;">&#40;</span>preTag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://bg135.com/custom-button.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Android开发(2)</title>
		<link>http://bg135.com/android-development-2.html</link>
		<comments>http://bg135.com/android-development-2.html#comments</comments>
		<pubDate>Mon, 25 Jul 2011 10:51:30 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[ANDRO]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=201</guid>
		<description><![CDATA[指定ROM、RAM大小启动模拟器: 1 emulator -memory 512 -partition-size 512 -avd gigabud 注释：1：-memroy 512          设置ram为 512 M 2：-partition-size 512  设置rom为512 M 3：-avd gigabud    加载指定的虚拟设备，其中的handroid2.3就是虚拟设备的名字 ROM、RAM的大小要根据PC的内存大小和实际需要进行配置，过大有可能提示内存不足。 webview的有关知识： 1、添加权限：AndroidManifest.xml中必须使用许可&#8221;android.permission.INTERNET&#8221;,否则会出Web page not available错误。 2、在要Activity中生成一个WebView组件：WebView webView = new WebView(this); 3、设置WebView基本信息： 如果访问的页面中有Javascript，则webview必须设置支持Javascript。 webview.getSettings().setJavaScriptEnabled(true); &#8230;]]></description>
			<content:encoded><![CDATA[<p>指定ROM、RAM大小启动模拟器:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">       emulator <span style="color: #339933;">-</span>memory <span style="color: #cc66cc;">512</span> <span style="color: #339933;">-</span>partition<span style="color: #339933;">-</span>size <span style="color: #cc66cc;">512</span> <span style="color: #339933;">-</span>avd gigabud</pre></td></tr></table></div>

<p>注释：1：-memroy 512          设置ram为 512 M<br />
2：-partition-size 512  设置rom为512 M<br />
3：-avd gigabud    加载指定的虚拟设备，其中的handroid2.3就是虚拟设备的名字</p>
<p>ROM、RAM的大小要根据PC的内存大小和实际需要进行配置，过大有可能提示内存不足。</p>
<h3>webview的有关知识：</h3>
<p>1、添加权限：AndroidManifest.xml中必须使用许可&#8221;android.permission.INTERNET&#8221;,否则会出Web page not available错误。<br />
2、在要Activity中生成一个WebView组件：WebView webView = new WebView(this);</p>
<p>3、设置WebView基本信息：<br />
如果访问的页面中有Javascript，则webview必须设置支持Javascript。<br />
webview.getSettings().setJavaScriptEnabled(true);<br />
触摸焦点起作用<br />
requestFocus();//如果不设置，则在点击网页文本输入框时，不能弹出软键盘及不响应其他的一些事件。<br />
取消滚动条<br />
this.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);</p>
<p>4、设置WevView要显示的网页：<br />
互联网用：webView.loadUrl(&#8220;http://www.google.com&#8221;);<br />
本地文件用：webView.loadUrl(&#8220;file:///android_asset/XX.html&#8221;);  本地文件存放在：assets文件中</p>
<p>5、如果希望点击链接由自己处理，而不是新开Android的系统browser中响应该链接。</p>
<p>给WebView添加一个事件监听对象（WebViewClient)，并重写其中的一些方法shouldOverrideUrlLoading：对网页中超链接按钮的响应。当按下某个连接时WebViewClient会调用这个方法，并传递参数：按下的url</p>
<p>onLoadResource<br />
onPageStart<br />
onPageFinish<br />
onReceiveError<br />
onReceivedHttpAuthRequest<br />
6、如果用webview点链接看了很多页以后，如果不做任何处理，点击系统“Back”键，整个浏览器会调用finish()而结束自身，如果希望浏览的网页回退而不是退出浏览器，需要在当前Activity中处理并消费掉该Back事件。</p>
<p>覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onKeyDown<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> keyCoder,<span style="color: #003399;">KeyEvent</span> event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>webView.<span style="color: #006633;">canGoBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> keyCoder <span style="color: #339933;">==</span> <span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">KEYCODE_BACK</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                              webview.<span style="color: #006633;">goBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//goBack()表示返回webView的上一页面</span>
                                 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                           <span style="color: #009900;">&#125;</span>
                        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                   <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Android的webView很强大，其实就是一个浏览器，你可以把它嵌入到你想要的位置，我这里遇到两个问题，就是怎么知道网页的加载进度和加载网页时，点击网页里面的链接还是在当前的webview里跳转，不想跳到浏览器那边，解决办法如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//此方法可以处理webview 在加载时和加载完成时一些操作</span>
webView.<span style="color: #006633;">setWebChromeClient</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WebChromeClient（）<span style="color: #009900;">&#123;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onProgressChanged<span style="color: #009900;">&#40;</span>WebView view, <span style="color: #000066; font-weight: bold;">int</span> newProgress<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newProgress<span style="color: #339933;">==</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>                        <span style="color: #666666; font-style: italic;">// 这里是设置activity的标题， 也可以根据自己的需求做一些其他的操作</span>
          title.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;加载完成&quot;</span><span style="color: #009900;">&#41;</span>；
     <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
     title.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;加载中.......&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">   webView.<span style="color: #006633;">setWebViewClient</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WebViewClient（）<span style="color: #009900;">&#123;</span>
  @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> shouldOverrideUrlLoading<span style="color: #009900;">&#40;</span>WebView view, <span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">//重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边</span>
       view.<span style="color: #006633;">loadUrl</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>[/codesyntax]</p>
<p>[codesyntax lang="java" container="pre_valid" strict="yes"]</p>
<pre>@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) { // 重写此方法可以让webview处理https请求
handler.proceed();
}

});
</pre>
<p>&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">在当前Activity中打开网页：
Intent it <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_VIEW</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			it.<span style="color: #006633;">setFlags</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">FLAG_ACTIVITY_NEW_TASK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			it.<span style="color: #006633;">setData</span><span style="color: #009900;">&#40;</span>Uri.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://code.google.com/p/csipsimple/issues&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			startActivity<span style="color: #009900;">&#40;</span>it<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
用webview打开网页
WebView webView <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>WebView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">webview</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    webView.<span style="color: #006633;">getSettings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setJavaScriptEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    webView.<span style="color: #006633;">setWebViewClient</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FaqWebViewClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    webView.<span style="color: #006633;">loadUrl</span><span style="color: #009900;">&#40;</span>“<span style="color: #0000ff;">&quot;http://code.google.com/p/csipsimple/wiki/FAQ#Summary&quot;</span>”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> FaqWebViewClient <span style="color: #000000; font-weight: bold;">extends</span> WebViewClient <span style="color: #009900;">&#123;</span>
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onPageFinished<span style="color: #009900;">&#40;</span>WebView view, <span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPageFinished</span><span style="color: #009900;">&#40;</span>view, url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			LinearLayout indicator <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>LinearLayout<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">loading_indicator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			indicator.<span style="color: #006633;">setVisibility</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span>.<span style="color: #006633;">GONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Chronometer </strong><strong>用于倒计时</strong><strong> </strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">LayoutAnimationController controller <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LayoutAnimationController<span style="color: #009900;">&#40;</span>set, 0.5f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">ListView</span> listView <span style="color: #339933;">=</span> getListView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        listView.<span style="color: #006633;">setLayoutAnimation</span><span style="color: #009900;">&#40;</span>controller<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>将activity显示为dialog形式:</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>activity android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.csipsimple.ui.prefs.PrefsFast&quot;</span> android<span style="color: #339933;">:</span>label<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@string/prefs_fast&quot;</span>
			android<span style="color: #339933;">:</span>theme<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@style/CupcakeDialog&quot;</span> android<span style="color: #339933;">:</span>configChanges<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;orientation&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
			<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
				<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>action android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.csipsimple.ui.prefs.PrefsFast&quot;</span> <span style="color: #339933;">/&amp;</span>gt<span style="color: #339933;">;</span>
                 <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>category android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.intent.category.DEFAULT&quot;</span> <span style="color: #339933;">/&amp;</span>gt<span style="color: #339933;">;</span>
			<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>
		<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;/</span>activity<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<h4>设置Activity Title:</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">     <span style="color: #003399;">Window</span> w <span style="color: #339933;">=</span> getWindow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		w.<span style="color: #006633;">requestFeature</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Window</span>.<span style="color: #006633;">FEATURE_LEFT_ICON</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">accounts_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		w.<span style="color: #006633;">setFeatureDrawableResource</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Window</span>.<span style="color: #006633;">FEATURE_LEFT_ICON</span>, R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">ic_list_accounts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Custom title listing the account name for filters</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">boolean</span> customTitleOk <span style="color: #339933;">=</span> requestWindowFeature<span style="color: #009900;">&#40;</span><span style="color: #003399;">Window</span>.<span style="color: #006633;">FEATURE_CUSTOM_TITLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">filters_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>customTitleOk<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    getWindow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setFeatureInt</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Window</span>.<span style="color: #006633;">FEATURE_CUSTOM_TITLE</span>, R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">account_filters_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #000000; font-weight: bold;">final</span> TextView titleText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">account_filters_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>titleText <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		        SipProfile acct <span style="color: #339933;">=</span> database.<span style="color: #006633;">getAccount</span><span style="color: #009900;">&#40;</span>accountId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		        titleText.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">filters_for</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		        titleText.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> acct.<span style="color: #006633;">display_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<h4>ExpendListView 展开某些子view:</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">getExpandableListView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">expandGroup</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
getExpandableListView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">expandGroup</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://bg135.com/android-development-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android减少内存与内存泄露</title>
		<link>http://bg135.com/reduce-the-memory-and-memory-leaks-android.html</link>
		<comments>http://bg135.com/reduce-the-memory-and-memory-leaks-android.html#comments</comments>
		<pubDate>Mon, 23 May 2011 05:07:13 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=182</guid>
		<description><![CDATA[Android 减少内存使用以及内存泄露   MAT Heap ASM]]></description>
			<content:encoded><![CDATA[<p>Java编程中经常容易被忽视，但本身又十分重要的一个问题就是内存使用的问题。Android应用主要使用Java语言编写，因此这个问题也同样会在Android开发中出现。Android主要应用在嵌入式设备当中，而嵌入式设备由于一些众所周知的条件限制，通常都不会有很高的配置，特别是内存是比较有限的。如果我们编写的代码当中有太多的对内存使用不当的地方，难免会使得我们的设备运行缓慢，甚至是死机。为了能够使得Android应用程序安全且快速的运行，Android的每个应用程序都会使用一个专有的Dalvik虚拟机实例来运行，它是由Zygote服务进程孵化出来的，也就是说每个应用程序都是在属于自己的进程中运行的。一方面，如果程序在运行过程中出现了内存泄漏的问题，仅仅会使得自己的进程被kill掉，而不会影响其他进程（如果是system_process等系统进程出问题的话，则会引起系统重启）。另一方面Android为不同类型的进程分配了不同的内存使用上限，如果应用进程使用的内存超过了这个上限，则会被系统视为内存泄漏，从而被kill掉。</p>
<p>然而内存的消耗甚至泄露是不可避免的，那么首先只有养成良好的编程习惯，尽量做到减少内存的使用，尽可能的使垃圾内存得到回收，这才是解决问题的根本途径，那么我们该怎样编写代码呢？以下是本人的一些拙见以及网上达人们得一点点小小的建议：</p>
<h3>(一) 查询数据库没有关闭游标</h3>
<p>程序中经常会进行查询数据库的操作，但是经常会有使用完毕Cursor后没有关闭的情况。如果我们的查询结果集比较小，对内存的消耗不容易被发现，只有在常时间大量操作的情况下才会复现内存问题，这样就会给以后的测试和问题排查带来困难和风险。我觉得可以这样做：</p>
<p>Cursor c = sqliteDb.query(TABLE_WORDS, word_full_projection,selection, null, null, null, null);<br />
int numRows = c.getCount();<br />
if(numRows &gt; 0){<br />
c.moveToFirst();<br />
wordProfile account = new wordProfile();<br />
account.createFromDb(c);<br />
c.close();<br />
return account;<br />
}<br />
c.close();</p>
<p>可以采取将cursor里的值赋值给实现Parcelable的类的成员变量，在所需要的地方相应取值便是，这样编可以做到及时关闭游标，释放资源。</p>
<h3>(二) 构造Adapter时，没有使用缓存的 convertView</h3>
<p>以构造ListView的BaseAdapter为例，在BaseAdapter中提高了方法：</p>
<p>public View getView(int position, View convertView, ViewGroup parent)</p>
<p>来向ListView提供每一个item所需要的view对象。初始时ListView会从BaseAdapter中根据当前的屏幕布局实例化一定数量的view对象，同时ListView会将这些view对象缓存起来。当向上滚动ListView时，原先位于最上面的list item的view对象会被回收，然后被用来构造新出现的最下面的list item。这个构造过程就是由getView()方法完成的，getView()的第二个形参 View convertView就是被缓存起来的list item的view对象(初始化时缓存中没有view对象则convertView是null)。</p>
<p>由此可以看出，如果我们不去使用convertView，而是每次都在getView()中重新实例化一个View对象的话，即浪费资源也浪费时间，也会使得内存占用越来越大。ListView回收list item的view对象的过程可以查看:</p>
<p>android.widget.AbsListView.java &#8211;&gt; void addScrapView(View scrap) 方法。</p>
<h3>(三) Bitmap对象不在使用时调用recycle()释放内存</h3>
<p>有时我们会手工的操作Bitmap对象，如果一个Bitmap对象比较占内存，当它不在被使用的时候，可以调用Bitmap.recycle()方法回收此对象的像素所占用的内存，但这不是必须的，视情况而定。</p>
<h3>(四) 将变量的作用域设置为最小</h3>
<p>最小化作用域意味着对垃圾收集器更快的可见。让我们举一个例子。我们有一个变量定义在方法级,当方法执行完毕后,也就是说,控制跳出方法后,则该变量将不会被引用。这也就意味着它已经可以被垃圾回收。但是如果该变量是类级的,这时垃圾回收器就需要等待直到对该类的所有的引用都被<br />
移除后才能进行垃圾回收。优化属性的作用域是必须的。同样这也是封装原则。最小化作用域降低了通过包访问变量并减少了耦合。</p>
<h3>(五) <strong>仅当你确实需要时才初始化</strong></h3>
<p>这个的意思是在你确实需要使用对象之前才分配内存。有些时候,需要在方法的开头定义一些属性。当定义这些属性时,我们很可能在定义它们的时候对其进行初始化。 这种情况下,    如果在首次使用之前出现任何问题    (如出现异常),那么已经初始化的变量所占用的内存就被浪费了。</p>
<h3>(六) <strong>不要在循环中定义变量</strong></h3>
<p>这是大部分内存溢出的程序中的另一个常见的错误。在循环中定义和初始化变量,对于循环的每次执行,该变量实例将在内存中创建并分配内存。在循环启动后,第一个对象已经可以回收了。这种额外的内存分配也会增加垃圾回收器的负担。看一下下面的代码片段你将很容易的找出问题所在。有些时候,<br />
确实有必要在循环内定义变量,不过一定要小心使用。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Vector</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DeclareInLoopExample <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getDataIncorrect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
       Vector<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>string<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>string<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str <span style="color: #339933;">:</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003399;">String</span> newStr <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...Some string operations</span>
newStr <span style="color: #339933;">=</span> newStr <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;X&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;X&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getDataCorrect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
Vector<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>string<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>string<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> newStr <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str <span style="color: #339933;">:</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// ...Some string operations</span>
newStr <span style="color: #339933;">=</span> newStr <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;X&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;X&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>(七) <strong>不要在循环中定义变量</strong></h3>
<p>这是和 String 相关的一些东西,所有对 String 进行的操作都将产生另一个 String 对象。通常我们使用的 String 链接操作,这个需要注意,它会导致一个很长的动态的查询或产生一个 toString 方法中的 String。如果我们使用+操作符来连接字符串,这些额外的对象将根据+产生。当有过多操作的时候会变成很大的开销。使用不是不变对象,如 StringBuffer 来进行字符串链接操作。</p>
<h3>(八) <strong>不要在循环中定义变量</strong></h3>
<p>这会使得垃圾回收器的工作变得比较容易。 将重对象的引用设置为 null。这将释放对中对象的引用,使得垃圾回收器能立即释放它们占用的内存。</p>
<h3>(九) <strong>简单的使用 finally 块</strong></h3>
<p>finally 块即使 try 或 catch 执行不成功的时候也会执行,也就是说即使在 try 或 catch 中出现了任何异常,finally 块中的语句绝对也会执行。将清理内存的语句放在这里以保证内存被清理。</p>
<p>(十) <strong>分散对象创建或删除的时间</strong><br />
集中在短时间内大量创建新对象,特别是大对象,会导致突然需要大量内存,JVM在面临这种情况时,只能进行主GC,以回收内存或整合内存碎片,从而增 加主GC的频率。集中删除对象,道理也是一样的。它使得突然出现了大量的垃圾对象,空闲空间必然减少,从而大大增加了下一次创建新对象时强制主GC的机 会。</p>
<p>其他参考资料：<a title="http://www.chinaup.org/docs/toolbox/performance.html" href="http://www.chinaup.org/docs/toolbox/performance.html">http://www.chinaup.org/docs/toolbox/performance.html</a></p>
<p>&nbsp;</p>
<p>按照以上的编程方法去写代码也许可以使你减少内存的消耗，防止内存泄露。然而当内存被慢慢的耗尽甚至出现内存泄露后，我们该怎么办呢？下面的论述也许对你定位内存泄露的原因以及找出正确的解决办法会有所帮助：</p>
<p>在介绍内存查看、分析工具之前，我想有必要先介绍一下相关的概念。</p>
<p><strong>为何会内存溢出</strong></p>
<p>我们知道JVM根据generation(代)来进行GC，根据下图所示，一共被分为young generation(年轻代)、tenured generation(老年代)、permanent generation(永久代, perm gen)，perm gen（或称Non-Heap 非堆）是个异类，稍后会讲到。注意，heap空间不包括perm gen。</p>
<p><a rel="attachment wp-att-183" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/fig3"><img class="aligncenter size-full wp-image-183" title="ithouge-heap" src="http://bg135.com/wp-content/uploads/2011/05/fig3.gif" alt="" width="1006" height="549" /></a>绝大多数的对象都在young generation被分配，也在young generation被收回，当young generation的空间被填满，GC会进行minor collection(次回收)，这次回收不涉及到heap中的其他generation，minor collection根据weak generational hypothesis(弱年代假设)来假设young generation中大量的对象都是垃圾需要回收，minor collection的过程会非常快。young generation中未被回收的对象被转移到tenured generation，然而tenured generation也会被填满，最终触发major collection(主回收)，这次回收针对整个heap，由于涉及到大量对象，所以比minor collection慢得多。</p>
<p>JVM有三种垃圾回收器，分别是throughput collector，用来做并行young generation回收，由参数-XX:+UseParallelGC启动；concurrent low pause collector，用来做tenured generation并发回收，由参数-XX:+UseConcMarkSweepGC启动；incremental low pause collector，可以认为是默认的垃圾回收器。不建议直接使用某种垃圾回收器，最好让JVM自己决断，除非自己有足够的把握。</p>
<p>Heap中各generation空间是如何划分的？通过JVM的-Xmx=n参数可指定最大heap空间，而<code>-Xms=n</code><code>则是指定</code>最小heap空间。在JVM初始化的时候，如果最小heap空间小于最大heap空间的话，如上图所示JVM会把未用到的空间标注为Virtual。除了这两个参数还有-XX:MinHeapFreeRatio=n和 -XX:MaxHeapFreeRatio=n来分别控制最大、最小的剩余空间与活动对象之比例。在32位Solaris SPARC操作系统下，默认值如下，在32位windows xp下，默认值也差不多。</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="284" valign="top">参数</td>
<td width="284" valign="top">默认值</td>
</tr>
<tr>
<td width="284" valign="top">MinHeapFreeRatio</td>
<td width="284" valign="top">40</td>
</tr>
<tr>
<td width="284" valign="top">MaxHeapFreeRatio</td>
<td width="284" valign="top">70</td>
</tr>
<tr>
<td width="284" valign="top">-Xms</td>
<td width="284" valign="top">3670k</td>
</tr>
<tr>
<td width="284" valign="top">-Xmx</td>
<td width="284" valign="top">64m</td>
</tr>
</tbody>
</table>
<p>由于tenured generation的major collection较慢，所以tenured generation空间小于young generation的话，会造成频繁的major collection，影响效率。Server JVM默认的young generation和tenured generation空间比例为1:2，也就是说young generation的eden和survivor空间之和是整个heap（当然不包括perm gen）的三分之一，该比例可以通过-XX:NewRatio=n参数来控制，而Client JVM默认的-XX:NewRatio是8。至于调整young generation空间大小的NewSize=n和MaxNewSize=n参数就不讲了，请参考后面的资料。</p>
<p>young generation中幸存的对象被转移到tenured generation，但不幸的是concurrent collector线程在这里进行major collection，而在回收任务结束前空间被耗尽了，这时将会发生Full Collections(Full GC)，整个应用程序都会停止下来直到回收完成。Full GC是高负载生产环境的噩梦……</p>
<p>现在来说说异类perm gen，它是JVM用来存储无法在Java语言级描述的对象，这些对象分别是类和方法数据（与class loader有关）以及interned strings(字符串驻留)。一般32位OS下perm gen默认64m，可通过参数-XX:MaxPermSize=n指定，<a href="http://www.yourkit.com/docs/kb/sizes.jsp">JVM Memory Structure</a>一文说，对于这块区域，没有更详细的文献了，神秘。</p>
<p>难么为神秘会内存泄露呢？要回答这个问题又要引出另外一个话题，既什么样的对象GC才会回收？当然是GC发现通过任何reference chain(引用链)无法访问某个对象的时候，该对象即被回收。名词GC Roots正是分析这一过程的起点，例如JVM自己确保了对象的可到达性(那么JVM就是GC Roots)，所以GC Roots就是这样在内存中保持对象可到达性的，一旦不可到达，即被回收。通常GC Roots是一个在current thread(当前线程)的call stack(调用栈)上的对象（例如方法参数和局部变量），或者是线程自身或者是system class loader(系统类加载器)加载的类以及native code(本地代码)保留的活动对象。所以GC Roots是分析对象为何还存活于内存中的利器。知道了什么样的对象GC才会回收后，再来学习下对象引用都包含哪些吧。</p>
<p>从最强到最弱，不同的引用（可到达性）级别反映了对象的生命周期。</p>
<p>1. Strong Ref(强引用)：通常我们编写的代码都是Strong Ref，于此对应的是强可达性，只有去掉强可达，对象才被回收。</p>
<p>2.Soft Ref(软引用)：对应软可达性，只要有足够的内存，就一直保持对象，直到发现内存吃紧且没有Strong Ref时才回收对象。一般可用来实现缓存，通过java.lang.ref.SoftReference类实现。</p>
<p>3.Weak Ref(弱引用)：比Soft Ref更弱，当发现不存在Strong Ref时，立刻回收对象而不必等到内存吃紧的时候。通过java.lang.ref.WeakReference和java.util.WeakHashMap类实现。</p>
<p>4.Phantom Ref(虚引用)：根本不会在内存中保持任何对象，你只能使用Phantom Ref本身。一般用于在进入finalize()方法后进行特殊的清理过程，通过java.lang.ref.PhantomReference实现。</p>
<p>有了上面的种种我相信很容易就能把heap和perm gen撑破了吧，是的利用Strong Ref，存储大量数据，直到heap撑破；利用interned strings（或者class loader加载大量的类）把perm gen撑破。</p>
<p><strong>关于</strong><strong>shallow size</strong><strong>、</strong><strong>retained size</strong></p>
<p>Shallow size就是对象本身占用内存的大小，不包含对其他对象的引用，也就是对象头加成员变量（不是成员变量的值）的总和。在32位系统上，对象头占用8字节，int占用4字节，不管成员变量（对象或数组）是否引用了其他对象（实例）或者赋值为null它始终占用4字节。故此，对于String对象实例来说，它有三个int成员（3*4=12字节）、一个char[]成员（1*4=4字节）以及一个对象头（8字节），总共3*4 +1*4+8=24字节。根据这一原则，对String a=”rosen jiang”来说，实例a的shallow size也是24字节。</p>
<p>Retained size是该对象自己的shallow size，加上从该对象能直接或间接访问到对象的shallow size之和。换句话说，retained size是该对象被GC之后所能回收到内存的总和。为了更好的理解retained size，不妨看个例子。</p>
<p>把内存中的对象看成下图中的节点，并且对象和对象之间互相引用。这里有一个特殊的节点GC Roots，正解！这就是reference chain的起点。</p>
<p><a rel="attachment wp-att-184" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/retained_objects"><img class="size-full wp-image-184 alignleft" title="ithouge-gc1" src="http://bg135.com/wp-content/uploads/2011/05/retained_objects.gif" alt="" width="267" height="251" /></a><a rel="attachment wp-att-185" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/retained_objects_2"><img class="size-full wp-image-185 alignleft" title="ithouge_gc2" src="http://bg135.com/wp-content/uploads/2011/05/retained_objects_2.gif" alt="" width="269" height="251" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>从obj1入手，上图中蓝色节点代表仅仅只有通过obj1才能直接或间接访问的对象。因为可以通过GC Roots访问，所以左图的obj3不是蓝色节点；而在右图却是蓝色，因为它已经被包含在retained集合内。</p>
<p>所以对于左图，obj1的retained size是obj1、obj2、obj4的shallow size总和；右图的retained size是obj1、obj2、obj3、obj4的shallow size总和。obj2的retained size可以通过相同的方式计算。</p>
<p><strong>Heap Dump</strong></p>
<p>heap dump是特定时间点，java进程的内存快照。有不同的格式来存储这些数据，总的来说包含了快照被触发时java对象和类在heap中的情况。由于快照只是一瞬间的事情，所以heap dump中无法包含一个对象在何时、何地（哪个方法中）被分配这样的信息。</p>
<p>在不同平台和不同java版本有不同的方式获取heap dump，而MAT需要的是HPROF格式的heap dump二进制文件。想无需人工干预的话，要这样配置JVM参数：-XX:-HeapDumpOnOutOfMemoryError，当错误发生时，会自动生成heap dump，在生产环境中，只有用这种方式。如果你想自己控制什么时候生成heap dump，在Windows+JDK6环境中可利用JConsole工具，而在Linux或者Mac OS X环境下均可使用JDK5、6自带的jmap工具。当然，还可以配置JVM参数：-XX:+HeapDumpOnCtrlBreak，也就是在控制台使用Ctrl+Break键来生成heap dump。由于我是windows+JDK5，所以选择了-XX:-HeapDumpOnOutOfMemoryError这种方式，更多配置请参考<a href="http://wiki.eclipse.org/index.php/MemoryAnalyzer">MAT Wiki</a>。</p>
<p>&nbsp;</p>
<p>那么下面让我们一起来看看<strong>内存泄露调试</strong>：</p>
<p>一开始不得不说说ClassLoader，本质上，它的工作就是把磁盘上的类文件读入内存，然后调用java.lang.ClassLoader.defineClass方法告诉系统把内存镜像处理成合法的字节码。Java提供了抽象类ClassLoader，所有用户自定义类装载器都实例化自ClassLoader的子类。system class loader在没有指定装载器的情况下默认装载用户类，在Sun Java 1.5中既sun.misc.Launcher$AppClassLoader。更详细的内容请参看下面的资料。</p>
<h3><strong>（一）</strong>内存监测工具 DDMS &#8211;&gt; Heap</h3>
<p>无论怎么小心，想完全避免bad code是不可能的，此时就需要一些工具来帮助我们检查代码中是否存在会造成内存泄漏的地方。Android tools中的DDMS就带有一个很不错的内存监测工具Heap(这里我使用eclipse的ADT插件，并以真机为例，在模拟器中的情况类似)。用Heap监测应用进程使用内存情况的步骤如下：</p>
<p>1. 启动eclipse后，切换到DDMS透视图，并确认Devices视图、Heap视图都是打开的；</p>
<p>2. 将手机通过USB链接至电脑，链接时需要确认手机是处于“USB调试”模式，而不是作为“Mass Storage”；</p>
<p>3. 链接成功后，在DDMS的Devices视图中将会显示手机设备的序列号，以及设备中正在运行的部分进程信息；</p>
<p>4. 点击选中想要监测的进程，比如system_process进程；</p>
<p>5. 点击选中Devices视图界面中最上方一排图标中的“Update Heap”图标；</p>
<p>6. 点击Heap视图中的“Cause GC”按钮；</p>
<p>7. 此时在Heap视图中就会看到当前选中的进程的内存使用量的详细情况［如图所示］。</p>
<p><a rel="attachment wp-att-186" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/010972bc-ef42-304c-955b-72727c33fe6e"><img class="aligncenter size-full wp-image-186" title="ithouge_heap" src="http://bg135.com/wp-content/uploads/2011/05/010972bc-ef42-304c-955b-72727c33fe6e.png" alt="" width="554" height="347" /></a></p>
<p>说明：</p>
<p>a) 点击“Cause GC”按钮相当于向虚拟机请求了一次gc操作；</p>
<p>b) 当内存使用信息第一次显示以后，无须再不断的点击“Cause GC”，Heap视图界面会定时刷新，在对应用的不断的操作过程中就可以看到内存使用的变化；</p>
<p>c) 内存使用信息的各项参数根据名称即可知道其意思，在此不再赘述。</p>
<p>如何才能知道我们的程序是否有内存泄漏的可能性呢。这里需要注意一个值：Heap视图中部有一个Type叫做data object，即数据对象，也就是我们的程序中大量存在的类类型的对象。在data object一行中有一列是“Total Size”，其值就是当前进程中所有Java数据对象的内存总量，一般情况下，这个值的大小决定了是否会有内存泄漏。可以这样判断：</p>
<p>a) 不断的操作当前应用，同时注意观察data object的Total Size值；</p>
<p>b) 正常情况下Total Size值都会稳定在一个有限的范围内，也就是说由于程序中的的代码良好，没有造成对象不被垃圾回收的情况，所以说虽然我们不断的操作会不断的生成很多对象，而在虚拟机不断的进行GC的过程中，这些对象都被回收了，内存占用量会会落到一个稳定的水平；</p>
<p>c) 反之如果代码中存在没有释放对象引用的情况，则data object的Total Size值在每次GC后不会有明显的回落，随着操作次数的增多Total Size的值会越来越大，</p>
<p>直到到达一个上限后导致进程被kill掉。</p>
<p>总之，使用DDMS的Heap视图工具可以很方便的确认我们的程序是否存在内存泄漏的可能性。</p>
<h3><strong>（二）</strong>内存分析工具 MAT(Memory Analyzer Tool)</h3>
<p>如果使用DDMS确实发现了我们的程序中存在内存泄漏，那又如何定位到具体出现问题的代码片段，最终找到问题所在呢？如果从头到尾的分析代码逻辑，那肯定会把人逼疯，特别是在维护别人写的代码的时候。这里介绍一个极好的内存分析工具 &#8211; Memory Analyzer Tool(MAT)。</p>
<p>MAT是一个Eclipse插件，同时也有单独的RCP客户端。官方下载地址、MAT介绍和详细的使用教程请参见：www.eclipse.org/mat，在此不进行说明了。另外在MAT安装后的帮助文档里也有完备的使用教程。在此仅举例说明其使用方法。我自己使用的是MAT的eclipse插件，使用插件要比RCP稍微方便一些。</p>
<p>使用MAT进行内存分析需要几个步骤，包括：生成.hprof文件、打开MAT并导入.hprof文件、使用MAT的视图工具分析内存。以下详细介绍。</p>
<p>我们先来看一个例子：</p>
<p><code>&lt;</code><code>pre</code> <code>class</code><code>=</code><code>"brush:[java]"</code><code>&gt;</code></p>
<div>/**<br />
* Pilot class<br />
* @author rosen jiang<br />
*/<br />
package org.rosenjiang.bo;&nbsp;</p>
<p>public class ithouge{</p>
<p>String name;<br />
int age;</p>
<p>public ithouge(String a, int b){<br />
name = a;<br />
age = b;<br />
}<br />
}</p>
</div>
<p><code> &lt;/</code><code>pre</code><code>&gt;</code></p>
<p><code>&lt;</code><code>pre</code> <code>class</code><code>=</code><code>"brush:[java]"</code><code>&gt;</code></p>
<div>/**<br />
* OOMHeapTest class<br />
* @author rosen jiang<br />
*/<br />
package org.rosenjiang.test;&nbsp;</p>
<p>import java.util.Date;<br />
import java.util.HashMap;<br />
import java.util.Map;<br />
import org.rosenjiang.bo.Pilot;</p>
<p>public class OOMHeapTest {<br />
public static void main(String[] args){<br />
oom();<br />
}</p>
<p>private static void oom(){<br />
Map&lt;String, ithouge&gt; map = new HashMap&lt;String, ithouge&gt;();<br />
Object[] array = new Object[1000000];<br />
for(int i=0; i&lt;1000000; i++){<br />
String d = new Date().toString();<br />
ithouge p = new ithouge(d, i);<br />
map.put(i+&#8221;rosen jiang&#8221;, p);<br />
array[i]=p;<br />
}<br />
}<br />
}</p>
</div>
<p><code>&lt;/</code><code>pre</code><code>&gt;</code></p>
<p>接下来是MAT使用方法介绍：</p>
<h4>(一) 生成.hprof文件</h4>
<p>生成.hprof文件的方法有很多，而且Android的不同版本中生成.hprof的方式也稍有差别，我使用的版本的是2.1，各个版本中生成.prof文件的方法请参考<a title="http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/heap-profiling.html;hb=HEAD" href="http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/heap-profiling.html;hb=HEAD">http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/heap-profiling.html;hb=HEAD</a></p>
<p>1. 打开eclipse并切换到DDMS透视图，同时确认Devices、Heap和logcat视图已经打开了；</p>
<p>2. 将手机设备链接到电脑，并确保使用“USB 调试”模式链接，而不是“Mass Storage“模式；</p>
<p>3. 链接成功后在Devices视图中就会看到设备的序列号，和设备中正在运行的部分进程；</p>
<p>4. 点击选中想要分析的应用的进程，在Devices视图上方的一行图标按钮中，同时选中“Update Heap”和“Dump HPROF file”两个按钮；</p>
<p>5. 这是DDMS工具将会自动生成当前选中进程的.hprof文件，并将其进行转换后存放在sdcard当中，如果你已经安装了MAT插件，那么此时MAT将会自动被启用，并开始对.hprof文件进行分析；</p>
<p>注意：第4步和第5步能够正常使用前提是我们需要有sdcard，并且当前进程有向sdcard中写入的权限(WRITE_EXTERNAL_STORAGE)，否则.hprof文件不会被生成，在logcat中会显示诸如</p>
<p>ERROR/dalvikvm(8574): hprof: can&#8217;t open /sdcard/com.xxx.hprof-hptemp: Permission denied.</p>
<p>的信息。</p>
<p>如果我们没有sdcard，或者当前进程没有向sdcard写入的权限（如system_process），那我们可以这样做：</p>
<p>6. 在当前程序中，例如framework中某些代码中，可以使用android.os.Debug中的：</p>
<p>public static void dumpHprofData(String fileName) throws IOException</p>
<p>方法，手动的指定.hprof文件的生成位置。例如：</p>
<p>xxxButton.setOnClickListener(new View.OnClickListener() {</p>
<p>public void onClick(View view) {</p>
<p>android.os.Debug.dumpHprofData(&#8220;/data/temp/myapp.hprof&#8221;);</p>
<p>&#8230; &#8230;</p>
<p>}</p>
<p>}</p>
<p>上述代码意图是希望在xxxButton被点击的时候开始抓取内存使用信息，并保存在我们指定的位置：/data/temp/myapp.hprof，这样就没有权限的限制了，而且也无须用sdcard。但要保证/data/temp目录是存在的。这个路径可以自己定义，当然也可以写成sdcard当中的某个路径。</p>
<h4>(二) 使用MAT导入.hprof文件</h4>
<p>1. 如果是eclipse自动生成的.hprof文件，可以使用MAT插件直接打开（可能是比较新的ADT才支持）；</p>
<p>2. 如果eclipse自动生成的.hprof文件不能被MAT直接打开，或者是使用android.os.Debug.dumpHprofData()方法手动生成的.hprof文件，则需要将.hprof文件进行转换，转换的方法：</p>
<p>例如我将.hprof文件拷贝到PC上的/ANDROID_SDK/tools目录下，并输入命令hprof-conv xxx.hprof yyy.hprof，其中xxx.hprof为原始文件，yyy.hprof为转换过后的文件。转换过后的文件自动放在/ANDROID_SDK/tools目录下。OK，到此为止，.hprof文件处理完毕，可以用来分析内存泄露情况了。</p>
<p>3. 在Eclipse中点击Windows-&gt;Open Perspective-&gt;Other-&gt;Memory Analyzer，或者打Memory Analyzer Tool的RCP。在MAT中点击File-&gt;Open File，浏览并导入刚刚转换而得到的.hprof文件。</p>
<h4>(三) 使用MAT的视图工具分析内存</h4>
<p><a rel="attachment wp-att-187" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105251"><img class="aligncenter size-full wp-image-187" title="ithouge_mat" src="http://bg135.com/wp-content/uploads/2011/05/20105251.png" alt="" width="851" height="875" /></a></p>
<p>MAT工具分析了heap dump后在界面上非常直观的展示了一个饼图，该图深色区域被怀疑有内存泄漏，可以发现整个heap才64M内存，深色区域就占了99.5%。接下来是一个简短的描述，告诉我们main线程占用了大量内存，并且明确指出system class loader加载的&#8221;java.lang.Thread&#8221;实例有内存聚集，并建议用关键字&#8221;java.lang.Thread&#8221;进行检查。所以，MAT通过简单的两句话就说明了问题所在，就算使用者没什么处理内存问题的经验。在下面还有一个&#8221;Details&#8221;链接，在点开之前不妨考虑一个问题：为何对象实例会聚集在内存中，为何存活（而未被GC）？是的——Strong Ref，那么再走近一些吧。</p>
<p><a rel="attachment wp-att-188" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105252"><img class="aligncenter size-full wp-image-188" title="ithouge-mat2" src="http://bg135.com/wp-content/uploads/2011/05/20105252.png" alt="" width="813" height="519" /></a></p>
<p>点击了&#8221;Details&#8221;链接之后，除了在上一页看到的描述外，还有Shortest Paths To the Accumulation Point和Accumulated Objects部分，这里说明了从GC root到聚集点的最短路径，以及完整的reference chain。观察Accumulated Objects部分，java.util.HashMap和java.lang.Object[1000000]实例的retained heap(size)最大，在上一篇文章中我们知道retained heap代表从该类实例沿着reference chain往下所能收集到的其他类实例的shallow heap(size)总和，所以明显类实例都聚集在HashMap和Object数组中了。这里我们发现一个有趣的现象，既Object数组的shallow heap和retained heap竟然一样，通过<a href="http://www.yourkit.com/docs/90/help/sizes.jsp">Shallow and retained sizes</a>一文可知，数组的shallow heap和一般对象（非数组）不同，依赖于数组的长度和里面的元素的类型，对数组求shallow heap，也就是求数组集合内所有对象的shallow heap之和。好，再来看org.rosenjiang.bo.ithouge对象实例的shallow heap为何是16，因为对象头是8字节，成员变量int是4字节、String引用是4字节，故总共16字节。</p>
<p><a rel="attachment wp-att-189" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105253"><img class="aligncenter size-full wp-image-189" title="ithouge_mat3" src="http://bg135.com/wp-content/uploads/2011/05/20105253.png" alt="" width="678" height="351" /></a>接着往下看，来到了Accumulated Objects by Class区域，顾名思义，这里能找到被聚集的对象实例的类名。org.rosenjiang.bo.ithouge类上头条了，被实例化了290,325次，再返回去看程序，我承认是故意这么干的。还有很多有用的报告可用来协助分析问题，只是本文中的例子太简单，也用不上。以后如有用到，一定撰文详细叙述。</p>
<p><strong>又是perm gen</strong></p>
<p>我们在上一篇文章中知道，perm gen是个异类，里面存储了类和方法数据（与class loader有关）以及interned strings（字符串驻留）。在heap dump中没有包含太多的perm gen信息。那么我们就用这些少量的信息来解决问题吧。</p>
<p>看下面的代码，利用interned strings把perm gen撑破了。</p>
<p><code>&lt;</code><code>pre</code> <code>class</code><code>=</code><code>"brush:[java]"</code><code>&gt; </code></p>
<div>/**<br />
* OOMPermTest class<br />
* @author rosen jiang<br />
*/<br />
package org.rosenjiang.test;&nbsp;</p>
<p>public class OOMPermTest {<br />
public static void main(String[] args){<br />
oom();<br />
}</p>
<p>private static void oom(){<br />
Object[] array = new Object[10000000];<br />
for(int i=0; i&lt;10000000; i++){<br />
String d = String.valueOf(i).intern();<br />
array[i]=d;<br />
}<br />
}<br />
}</p>
</div>
<p><code> &lt;/</code><code>pre</code><code>&gt;</code></p>
<p>控制台打印如下的信息，然后把java_pid1824.hprof文件导入到MAT。其实在MAT里，看到的状况应该和“OutOfMemoryError: Java heap space”差不多（用了数组），因为heap dump并没有包含interned strings方面的任何信息。只是在这里需要强调，使用intern()方法的时候应该多加注意。</p>
<p>java.lang.OutOfMemoryError: PermGen space<br />
Dumping heap to java_pid1824.hprof <img src="http://www.blogjava.net/Images/dot.gif" alt="" /><br />
Heap dump file created [121273334 bytes in 2.845 secs]<br />
Exception in thread &#8221;main&#8221; java.lang.OutOfMemoryError: PermGen space</p>
<p><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><br />
<img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><br />
<img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /></p>
<p>倒是在思考如何把class loader撑破废了些心思。经过尝试，发现使用ASM来动态生成类才能达到目的。ASM(http://asm.objectweb.org)的主要作用是处理已编译类(compiled class)，能对已编译类进行生成、转换、分析（功能之一是实现动态代理），而且它运行起来足够的快和小巧，文档也全面，实属居家必备之良品。ASM提供了core API和tree API，前者是基于事件的方式，后者是基于对象的方式，类似于XML的SAX、DOM解析，但是使用tree API性能会有损失。既然下面要用到ASM，这里不得不啰嗦下已编译类的结构，包括：</p>
<p>1、修饰符（例如public、private）、类名、父类名、接口和annotation部分。</p>
<p>2、类成员变量声明，包括每个成员的修饰符、名字、类型和annotation。</p>
<p>3、方法和构造函数描述，包括修饰符、名字、返回和传入参数类型，以及annotation。当然还包括这些方法或构造函数的具体Java字节码。</p>
<p>4、常量池(constant pool)部分，constant pool是一个包含类中出现的数字、字符串、类型常量的数组。</p>
<p><a rel="attachment wp-att-190" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105257"><img class="aligncenter size-full wp-image-190" title="ithouge_asm" src="http://bg135.com/wp-content/uploads/2011/05/20105257.jpg" alt="" width="459" height="298" /></a></p>
<p>已编译类和原来的类源码区别在于，已编译类只包含类本身，内部类不会在已编译类中出现，而是生成另外一个已编译类文件；其二，已编译类中没有注释；其三，已编译类没有package和import部分。</p>
<p>这里还得说说已编译类对Java类型的描述，对于原始类型由单个大写字母表示，Z代表boolean、C代表char、B代表byte、S代表short、I代表int、F代表float、J代表long、D代表double；而对类类型的描述使用内部名(internal name)外加前缀L和后面的分号共同表示来表示，所谓内部名就是带全包路径的表示法，例如String的内部名是java/lang/String；对于数组类型，使用单方括号加上数据元素类型的方式描述。最后对于方法的描述，用圆括号来表示，如果返回是void用V表示，具体参考下图。</p>
<p><a rel="attachment wp-att-191" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105259"><img class="size-full wp-image-191 alignleft" title="ithouge_asm2" src="http://bg135.com/wp-content/uploads/2011/05/20105259.jpg" alt="" width="303" height="263" /></a><a rel="attachment wp-att-192" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105258"><img class="aligncenter size-full wp-image-192" title="ithouge_asm3" src="http://bg135.com/wp-content/uploads/2011/05/20105258.jpg" alt="" width="482" height="112" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>下面的代码中会使用ASM core API，注意接口ClassVisitor是核心，FieldVisitor、MethodVisitor都是辅助接口。ClassVisitor应该按照这样的方式来调用：visit visitSource? visitOuterClass? ( visitAnnotation | visitAttribute )*( visitInnerClass | visitField | visitMethod )* visitEnd。就是说visit方法必须首先调用，再调用最多一次的visitSource，再调用最多一次的visitOuterClass方法，接下来再多次调用visitAnnotation和visitAttribute方法，最后是多次调用visitInnerClass、visitField和visitMethod方法。调用完后再调用visitEnd方法作为结尾。</p>
<p>注意ClassWriter类，该类实现了ClassVisitor接口，通过toByteArray方法可以把已编译类直接构建成二进制形式。由于我们要动态生成子类，所以这里只对ClassWriter感兴趣。首先是抽象类原型：</p>
<p><code>&lt;</code><code>pre</code> <code>class</code><code>=</code><code>"brush:[java]"</code><code>&gt; </code></p>
<div>/**<br />
* @author rosen jiang<br />
* MyAbsClass class<br />
*/<br />
package org.rosenjiang.test;&nbsp;</p>
<p>public abstract class MyAbsClass {<br />
int LESS = -1;<br />
int EQUAL = 0;<br />
int GREATER = 1;<br />
abstract int absTo(Object o);<br />
}</p>
</div>
<p><code> &lt;/</code><code>pre</code><code>&gt;</code></p>
<p>其次是自定义类加载器，实在没法，ClassLoader的defineClass方法都是protected的，要加载字节数组形式（因为toByteArray了）的类只有继承一下自己再实现。</p>
<p><code>&lt;</code><code>pre</code> <code>class</code><code>=</code><code>"brush:[java]"</code><code>&gt;</code></p>
<div>/**<br />
* @author rosen jiang<br />
* MyClassLoader class<br />
*/<br />
package org.rosenjiang.test;&nbsp;</p>
<p>public class MyClassLoader extends ClassLoader {<br />
public Class defineClass(String name, byte[] b) {<br />
return defineClass(name, b, 0, b.length);<br />
}<br />
}</p>
</div>
<p><code>&lt;/</code><code>pre</code><code>&gt;</code></p>
<p>最后是测试类。</p>
<p><code>&lt;</code><code>pre</code> <code>class</code><code>=</code><code>"brush:[java]"</code><code>&gt; </code></p>
<div>/**<br />
* @author rosen jiang<br />
* OOMPermTest class<br />
*/<br />
package org.rosenjiang.test;&nbsp;</p>
<p>import java.util.ArrayList;<br />
import java.util.List;<br />
import org.objectweb.asm.ClassWriter;<br />
import org.objectweb.asm.Opcodes;</p>
<p>public class OOMPermTest {<br />
public static void main(String[] args) {<br />
OOMPermTest o = new OOMPermTest();<br />
o.oom();<br />
}</p>
<p>private void oom() {<br />
try {<br />
ClassWriter cw = new ClassWriter(0);<br />
cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT,<br />
&#8220;org/rosenjiang/test/MyAbsClass&#8221;, null, &#8221;java/lang/Object&#8221;,<br />
new String[] {});<br />
cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, &#8221;LESS&#8221;, &#8221;I&#8221;,<br />
null, new Integer(-1)).visitEnd();<br />
cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, &#8221;EQUAL&#8221;, &#8221;I&#8221;,<br />
null, new Integer(0)).visitEnd();<br />
cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, &#8221;GREATER&#8221;, &#8221;I&#8221;,<br />
null, new Integer(1)).visitEnd();<br />
cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT, &#8221;absTo&#8221;,<br />
&#8220;(Ljava/lang/Object;)I&#8221;, null, null).visitEnd();<br />
cw.visitEnd();<br />
byte[] b = cw.toByteArray();</p>
<p>List&lt;ClassLoader&gt; classLoaders = new ArrayList&lt;ClassLoader&gt;();<br />
while (true) {<br />
MyClassLoader classLoader = new MyClassLoader();<br />
classLoader.defineClass(&#8220;org.rosenjiang.test.MyAbsClass&#8221;, b);<br />
classLoaders.add(classLoader);<br />
}<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
}</p>
</div>
<p><code>&lt;/</code><code>pre</code><code>&gt;</code></p>
<p>不一会儿，控制台就报错了。</p>
<p>java.lang.OutOfMemoryError: PermGen space<br />
Dumping heap to java_pid3023.hprof <img src="http://www.blogjava.net/Images/dot.gif" alt="" /><br />
Heap dump file created [92593641 bytes in 2.405 secs]<br />
Exception in thread &#8221;main&#8221; java.lang.OutOfMemoryError: PermGen space<br />
<img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><br />
<img src="http://www.blogjava.net/Images/dot.gif" alt="" /></p>
<p>打开java_pid3023.hprof文件，注意看下图的Classes: 88.1k和Class Loader: 87.7k部分，从这点可看出class loader加载了大量的类。</p>
<p><a rel="attachment wp-att-193" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105254"><img class="size-full wp-image-193 alignleft" title="ithouge_asm4" src="http://bg135.com/wp-content/uploads/2011/05/20105254.png" alt="" width="631" height="134" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>更进一步分析，点击上图中红框线圈起来的按钮，选择Java Basics——Class Loader Explorer功能。打开后能看到下图所示的界面，第一列是class loader名字；第二列是class loader已定义类(defined classes)的个数，这里要说一下已定义类和已加载类(loaded classes)了，当需要加载类的时候，相应的class loader会首先把请求委派给父class loader，只有当父class loader加载失败后，该class loader才会自己定义并加载类，这就是Java自己的“双亲委派加载链”结构；第三列是class loader所加载的类的实例数目。</p>
<p><a rel="attachment wp-att-194" href="http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/attachment/20105255"><img class="size-full wp-image-194 alignleft" title="ithouge_asm5" src="http://bg135.com/wp-content/uploads/2011/05/20105255.png" alt="" width="637" height="567" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>在Class Loader Explorer这里，能发现class loader是否加载了过多的类。另外，还有Duplicate Classes功能，也能协助分析重复加载的类，在此就不再截图了，可以肯定的是MyAbsClass被重复加载了N多次。</p>
<p><strong>最后</strong></p>
<p>其实MAT工具非常的强大，上面故弄玄虚的范例代码根本用不上MAT的其他分析功能，所以就不再描述了。其实对于OOM不只我列举的两种溢出错误，还有多种其他错误，但我想说的是，对于perm gen，如果实在找不出问题所在，建议使用JVM的-verbose参数，该参数会在后台打印出日志，可以用来查看哪个class loader加载了什么类，例：“[Loaded org.rosenjiang.test.MyAbsClass from org.rosenjiang.test.MyClassLoader]”。</p>
<p>&nbsp;</p>
<p>参考资料：</p>
<h3><a href="http://rayleeya.iteye.com/blog/727074">[Android] 内存泄漏调试经验分享 (一)</a></h3>
<h3><a href="http://rayleeya.iteye.com/blog/755657">[Android] 内存泄漏调试经验分享 (二)</a></h3>
<p><a href="http://www.blogjava.net/rosen/archive/2010/05/21/321575.html">使用Memory Analyzer tool(MAT)分析内存泄漏（一）</a></p>
<p><a id="viewpost1_TitleUrl" href="http://www.blogjava.net/rosen/archive/2010/06/13/323522.html">使用Memory Analyzer tool(MAT)分析内存泄漏（二）</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/reduce-the-memory-and-memory-leaks-android.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

