<?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>Sun, 13 May 2012 10:27:22 +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>Ubuntu wireshark install</title>
		<link>http://bg135.com/ubuntu-wireshark-install.html</link>
		<comments>http://bg135.com/ubuntu-wireshark-install.html#comments</comments>
		<pubDate>Thu, 12 Apr 2012 02:42:41 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[技术大类]]></category>
		<category><![CDATA[wireshark]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=234</guid>
		<description><![CDATA[Wireshark是世界上最流行的网络分析工具。这个强大的工具可以捕捉网络中的数据，并为用户提供关于网络和上层协议的各种信息。 与很多其他网络工具一样，Wireshark也使用pcap network library来进行封包捕捉。 Wireshark的优势： - 安装方便。 - 简单易用的界面。 - 提供丰富的功能。 Wireshark的原名是Ethereal，新名字是2006年起用的。当时Ethereal的主要开发者决定离开他原来供职的公司，并继续开发这个软件。但由于Ethereal这个名称的使用权已经被原来那个公司注册，Wireshark这个新名字也就应运而生了。 &#160; 系统要求： libpcap库对于Ethereal和Wireshark都非常重要，它们需要使用这个库的功能进行封包捕捉工作。 如果您的系统中没有安装libpcap或者其它必要的组件, #apt-get install libgtk2.0-dev libglib2.0-dev #apt-get install checkinstall &#160; 安装完这个还会报一个libpcap的错，libpcap的安装方法如下： 下载libpcap的源码包 解压之后依次执行： ./configure make make install 安装完成之后： 下载Wireshark的源码包 下载完成之后依次执行： #./configure 编译和安装： #make&#160; #make install #checkinstall &#8230;]]></description>
			<content:encoded><![CDATA[<p>Wireshark是世界上最流行的网络分析工具。这个强大的工具可以捕捉网络中的数据，并为用户提供关于网络和上层协议的各种信息。<br />
与很多其他网络工具一样，Wireshark也使用pcap network library来进行封包捕捉。</p>
<p>Wireshark的优势：</p>
<p>- 安装方便。</p>
<p>- 简单易用的界面。</p>
<p>- 提供丰富的功能。</p>
<p>Wireshark的原名是Ethereal，新名字是2006年起用的。当时Ethereal的主要开发者决定离开他原来供职的公司，并继续开发这个软件。但由于Ethereal这个名称的使用权已经被原来那个公司注册，Wireshark这个新名字也就应运而生了。</p>
<p>&nbsp;</p>
<p><strong>系统要求：</strong></p>
<p>libpcap库对于Ethereal和Wireshark都非常重要，它们需要使用这个库的功能进行封包捕捉工作。<br />
如果您的系统中没有安装libpcap或者其它必要的组件,</p>
<p><strong>#apt-get install libgtk2.0-dev libglib2.0-dev </strong></p>
<p><strong>#apt-get install checkinstall </strong></p>
<p>&nbsp;</p>
<p>安装完这个还会报一个libpcap的错，libpcap的安装方法如下：</p>
<p><a href="http://www.tcpdump.org/#latest-release">下载libpcap的源码包</a></p>
<p>解压之后依次执行：</p>
<p>./configure<br />
make<br />
make install</p>
<p>安装完成之后：</p>
<p><a href="http://http://linux.softpedia.com/progDownload/Ethereal-Download-1961.html">下载Wireshark的源码包</a></p>
<p>下载完成之后依次执行：</p>
<p><strong>#./configure </strong></p>
<p>编译和安装：</p>
<table>
<tbody>
<tr>
<td><strong>#make</strong>&nbsp;</p>
<p><strong>#make install<br />
#checkinstall </strong></td>
</tr>
</tbody>
</table>
<p>运行Wireshark：<br />
<strong>#wireshark </strong></p>
<p>&nbsp;</p>
<p><strong><br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/ubuntu-wireshark-install.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android source code</title>
		<link>http://bg135.com/android-source-code.html</link>
		<comments>http://bg135.com/android-source-code.html#comments</comments>
		<pubDate>Sat, 07 Apr 2012 09:49:33 +0000</pubDate>
		<dc:creator>ithouge</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[技术大类]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://bg135.com/?p=232</guid>
		<description><![CDATA[Android最好、最权威的东西自然是在源码里，以下文章零起点逐步揭露android的神秘面纱。 准备篇： 设置Linux的编译环境： In general you will need: Python 2.5 &#8212; 2.7, which you can download from python.org. GNU Make 3.81 &#8212; 3.82, which you can download from gnu.org, JDK 6 if you wish to build Gingerbread or newer; JDK &#8230;]]></description>
			<content:encoded><![CDATA[<p>Android最好、最权威的东西自然是在源码里，以下文章零起点逐步揭露android的神秘面纱。</p>
<h2>准备篇：</h2>
<p>设置Linux的编译环境：</p>
<p>In general you will need:</p>
<ul>
<li>Python 2.5 &#8212; 2.7, which you can download from <a href="http://www.python.org/download/">python.org</a>.</li>
<li>GNU Make 3.81 &#8212; 3.82, which you can download from <a href="http://ftp.gnu.org/gnu/make/">gnu.org</a>,</li>
<li>JDK 6 if you wish to build Gingerbread or newer; JDK 5 for Froyo or older.  You can download both from <a href="http://java.sun.com/javase/downloads/">java.sun.com</a>.</li>
<li>Git 1.7 or newer. You can find it at <a href="http://git-scm.com/download">git-scm.com</a>.</li>
</ul>
<p>Detailed instructions for Ubuntu 10.04+ follow.</p>
<p>&nbsp;</p>
<p>这里建议搭建一套Android开发环境，具体参考文章：<span style="font-size: 20px; font-weight: bold;"><a rel="bookmark" href="http://bg135.com/android%e5%bc%80%e5%8f%91%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba.html">Android开发环境搭建</a></span></p>
<p>当然也可以选择性的安装。</p>
<h2 id="installing-required-packages-ubuntu-1004-1110">Installing required packages (Ubuntu 10.04 &#8212; 11.10)</h2>
<p>Ubuntu 10.04 64-bit is recommended.  Building using a newer version of Ubuntu or a 32-bit Ubuntu is currently only experimentally supported and is not guaranteed to work on branches other than master.</p>
<p>64-bit (recommended)</p>
<pre><code>$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl zlib1g-dev libc6-dev lib32ncurses5-dev ia32-libs \
  x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev \
  libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown \
  libxml2-utils xsltproc
</code></pre>
<p>On Ubuntu 10.10:</p>
<pre><code>$ sudo ln -s /usr/lib32/mesa/libGL.so.1 /usr/lib32/mesa/libGL.so
</code></pre>
<p>On Ubuntu 11.10:</p>
<pre><code>$ sudo apt-get install libx11-dev:i386
</code></pre>
<p>32-bit (experimental)</p>
<pre><code>$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl zlib1g-dev libc6-dev libncurses5-dev x11proto-core-dev \
  libx11-dev libreadline6-dev libgl1-mesa-dev tofrodos python-markdown \
  libxml2-utils xsltproc
</code></pre>
<p>&nbsp;</p>
<h2 id="installing-required-packages-ubuntu-1204">Installing required packages (Ubuntu 12.04)</h2>
<p>Building on Ubuntu 12.04 is currently only experimentally supported and is not guaranteed to work on branches other than master.</p>
<pre><code>$ sudo apt-get install git-core gnupg flex bison gperf build-essential \
  zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
  libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev:i386 \
  g++-multilib mingw32 openjdk-6-jdk tofrodos python-markdown \
  libxml2-utils xsltproc zlib1g-dev:i386
</code></pre>
<p>You may also need to fix a compilation issue in a kernel header:</p>
<pre><code>$ sudo vim /usr/include/linux/usb/ch9.h  # line 592

# return le16_to_cpu(epd-&gt;wMaxPacketSize);
return __le16_to_cpu(epd-&gt;wMaxPacketSize);
</code></pre>
<p>&nbsp;</p>
<p>前期准备工作完成，开始进入下载阶段。</p>
<p>&nbsp;</p>
<h2>下载篇：</h2>
<p>To install, initialize, and configure Repo, follow these steps:</p>
<ul>
<li>Make sure you have a bin/ directory in your home directory, and that it is included in your path:
<pre><code>$ mkdir ~/bin
$ PATH=~/bin:$PATH
</code></pre>
</li>
<li>Download the Repo script and ensure it is executable:
<pre><code>$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo &gt; ~/bin/repo
$ chmod a+x ~/bin/repo
</code></pre>
</li>
<li>The SHA-1 checksum for repo is 29ba4221d4fccdfa8d87931cd73466fdc24040b5</li>
</ul>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2 id="initializing-a-repo-client">Initializing a Repo client</h2>
<p>After installing Repo, set up your client to access the android source repository:</p>
<ul>
<li>Create an empty directory to hold your working files.  If you&#8217;re using MacOS, this has to be on a case-sensitive filesystem.  Give it any name you like:
<pre><code>$ mkdir WORKING_DIRECTORY
$ cd WORKING_DIRECTORY
</code></pre>
</li>
<li>Run <code>repo init</code> to bring down the latest version of Repo  with all its most recent bug fixes.  You must specify a URL for the  manifest, which specifies where the various repositories included in the  Android source will be placed within your working directory.
<pre><code>$ repo init -u https://android.googlesource.com/platform/manifest
</code></pre>
<p>To check out a branch other than &#8220;master&#8221;, specify it with -b:</p>
<pre><code>$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
</code></pre>
<pre><span style="font-family: monospace;">
</span></pre>
</li>
</ul>
<p>完成之后在当前目录下产生一个.repo的目录。</p>
<p>&nbsp;</p>
<h2 id="getting-the-files">Getting the files</h2>
<p>To pull down files to your working directory from the repositories as specified in the default manifest, run</p>
<pre><code>$ repo sync</code></pre>
<pre><code>
</code></pre>
<p>这步之后就开始漫长的等待，快则几个小时，慢的话也许几天。。。。</p>
<p>如果中途断开，则重新执行：repo sync 或repo -f sync</p>
<p>&nbsp;</p>
<p>下载完成之后，便可以开启编译之旅。</p>
<p>&nbsp;</p>
<h2>编译篇：</h2>
<h1 id="building-the-system">Building the System</h1>
<p>The basic sequence of build commands is as follows:</p>
<h2 id="initialize">Initialize</h2>
<p>Initialize the environment with the <code>envsetup.sh</code> script. Note that replacing &#8220;source&#8221; with a single dot saves a few characters, and the short form is more commonly used in documentation.</p>
<pre><code>$ source build/envsetup.sh
</code></pre>
<p>or</p>
<pre><code>$ . build/envsetup.sh
</code></pre>
<p>&nbsp;</p>
<h2 id="choose-a-target">Choose a Target</h2>
<p>Choose which target to build with <code>lunch</code>.  The exact configuration can be passed as an argument, e.g.</p>
<pre><code>$ lunch full-eng
</code></pre>
<p>The example above refers to a complete build for the emulator, with all debugging enabled.</p>
<p>If run with no arguments <code>lunch</code> will prompt you to choose a target from the menu.</p>
<p>All build targets take the form BUILD-BUILDTYPE, where the BUILD is a codename referring to the particular feature combination:</p>
<table>
<thead>
<tr>
<th>Build name</th>
<th>Device</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>full</td>
<td>emulator</td>
<td>fully configured with all languages, apps, input methods</td>
</tr>
<tr>
<td>full_maguro</td>
<td>maguro</td>
<td><code>full</code> build running on Galaxy Nexus GSM/HSPA+ (&#8220;maguro&#8221;)</td>
</tr>
<tr>
<td>full_panda</td>
<td>panda</td>
<td><code>full</code> build running on PandaBoard (&#8220;panda&#8221;)</td>
</tr>
</tbody>
</table>
<p>and the BUILDTYPE is one of the following:</p>
<table>
<thead>
<tr>
<th>Buildtype</th>
<th>Use</th>
</tr>
</thead>
<tbody>
<tr>
<td>user</td>
<td>limited access; suited for production</td>
</tr>
<tr>
<td>userdebug</td>
<td>like &#8220;user&#8221; but with root access and debuggability; preferred for debugging</td>
</tr>
<tr>
<td>eng</td>
<td>development configuration with additional debugging tools</td>
</tr>
</tbody>
</table>
<p>For more information about building for and running on actual hardware, see <a href="http://source.android.com/source/building-devices.html">Building for devices</a></p>
<h2 id="build-the-code">Build the Code</h2>
<p>Build everything with <code>make</code>. GNU make can handle parallel tasks with a <code>-jN</code> argument, and it&#8217;s common to use a number of tasks N that&#8217;s between 1 and 2 times the number of hardware threads on the computer being used for the build. E.g. on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between <code>make -j16</code> and <code>make -j32</code>.</p>
<pre><code>$ make -j4
</code></pre>
<pre><code>
</code></pre>
<pre><span style="font-family: monospace;">
</span></pre>
<p>编译一般也不会出现什么问题，只是如果是要在32bit的Ubuntu下编译，则要做以下处理：</p>
<p>&nbsp;</p>
<h3>1、修改Android.mk配置文件</h3>
<p>将以下位置的几个配置文件：</p>
<p>./external/clearsilver/cgi/Android.mk</p>
<p>./external/clearsilver/cs/Android.mk</p>
<p>./external/clearsilver/java-jni/Android.mk</p>
<p>./external/clearsilver/util/Android.mk</p>
<p>文件中的</p>
<p>LOCAL_CFLAGS += -m64</p>
<p>LOCAL_LDFLAGS += -m64</p>
<p>m64替换m32（或者注释掉，然后重写也可以）</p>
<p>LOCAL_CFLAGS += -m32</p>
<p>LOCAL_LDFLAGS += -m32</p>
<h3>2、修改main.mk</h3>
<p>再将./build/core/main.mk 中的</p>
<p>ifneq (64,$(findstring 64,$(build_arch)))</p>
<p>改为：</p>
<p>ifneq (i686,$(findstring i686,$(build_arch)))</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>首次编译，可能要花点时间，静静的等待，出现如下字样，则恭喜您编译成功啦。</p>
<p>Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img</p>
<p>Install system fs image: out/target/product/generic/system.img</p>
<p>Installed file list: out/target/product/generic/installed-files.txt</p>
<p>&nbsp;</p>
<p>编译完成之后便可以开始在Eclipse里面调试源码啦。</p>
<p>&nbsp;</p>
<h2>调试篇：</h2>
<h1 id="using-eclipse">Using Eclipse</h1>
<p>&nbsp;</p>
<h2 id="basic-setup">Basic setup</h2>
<p>First, it&#8217;s important to make sure the regular Android development system is set up.</p>
<pre><code>cd /path/to/android/root
make
</code></pre>
<p><strong>Important</strong>: You will still be using <code>make</code> to build the files you will actually run (in the emulator or on a  device). You will be using Eclipse to edit files and verify that they  compile, but when you want to run something you will need to make sure  files are saved in Eclipse and run <code>make</code> in a shell. The Eclipse build is just for error checking.</p>
<p>Eclipse needs a list of directories to search for Java files. This is called the &#8220;Java Build Path&#8221; and can be set with the <code>.classpath</code> file. We have a sample version to start you off.</p>
<pre><code>cd /path/to/android/root
cp development/ide/eclipse/.classpath .
chmod u+w .classpath
</code></pre>
<p>Now edit that copy of <code>.classpath</code>, if necessary.</p>
<p>&nbsp;</p>
<p>注意：Eclipse里只是用于对源码调试，如果要运行到模拟器或Device则还另须make。</p>
<p>&nbsp;</p>
<h3 id="increase-eclipses-memory-settings">Increase Eclipse&#8217;s Memory Settings</h3>
<p>The Android project is large enough that Eclipse&#8217;s Java VM sometimes  runs out of memory while compiling it. Avoid this problem by editing the  <code>eclipse.ini</code> file. On Apple OSX the eclipse.ini file is located at</p>
<pre><code>/Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
</code></pre>
<p>Memory-related defaults (as of Eclipse 3.4):</p>
<pre><code>-Xms40m
-Xmx256m
-XX:MaxPermSize=256m
</code></pre>
<p>Recommended settings for Android development:</p>
<pre><code>-Xms128m
-Xmx512m
-XX:MaxPermSize=256m
</code></pre>
<p>These settings set Eclipse&#8217;s minimum Java heap size to 128MB, set the  maximum Java heap size to 512MB, and keep the maximum permanent  generation size at the default of 256MB.</p>
<p>&nbsp;</p>
<p>修改完之后，对于3.5及以下的版本可以如下操作：</p>
<p>Now create a project for Android development:</p>
<ol>
<li>If Eclipse asks you for a workspace location, choose the default.</li>
<li>If you have a &#8220;Welcome&#8221; screen, close it to reveal the Java perspective.</li>
<li>File &gt; New &gt; Java Project</li>
<li>Pick a project name, &#8220;android&#8221; or anything you like.</li>
<li>Select &#8220;Create project from existing source&#8221;, enter the path to your Android root directory, and click Finish.</li>
<li>Wait while it sets up the project. (You&#8217;ll see a subtle progress meter in th</li>
</ol>
<p>对于3.5以上的版本的版本可以尝试以下操作：</p>
<p>由于3.5以上版本没有Create project from existing source这个选项，因此读者可以尝试方法：在启动Eclipse的时候，选择工作区时不要勾选设置为默认工作区，同时工作区指向下载的android source code的根目录，点击“确定&#8221;,加载完成之后，整个源码便加载进来啦。</p>
<p>&nbsp;</p>
<p>注意：在导入完整个source code之后，经常报如下错误：</p>
<p><code><em><span style="font-family: 'Courier New';">Project'gingerbread' is missing required library:'out/target/common/obj/JAVA_LIBRARIES/google-common_intermediates/javalib.jar'<br />
Project'gingerbread' is missing required library:'out/target/common/obj/JAVA_LIBRARIES/gsf-client_intermediates/javalib.jar'</span></em></code></p>
<p><code><em><span style="font-family: 'Courier New';">删除</span></em><em>.classpath</em></code><code><em><span style="font-family: 'Courier New';">中的这两行路径：</span></em></code></p>
<p><code><em><span style="font-family: 'Courier New';">&lt;classpathentrykind="lib"path="out/target/common/obj/JAVA_LIBRARIES/google-common_intermediates/javalib.jar"/&gt;<br />
&lt;classpathentrykind="lib"path="out/target/common/obj/JAVA_LIBRARIES/gsf-client_intermediates/javalib.jar"/&gt;</span></em></code></p>
<p><code><em><span style="font-family: 'Courier New';">添加</span></em></code></p>
<p><code><em><span style="font-family: 'Courier New';">&lt;classpathentrykind="lib"path="out/target/common/obj/JAVA_LIBRARIES/android-common_intermediates/javalib.jar"/&gt;</span></em></code></p>
<p><code><em><span style="font-family: 'Courier New';">然后重启</span></em><em>Eclipse</em></code><code><em><span style="font-family: 'Courier New';">，即可解决。</span></em></code></p>
<p>&nbsp;</p>
<p><span style="font-family: 'Courier New';"><em>当然我们也可以单独调试某个模块，比如说：Contacts，Settings，Launcher2，Mms，Browser等等。</em></span></p>
<p>&nbsp;</p>
<p>1、使用到了未包括在SDK中（公开发布）的API，有两种情况：</p>
<p>a 被 @hide 标注了的类：这 些类是被隐藏起来的，这些类大多都在com.android.internal包下面；</p>
<p>b 一些未包括在SDK中的、Google私有的Api，它们大多都在com.google.android包下面</p>
<p>&nbsp;</p>
<p>2、使用了一些由framework层声明的aidl接口访问系统服务，然而这些aidl在编译时才会转换成.java源码，因此在SDK环境下编译这些应用时无法访问到它们。</p>
<p>解决方法是：</p>
<p>（请注意：out目录 是在完整源码编译环境下（Linux下），执行一次完整编译后的输出目录）</p>
<p>&nbsp;</p>
<p>1、找到那些被隐藏起来的类、它们在以下两个位置：</p>
<p>A、out\target\common\obj\JAVA_LIBRARIES\framework_intermediates/ classes.jar ；</p>
<p>B、out\target\common\obj\JAVA_LIBRARIES\android-common_intermediates\ classes.jar ；</p>
<p>将找到的.jar文件，在Eclipse中作为User Library 添加到 Project 的Librarys依赖中，并将该库的顺序置于Android SDK Library之上。</p>
<p>在 out\target\common\obj\JAVA_LIBRARIES\ 目录下，还有很多其它具体应用相关的jar包，&#8230;. ；</p>
<p>&nbsp;</p>
<p>2、找到那些缺失的、编译时才生成的，.java源文件，它们通常在以下位置：</p>
<p>A、 out\target\common\obj\JAVA_LIBRARIES\XXX _intermediates\src 目录下，XXX是相关模块的名称；</p>
<p>B、out\target\common\obj\APPS\ XXX _intermediates\src 目录下；</p>
<p>将找到的src目录，在Eclipse下，以Link Source的方式，连接到Project 中作为源码的一部分</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>例如：</p>
<p>Eclipse下编译Android自带联系人应用，错误信息：</p>
<ol>
<li>import com.android.phone.CallLogAsync;</li>
<li>import com.android.phone.HapticFeedback;</li>
</ol>
<p>解决方法导入：</p>
<p>out\target\common\obj\APPS\Phone_intermediates\classes.jar</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://bg135.com/android-source-code.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%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><a id="cb_post_title_url" href="http://www.cnblogs.com/top5/archive/2012/04/09/2439630.html"></a></h2>
<h2 style="padding-left: 30px;"><a id="cb_post_title_url" href="http://www.cnblogs.com/top5/archive/2012/04/09/2439630.html">Android流量统计TrafficStats类的使用</a></h2>
<p>&nbsp;</p>
]]></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>9</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>
	</channel>
</rss>

