﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客- 攀升·Uranus-随笔分类-Android</title><link>http://www.cppblog.com/iuranus/category/11397.html</link><description>&lt;br&gt;&lt;font color="#ADFF2F"&gt;Something Different，Something New&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Fri, 12 Sep 2014 18:31:09 GMT</lastBuildDate><pubDate>Fri, 12 Sep 2014 18:31:09 GMT</pubDate><ttl>60</ttl><item><title>(转)内存溢出的解决办法 Android</title><link>http://www.cppblog.com/iuranus/archive/2010/08/23/124394.html</link><dc:creator>攀升</dc:creator><author>攀升</author><pubDate>Mon, 23 Aug 2010 04:26:00 GMT</pubDate><guid>http://www.cppblog.com/iuranus/archive/2010/08/23/124394.html</guid><wfw:comment>http://www.cppblog.com/iuranus/comments/124394.html</wfw:comment><comments>http://www.cppblog.com/iuranus/archive/2010/08/23/124394.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/iuranus/comments/commentRss/124394.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/iuranus/services/trackbacks/124394.html</trackback:ping><description><![CDATA[<span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">昨天在模拟器上给gallery放入图片的时候，出现java.lang.OutOfMemoryError: bitmap size exceeds VM budget 异常，图像大小超过了RAM内存。</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp;&nbsp; &nbsp;模拟器RAM比较小，只有8M内存，当我放入的大量的图片（每个100多K左右），就出现上面的原因。由于每张图片先前是压缩的情况。放入到Bitmap的时候，大小会变大，导致超出RAM内存，具体解决办法如下：</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">//解决加载图片 内存溢出的问题</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //Options 只保存图片尺寸大小，不保存图片到内存</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;BitmapFactory.Options opts = new BitmapFactory.Options();</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//缩放的比例，缩放是很难按准备的比例进行缩放的，其值表明缩放的倍数，SDK中建议其值是2的指数值,值越大会导致图片不清晰</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;opts.inSampleSize = 4;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Bitmap bmp = null;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;bmp = BitmapFactory.decodeResource(getResources(), mImageIds[position],opts);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//回收</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; bmp.recycle();</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">通过上面的方式解决了，但是这并不是最完美的解决方式。</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">通过一些了解，得知如下：</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">优化Dalvik虚拟机的堆内存分配</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">对于Android平台来说，其托管层使用的Dalvik Java VM从目前的表现来看还有很多地方可以优化处理，比如我们在开发一些大型游戏或耗资源的应用中可能考虑手动干涉GC处理，使用 dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以增强程序堆内存的处理效率。当然具体 原理我们可以参考开源工程，这里我们仅说下使用方法:&nbsp; &nbsp;private final static float TARGET_HEAP_UTILIZATION = 0.75f; 在程序onCreate时就可以调用 VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 即可。</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">Android堆内存也可自己定义大小</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; &nbsp; 对于一些Android项目，影响性能瓶颈的主要是Android自己内存管理机制问题，目前手机厂商对RAM都比较吝啬，对于软件的流畅性来说RAM对 性能的影响十分敏感，除了 优化Dalvik虚拟机的堆内存分配外，我们还可以强制定义自己软件的对内存大小，我们使用Dalvik提供的 dalvik.system.VMRuntime类来设置最小堆内存为例:</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;</span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br></span><span  style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); //设置最小heap内存为6MB大小。当然对于内存吃紧来说还可以通过手动干涉GC去处理</span>
<img src ="http://www.cppblog.com/iuranus/aggbug/124394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/iuranus/" target="_blank">攀升</a> 2010-08-23 12:26 <a href="http://www.cppblog.com/iuranus/archive/2010/08/23/124394.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）HttpClient基础 </title><link>http://www.cppblog.com/iuranus/archive/2010/07/04/119311.html</link><dc:creator>攀升</dc:creator><author>攀升</author><pubDate>Sun, 04 Jul 2010 12:50:00 GMT</pubDate><guid>http://www.cppblog.com/iuranus/archive/2010/07/04/119311.html</guid><wfw:comment>http://www.cppblog.com/iuranus/comments/119311.html</wfw:comment><comments>http://www.cppblog.com/iuranus/archive/2010/07/04/119311.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/iuranus/comments/commentRss/119311.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/iuranus/services/trackbacks/119311.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行请求HttpClient的最重要的功能是执行HTTP方法。一个HTTP方法的执行涉及到一个或多个HTTP请求或HTTP响应的交流，HttpClient通常是在内部处理的。用户将提供一个执行请求对象，HttpClient发送请求到目标服务器返回一个相应的响应对象，如果执行失败则抛出一个异常。所以，HttpClient ...&nbsp;&nbsp;<a href='http://www.cppblog.com/iuranus/archive/2010/07/04/119311.html'>阅读全文</a><img src ="http://www.cppblog.com/iuranus/aggbug/119311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/iuranus/" target="_blank">攀升</a> 2010-07-04 20:50 <a href="http://www.cppblog.com/iuranus/archive/2010/07/04/119311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转) AIDL --- Android中的远程接口</title><link>http://www.cppblog.com/iuranus/archive/2009/08/12/93040.html</link><dc:creator>攀升</dc:creator><author>攀升</author><pubDate>Wed, 12 Aug 2009 09:17:00 GMT</pubDate><guid>http://www.cppblog.com/iuranus/archive/2009/08/12/93040.html</guid><wfw:comment>http://www.cppblog.com/iuranus/comments/93040.html</wfw:comment><comments>http://www.cppblog.com/iuranus/archive/2009/08/12/93040.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/iuranus/comments/commentRss/93040.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/iuranus/services/trackbacks/93040.html</trackback:ping><description><![CDATA[<div class="blog_li" style="padding: 0px; text-align: center; margin-bottom: 10px;">
<strong style="font-size: 16px;">AIDL --- Android中的远程接口<br>http://labs.chinamobile.com/community/my_blog/517/4850<br><br></strong></div>
<strong style="font-size: 16px;">
&nbsp;&nbsp;&nbsp; 在Android中, 每个应用程序都可以有自己的进程. 在写UI应用的时候, 经常要用到Service.
在不同的进程中, 怎样传递对象呢?&nbsp; 显然, Java中不允许跨进程内存共享. 因此传递对象, 只能把对象拆分成操作系统能理解的简单形式, 以达到跨界对象访问的目的. 在J2EE中,采用RMI的方式, 可以通过序列化传递对象. 在Android中, 则采用AIDL的方式. 理论上AIDL可以传递Bundle,实际上做起来却比较麻烦.
</strong>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">AIDL(AndRoid接口描述语言)是一种借口描述语言; 编译器可以通过aidl文件生成一段代码，通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成AIDL可识别的参数(可能是多个参数),
然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象. </strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">AIDL的IPC的机制和COM或CORBA类似, 是基于接口的，但它是轻量级的。它使用代理类在客户端和实现层间传递值. 如果要使用AIDL, 需要完成2件事情: 1. 引入AIDL的相关类.; 2.
调用aidl产生的class.<a name=".E4.BD.BF.E7.94.A8AIDL.E5.AE.9E.E7.8E.B0"></a>
<p><strong style="font-size: 16px;">具体实现步骤如下: </strong></p>
<p><strong style="font-size: 16px;">1、创建AIDL文件,
在这个文件里面定义接口, 该接口定义了可供客户端访问的方法和属性。 如: ITaskBinder.adil</strong></p>
<p><strong style="font-size: 16px;">package com.cmcc.demo;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">import com.cmcc.demo.ITaskCallback;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">interface ITaskBinder {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
boolean isTaskRunning();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void stopRunningTask();&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void registerCallback(ITaskCallback cb);&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void unregisterCallback(ITaskCallback cb);</strong></p>
<p><strong style="font-size: 16px;">}</strong></p>
<p><strong style="font-size: 16px;">其中: ITaskCallback在文件ITaskCallback.aidl中定义:</strong></p>
<p><strong style="font-size: 16px;">package com.cmcc.demo;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">interface ITaskCallback {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; void
actionPerformed(int actionId);</strong></p>
<p><strong style="font-size: 16px;">}</strong></p>
<p><strong style="font-size: 16px;">注意: 理论上, 参数可以传递基本数据类型和String, 还有就是Bundle的派生类, 不过在Eclipse中,目前的ADT不支持Bundle做为参数, 据说用Ant编译可以, 我没做尝试.</strong></p>
<p><strong style="font-size: 16px;">2、编译AIDL文件,
用Ant的话, 可能需要手动, 使用Eclipse plugin的话,可以根据adil文件自动生产java文件并编译,
不需要人为介入. </strong></p>
<p><strong style="font-size: 16px;">3、在Java文件中,
实现AIDL中定义的接口. 编译器会根据AIDL接口, 产生一个JAVA接口。这个接口有一个名为Stub的内部抽象类，它继承扩展了接口并实现了远程调用需要的几个方法。接下来就需要自己去实现自定义的几个接口了.</strong></p>
<p><strong style="font-size: 16px;">ITaskBinder.aidl中接口的实现, 在MyService.java中接口以内嵌类的方式实现:</strong></p>
<p><strong style="font-size: 16px;"><strong>private</strong> <strong>final</strong> ITaskBinder.Stub mBinder = <strong>new</strong> ITaskBinder.Stub() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> stopRunningTask() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //@TODO</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>boolean</strong> isTaskRunning() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //@TODO</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> <strong>false</strong>;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> registerCallback(ITaskCallback cb) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (cb != <strong>null</strong>) mCallbacks.register(cb);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> unregisterCallback(ITaskCallback cb) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (cb != <strong>null</strong>) mCallbacks.unregister(cb);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">};</strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">在MyActivity.java中ITaskCallback.aidl接口实现:</strong></p>
<p><strong style="font-size: 16px;"><strong>private</strong> ITaskCallback mCallback = <strong>new</strong> ITaskCallback.Stub() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> actionPerformed(<strong>int</strong> id) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //TODO</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("callback id=" + id);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">};</strong></p>
<p><strong style="font-size: 16px;">4、向客户端提供接口ITaskBinder, 如果写的是service，扩展该Service并重载onBind ()方法来返回一个实现上述接口的类的实例。这个地方返回的mBinder,就是上面通过内嵌了定义的那个. (MyService.java)</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> IBinder onBind(Intent t) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service on bind");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> mBinder;</strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">}</strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">在Activity中, 可以通过Binder定义的接口, 来进行远程调用. </strong></p>
<p><strong style="font-size: 16px;"><a name=".E5.88.9B.E5.BB.BA.E4.B8.80.E4.B8.AAAIDL"></a>5、在服务器端回调客户端的函数. 前提是当客户端获取的IBinder接口的时候,要去注册回调函数, 只有这样, 服务器端才知道该调用那些函数在:MyService.java中:</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>void</strong> callback(<strong>int</strong> val) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>final</strong> <strong>int</strong> N = mCallbacks.beginBroadcast();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>for</strong> (<strong>int</strong> i=0; i&lt;N; i++) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>try</strong> {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mCallbacks.getBroadcastItem(i).actionPerformed(val);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <strong>catch</strong> (RemoteException e) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The RemoteCallbackList will take care of removing</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the dead object for us.</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mCallbacks.finishBroadcast();</strong></p>
<p style="text-indent: 21pt;"><strong style="font-size: 16px;">}</strong></p>
<p><strong style="font-size: 16px;">AIDL的创建方法:</strong></p>
<p><strong style="font-size: 16px;">AIDL语法很简单,可以用来声明一个带一个或多个方法的接口，也可以传递参数和返回值。
由于远程调用的需要, 这些参数和返回值并不是任何类型.下面是些AIDL支持的数据类型: </strong></p>
<p><strong style="font-size: 16px;">1. 不需要import声明的简单Java编程语言类型(int,boolean等) </strong></p>
<pre><strong style="font-size: 16px;">2. String, CharSequence不需要特殊声明</strong></pre>
<pre><strong style="font-size: 16px;"> </strong></pre>
<pre><strong style="font-size: 16px;">3. List, Map和Parcelables类型, 这些类型内所包含的数据成员也只能是简单数据类型, String等其他比支持的类型. </strong></pre>
<pre><strong style="font-size: 16px;">(</strong></pre>
<pre><strong style="font-size: 16px;">(另外: 我没尝试Parcelables, 在Eclipse+ADT下编译不过, 或许以后会有所支持).</strong></pre>
<pre><strong style="font-size: 16px;">下面是AIDL语法:</strong></pre>
<pre><strong style="font-size: 16px;"> // 文件名: SomeClass.aidl</strong></pre>
<pre><strong style="font-size: 16px;"> // 文件可以有注释, 跟java的一样</strong></pre>
<pre><strong style="font-size: 16px;"> // 在package以前的注释, 将会被忽略.</strong></pre>
<pre><strong style="font-size: 16px;"> // 函数和变量以前的注释, 都会被加入到生产java代码中.</strong></pre>
<pre><strong style="font-size: 16px;">package com.cmcc.demo;</strong></pre>
<pre><strong style="font-size: 16px;"> // import 引入语句</strong></pre>
<p><strong style="font-size: 16px;">import com.cmcc.demo.ITaskCallback;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">interface ITaskBinder {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; //函数跟java一样, 可以有0到多个参数 ,可以有一个返回值</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
boolean isTaskRunning();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void stopRunningTask();&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; //参数可以是另外的一个aidl定义的接口</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void registerCallback(ITaskCallback cb);&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">void
unregisterCallback(ITaskCallback cb);</strong></p>
<p><strong style="font-size: 16px;">//参数可以是String, 可以用in表入输入类型, out表示输出类型.</strong></p>
<pre style="text-indent: 18pt;"><strong style="font-size: 16px;">int getCustomerList(in String branch, out String[] customerList);</strong></pre>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<pre><strong style="font-size: 16px;">} </strong></pre>
<p><strong style="font-size: 16px;"><a name=".E5.AE.9E.E7.8E.B0.E6.8E.A5.E5.8F.A3"></a>实现接口时有几个原则:
</strong></p>
<pre><strong style="font-size: 16px;">.抛出的异常不要返回给调用者. 跨进程抛异常处理是不可取的.</strong></pre>
<pre><strong style="font-size: 16px;">.IPC调用是同步的。如果你知道一个IPC服务需要超过几毫秒的时间才能完成地话，你应该避免在Activity的主线程中调用。 也就是IPC调用会挂起应用程序导致界面失去响应. 这种情况应该考虑单起一个线程来处理.</strong></pre>
<pre><strong style="font-size: 16px;">.不能在AIDL接口中声明静态属性。</strong></pre>
<p><strong style="font-size: 16px;"><a name=".E5.90.91.E5.AE.A2.E6.88.B7.E7.AB.AF.E5."></a><a name=".E4.BD.BF.E7.94.A8parcelables.E8.BF.9B.E"></a>IPC的调用步骤: </strong></p>
<pre><strong style="font-size: 16px;">&nbsp;1. 声明一个接口类型的变量，该接口类型在.aidl文件中定义。</strong></pre>
<pre><strong style="font-size: 16px;"> 2. 实现ServiceConnection。</strong></pre>
<pre><strong style="font-size: 16px;"> 3. 调用ApplicationContext.bindService(),并在ServiceConnection实现中进行传递. </strong></pre>
<pre><strong style="font-size: 16px;">&nbsp;4. 在ServiceConnection.onServiceConnected()实现中，你会接收一个IBinder实例(被调用的Service). 调用</strong></pre>
<pre><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; YourInterfaceName.Stub.asInterface((IBinder)service)将参数转换为YourInterface类型。</strong></pre>
<pre><strong style="font-size: 16px;"> 5. 调用接口中定义的方法。 你总要检测到DeadObjectException异常，该异常在连接断开时被抛出。它只会被远程方法抛出。</strong></pre>
<pre><strong style="font-size: 16px;"> 6. 断开连接，调用接口实例中的ApplicationContext.unbindService()</strong></pre>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">下面是整个程序:</strong></p>
<p><strong style="font-size: 16px;"><strong>1. </strong><strong>ITaskCallback.aidl</strong></strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">package com.cmcc.demo;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">interface ITaskCallback {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; void
actionPerformed(int actionId);</strong></p>
<p><strong style="font-size: 16px;">}</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>2.
ITaskBinder.aidl</strong></strong></p>
<p><strong style="font-size: 16px;">package com.cmcc.demo;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">import com.cmcc.demo.ITaskCallback;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">interface ITaskBinder {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
boolean isTaskRunning();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void stopRunningTask();&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void registerCallback(ITaskCallback cb);&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;
void unregisterCallback(ITaskCallback cb);</strong></p>
<p><strong style="font-size: 16px;">}</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>3. &nbsp;MyService.java</strong></strong></p>
<p><strong style="font-size: 16px;"><strong>package</strong> com.cmcc.demo;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.app.Service;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.content.Intent;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.os.IBinder;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.os.RemoteCallbackList;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.os.RemoteException;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.util.Log;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>public</strong> <strong>class</strong> MyService <strong>extends</strong> Service {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; @Override</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onCreate() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service create");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; @Override</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onStart(Intent <u>intent</u>, <strong>int</strong> startId) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service start id=" + startId);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; callback(startId);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; @Override</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> IBinder onBind(Intent t) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service on bind");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> mBinder;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; @Override</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onDestroy() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service on destroy");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>super</strong>.onDestroy();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; @Override</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>boolean</strong> onUnbind(Intent intent) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service on unbind");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> <strong>super</strong>.onUnbind(intent);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onRebind(Intent intent) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("service on rebind");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>super</strong>.onRebind(intent);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>void</strong> printf(String str) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log.<em>e</em>("TAG", "###################------ " + str + "------");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>void</strong> callback(<strong>int</strong> val) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>final</strong> <strong>int</strong> N = mCallbacks.beginBroadcast();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>for</strong> (<strong>int</strong> i=0; i&lt;N; i++) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>try</strong> {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mCallbacks.getBroadcastItem(i).actionPerformed(val);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <strong>catch</strong> (RemoteException e) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The RemoteCallbackList will take care of removing</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the dead object for us.</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mCallbacks.finishBroadcast();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>final</strong> ITaskBinder.Stub mBinder = <strong>new</strong> ITaskBinder.Stub() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> stopRunningTask() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>boolean</strong> isTaskRunning() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>return</strong> <strong>false</strong>;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> registerCallback(ITaskCallback cb) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (cb != <strong>null</strong>) mCallbacks.register(cb);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> unregisterCallback(ITaskCallback cb) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong> (cb != <strong>null</strong>) mCallbacks.unregister(cb);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; };</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>final</strong> RemoteCallbackList&lt;ITaskCallback&gt; mCallbacks</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = <strong>new</strong> RemoteCallbackList&lt;ITaskCallback&gt;();</strong></p>
<p><strong style="font-size: 16px;">}</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>4.
MyActivity.java</strong></strong></p>
<p><strong style="font-size: 16px;"><strong>package</strong> com.cmcc.demo;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.app.Activity;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.content.ComponentName;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.content.Context;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.content.Intent;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.content.ServiceConnection;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.graphics.Color;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.os.Bundle;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.os.IBinder;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.os.RemoteException;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.util.Log;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.view.View;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.view.ViewGroup;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.view.View.OnClickListener;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.widget.AbsoluteLayout;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.widget.Button;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.widget.LinearLayout;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.widget.RelativeLayout;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> android.widget.TextView;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> java.io.BufferedReader;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> java.io.File;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> java.io.FileOutputStream;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> java.io.FileReader;</strong></p>
<p><strong style="font-size: 16px;"><strong>import</strong> java.io.PrintWriter;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;"><strong>public</strong> <strong>class</strong> MyActivity
<strong>extends</strong> Activity {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> Button btnOk;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> Button btnCancel;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; @Override</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onCreate(Bundle icicle) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>super</strong>.onCreate(icicle);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setContentView(R.layout.<em>test_service</em>);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnOk = (Button)findViewById(R.id.<em>btn_ok</em>);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnCancel = (Button)findViewById(R.id.<em>btn_cancel</em>);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnOk.setText("Start Service");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnCancel.setTag("Stop Service");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnOk.setOnClickListener(<strong>new</strong> OnClickListener()
{</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onClick(View v) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onOkClick();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnCancel.setOnClickListener(<strong>new</strong> OnClickListener()
{</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onClick(View v) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onCancelClick();</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>void</strong> onOkClick() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bundle args = <strong>new</strong> Bundle();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Intent intent = <strong>new</strong> Intent(<strong>this</strong>, MyService.<strong>class</strong>);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intent.putExtras(args);&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //printf("send intent to start");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //startService(intent);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bindService(intent, mConnection, Context.<em>BIND_AUTO_CREATE</em>);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; startService(intent);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>void</strong> onCancelClick() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Intent intent = <strong>new</strong> Intent(<strong>this</strong>, MyService.<strong>class</strong>);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //printf("send intent to stop");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unbindService(mConnection);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //stopService(intent);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> <strong>void</strong> printf(String str) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log.<em>e</em>("TAG", "###################------ " + str + "------");</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; ITaskBinder mService;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> ServiceConnection mConnection = <strong>new</strong>
ServiceConnection() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onServiceConnected(ComponentName className,</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IBinder service) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mService = ITaskBinder.Stub.<em>asInterface</em>(service);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>try</strong> {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mService.registerCallback(mCallback);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <strong>catch</strong> (RemoteException e) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> onServiceDisconnected(ComponentName
className) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mService = <strong>null</strong>;</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; };</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; </strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; <strong>private</strong> ITaskCallback mCallback = <strong>new</strong>
ITaskCallback.Stub() {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>public</strong> <strong>void</strong> actionPerformed(<strong>int</strong> id) {</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("callback id=" + id);</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</strong></p>
<p><strong style="font-size: 16px;">&nbsp;&nbsp;&nbsp; };</strong></p>
<strong style="font-size: 16px;"><strong>}</strong>
</strong><br><img src ="http://www.cppblog.com/iuranus/aggbug/93040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/iuranus/" target="_blank">攀升</a> 2009-08-12 17:17 <a href="http://www.cppblog.com/iuranus/archive/2009/08/12/93040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android Framework architecture -- bluez(2) </title><link>http://www.cppblog.com/iuranus/archive/2009/08/07/92522.html</link><dc:creator>攀升</dc:creator><author>攀升</author><pubDate>Fri, 07 Aug 2009 04:42:00 GMT</pubDate><guid>http://www.cppblog.com/iuranus/archive/2009/08/07/92522.html</guid><wfw:comment>http://www.cppblog.com/iuranus/comments/92522.html</wfw:comment><comments>http://www.cppblog.com/iuranus/archive/2009/08/07/92522.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/iuranus/comments/commentRss/92522.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/iuranus/services/trackbacks/92522.html</trackback:ping><description><![CDATA[<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bluetooth signal on成功后，要做的就是查找scan了，scan涉及到的东西，如下图：<br><br><img height=611 alt="" src="http://www.cppblog.com/images/cppblog_com/iuranus/bluetooth_setting.jpg" width=656 border=0><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class bluetoothsetting是UI的入口，通过按button scan进入搜索状态，applicaton层调用bluetoothdevice, 接着就是bluetoothservice的调用，&nbsp;bluetoothservice调用native方法，到此全部的java程序结束了。下面的调用都是JNI, cpp实现的。android_server_bluetoothservice.cpp里面实现了navtive方法，最终通过dbus封装，调用HCID deamon 的function DiscoverDevice.</p>
<img src ="http://www.cppblog.com/iuranus/aggbug/92522.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/iuranus/" target="_blank">攀升</a> 2009-08-07 12:42 <a href="http://www.cppblog.com/iuranus/archive/2009/08/07/92522.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Android Framework architecture -- bluez(1)</title><link>http://www.cppblog.com/iuranus/archive/2009/08/04/92109.html</link><dc:creator>攀升</dc:creator><author>攀升</author><pubDate>Mon, 03 Aug 2009 16:12:00 GMT</pubDate><guid>http://www.cppblog.com/iuranus/archive/2009/08/04/92109.html</guid><wfw:comment>http://www.cppblog.com/iuranus/comments/92109.html</wfw:comment><comments>http://www.cppblog.com/iuranus/archive/2009/08/04/92109.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/iuranus/comments/commentRss/92109.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/iuranus/services/trackbacks/92109.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要:        android一词的本义指“机器人”，和robot不同的是它是有AI的，那么同时google也接android这个单词，于2007年11月5日宣布推出基于Linux平台的开源手机操作系统，该平台由操作系统、中间件、用户界面和应用软件组成，号称是首个为移动终端打造的真正开放和完整的移动软件。<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/iuranus/archive/2009/08/04/92109.html'>阅读全文</a><img src ="http://www.cppblog.com/iuranus/aggbug/92109.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/iuranus/" target="_blank">攀升</a> 2009-08-04 00:12 <a href="http://www.cppblog.com/iuranus/archive/2009/08/04/92109.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>