Android中Service的使用详解

来源:互联网 发布:mac地址怎么查苹果手机 编辑:程序博客网 时间:2024/05/19 19:43

开始,先稍稍讲一点android中Service的概念和用途吧~

Service分为本地服务(LocalService)和远程服务(RemoteService):

1、本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外Local服务因为是在同一进程因此不需要IPC,

也不需要AIDL。相应bindService会方便很多。主进程被Kill后,服务便会终止。

2、远程服务为独立的进程,对应进程名格式为所在包名加上你指定的android:process字符串。由于是独立的进程,因此在Activity所在进程被Kill的时候,该服务依然在运行,

不受其他进程影响,有利于为多个进程提供服务具有较高的灵活性。该服务是独立的进程,会占用一定资源,并且使用AIDL进行IPC稍微麻烦一点。

按使用方式可以分为以下三种:

1、startService 启动的服务:主要用于启动一个服务执行后台任务,不进行通信。停止服务使用stopService;

2、bindService 启动的服务:该方法启动的服务可以进行通信。停止服务使用unbindService;

3、startService 同时也 bindService 启动的服务:停止服务应同时使用stepService与unbindService

 

 

Service 与 Thread 的区别

很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下。

1). Thread:Thread 是程序执行的最小单元,它是分配CPU的基本单位。可以用 Thread 来执行一些异步的操作。

2). Service:Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 Service 是运行在主进程的 main 线程上的。如:onCreate,onStart 这些函数在被系统调用的时候都是在主进程的 main 线程上运行的。如果是Remote Service,那么对应的 Service 则是运行在独立进程的 main 线程上。因此请不要把 Service 理解成线程,它跟线程半毛钱的关系都没有!

既然这样,那么我们为什么要用 Service 呢?其实这跟 android 的系统机制有关,我们先拿 Thread 来说。Thread 的运行是独立于 Activity 的,也就是说当一个 Activity 被 finish 之后,如果你没有主动停止 Thread 或者 Thread 里的 run 方法没有执行完毕的话,Thread 也会一直执行。因此这里会出现一个问题:当 Activity 被 finish 之后,你不再持有该 Thread 的引用。另一方面,你没有办法在不同的 Activity 中对同一 Thread 进行控制。

举个例子:如果你的 Thread 需要不停地隔一段时间就要连接服务器做某种同步的话,该 Thread 需要在 Activity 没有start的时候也在运行。这个时候当你 start 一个 Activity 就没有办法在该 Activity 里面控制之前创建的 Thread。因此你便需要创建并启动一个 Service ,在 Service 里面创建、运行并控制该 Thread,这样便解决了该问题(因为任何 Activity 都可以控制同一 Service,而系统也只会创建一个对应 Service 的实例)。

 

因此你可以把 Service 想象成一种消息服务,而你可以在任何有 Context 的地方调用 Context.startService、Context.stopService、Context.bindService,Context.unbindService,来控制它,你也可以在 Service 里注册 BroadcastReceiver,在其他地方通过发送 broadcast 来控制它,当然这些都是 Thread 做不到的。

 

Service的生命周期

onCreate  onStart  onDestroy  onBind

1). 被启动的服务的生命周期:如果一个Service被某个Activity 调用 Context.startService 方法启动,那么不管是否有Activity使用bindService绑定或unbindService解除绑定到该Service,该Service都在后台运行。如果一个Service被startService 方法多次启动,那么onCreate方法只会调用一次,onStart将会被调用多次(对应调用startService的次数),并且系统只会创建Service的一个实例(因此你应该知道只需要一次stopService调用)。该Service将会一直在后台运行,而不管对应程序的Activity是否在运行,直到被调用stopService,或自身的stopSelf方法。当然如果系统资源不足,android系统也可能结束服务。

2). 被绑定的服务的生命周期:如果一个Service被某个Activity 调用 Context.bindService 方法绑定启动,不管调用 bindService 调用几次,onCreate方法都只会调用一次,同时onStart方法始终不会被调用。当连接建立之后,Service将会一直运行,除非调用Context.unbindService 断开连接或者之前调用bindService 的 Context 不存在了(如Activity被finish的时候),系统将会自动停止Service,对应onDestroy将被调用。

3). 被启动又被绑定的服务的生命周期:如果一个Service又被启动又被绑定,则该Service将会一直在后台运行。并且不管如何调用,onCreate始终只会调用一次,对应startService调用多少次,Service的onStart便会调用多少次。调用unbindService将不会停止Service,而必须调用 stopService 或 Service的 stopSelf 来停止服务。

4). 当服务被停止时清除服务:当一个Service被终止(1、调用stopService;2、调用stopSelf;3、不再有绑定的连接(没有被启动))时,onDestroy方法将会被调用,在这里你应当做一些清除工作,如停止在Service中创建并运行的线程。

特别注意:

1、你应当知道在调用 bindService 绑定到Service的时候,你就应当保证在某处调用 unbindService 解除绑定(尽管 Activity 被 finish 的时候绑定会自      动解除,并且Service会自动停止);

2、你应当注意 使用 startService 启动服务之后,一定要使用 stopService停止服务,不管你是否使用bindService;

3、同时使用 startService 与 bindService 要注意到,Service 的终止,需要unbindService与stopService同时调用,才能终止 Service,不管 startService 与 bindService 的调用顺序,如果先调用 unbindService 此时服务不会自动终止,再调用 stopService 之后服务才会停止,如果先调用 stopService 此时服务也不会终止,而再调用 unbindService 或者 之前调用 bindService 的 Context 不存在了(如Activity 被 finish 的时候)之后服务才会自动停止;

4、当在旋转手机屏幕的时候,当手机屏幕在“横”“竖”变换时,此时如果你的 Activity 如果会自动旋转的话,旋转其实是 Activity 的重新创建,因此旋转之前的使用 bindService 建立的连接便会断开(Context 不存在了),对应服务的生命周期与上述相同。

5、在 sdk 2.0 及其以后的版本中,对应的 onStart 已经被否决变为了 onStartCommand,不过之前的 onStart 任然有效。这意味着,如果你开发的应用程序用的 sdk 为 2.0 及其以后的版本,那么你应当使用 onStartCommand 而不是 onStart。

 

下面开始上一个很简单的代码哈~里头的注释也要注意哦,有在上面没有讲到的会在注释里提到哇(尤其适用Bind方法的时候的数据传输哇)~

首先,因为要再Manifest文件里对服务进行注册,所以就先来Manifest的代码吧~

 

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     package="com.test.localservice"android:versionCode="1" 
  4.     android:versionName="1.0"> 
  5.     <uses-sdkandroid:minSdkVersion="8"/> 
  6.  
  7.     <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> 
  8.         <activityandroid:name=".LocalServiceTestActivity" 
  9.             android:label="@string/app_name"> 
  10.             <intent-filter> 
  11.                 <actionandroid:name="android.intent.action.MAIN"/> 
  12.                 <categoryandroid:name="android.intent.category.LAUNCHER"/> 
  13.             </intent-filter> 
  14.         </activity> 
  15.         <serviceandroid:name=".MyService"> 
  16.             <intent-filter> 
  17.                 <actionandroid:name="com.test.SERVICE_TEST"/> 
  18.                 <categoryandroid:name="android.intent.category.default"/> 
  19.             </intent-filter> 
  20.         </service> 
  21.     </application> 
  22. </manifest> 
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.test.localservice" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".LocalServiceTestActivity"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".MyService"><intent-filter><action android:name="com.test.SERVICE_TEST" /><category android:name="android.intent.category.default" /></intent-filter></service></application></manifest>

然后然后,是服务实现类~

 

 

  1.  
</pre><div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; overflow: auto; word-break: break-word; line-height: 18px; margin: 18px 0px !important;"><ol class="dp-j" style="padding-left: 40px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">package</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> com.test.service; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.app.Service; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.content.Intent; </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.os.Binder; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.os.IBinder; </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.util.Log; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyService </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Service { </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> LocalBinder </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Binder { </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        String stringToSend = <span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"I'm the test String"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        MyService getService() { </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">            Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"getService ---> "</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> + MyService.</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">            <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> MyService.</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        } </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">private</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">final</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> IBinder mBinder = </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> LocalBinder(); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> IBinder onBind(Intent intent) { </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"onBind~~~~~~~~~~~~"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//      IBinder myIBinder = null;</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//      if ( null == myIBinder ) </span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//          myIBinder = new LocalBinder() ; </span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//      return myIBinder;</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">return</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> mBinder;     </span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//也可以像上面几个语句那样重新new一个IBinder</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//如果这边不返回一个IBinder的接口实例,那么ServiceConnection中的onServiceConnected就不会被调用</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//那么bind所具有的传递数据的功能也就体现不出来~\(≧▽≦)/~啦啦啦(这个返回值是被作为onServiceConnected中的第二个参数的)</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onCreate() { </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onCreate(); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"onCreate~~~~~~~~~~"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onDestroy() { </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onDestroy(); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"onDestroy~~~~~~~~~~~"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onStart(Intent intent, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> startId) { </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onStart(intent, startId); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"onStart~~~~~~"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onStartCommand(Intent intent, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> flags, </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> startId) { </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"onStartCommand~~~~~~~~~~~~"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">return</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onStartCommand(intent, flags, startId); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="annotation" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">boolean</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onUnbind(Intent intent) { </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">// TODO Auto-generated method stub</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        Log.i(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"TAG"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"onUnbind~~~~~~~~~~~~~~~~"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">return</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onUnbind(intent); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; background-color: rgb(248, 248, 248); line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">    } </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">} </span></li></ol></div><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; line-height: 18px; background-color: rgb(255, 255, 255);">package com.test.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyService extends Service {public class LocalBinder extends Binder {String stringToSend = "I'm the test String";MyService getService() {Log.i("TAG", "getService ---> " + MyService.this);return MyService.this;}}private final IBinder mBinder = new LocalBinder();@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i("TAG", "onBind~~~~~~~~~~~~");//IBinder myIBinder = null;//if ( null == myIBinder ) //myIBinder = new LocalBinder() ; //return myIBinder;return mBinder;//也可以像上面几个语句那样重新new一个IBinder//如果这边不返回一个IBinder的接口实例,那么ServiceConnection中的onServiceConnected就不会被调用//那么bind所具有的传递数据的功能也就体现不出来~\(≧▽≦)/~啦啦啦(这个返回值是被作为onServiceConnected中的第二个参数的)}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i("TAG", "onCreate~~~~~~~~~~");}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.i("TAG", "onDestroy~~~~~~~~~~~");}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);Log.i("TAG", "onStart~~~~~~");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i("TAG", "onStartCommand~~~~~~~~~~~~");return super.onStartCommand(intent, flags, startId);}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubLog.i("TAG", "onUnbind~~~~~~~~~~~~~~~~");return super.onUnbind(intent);}}

再来,就是我们的Activity的测试类啦~

 

 

  1. <pre class="java" name="code">package com.test.service; 
  2.  
  3. import android.app.Activity; 
  4. import android.content.ComponentName; 
  5. import android.content.Context; 
  6. import android.content.Intent; 
  7. import android.content.ServiceConnection; 
  8. import android.media.MediaPlayer; 
  9. import android.os.Bundle; 
  10. import android.os.IBinder; 
  11. import android.util.Log; 
  12. import android.view.View; 
  13. import android.view.View.OnClickListener; 
  14. import android.widget.Button; 
  15.  
  16. publicclass ServiceTestActivity extends Activity { 
  17.     private Button startButton, bindButton; 
  18.     private Button stopButton, unbindButton; 
  19.     private ServiceConnection sc; 
  20.     private MediaPlayer mediaPlayer = null
  21.     private MyService myService;// 类似于MediaPlayer mPlayer = new 
  22.                                 // MediaPlayer();只不过这边的服务是自定义的,不是系统提供好了的 
  23.  
  24.     /** Called when the activity is first created. */ 
  25.     @Override 
  26.     publicvoid onCreate(Bundle savedInstanceState) { 
  27.         super.onCreate(savedInstanceState); 
  28.         setContentView(R.layout.main); 
  29.  
  30.         startButton = (Button) findViewById(R.id.startbutton_id); 
  31.         stopButton = (Button) findViewById(R.id.stopbutton_id); 
  32.         bindButton = (Button) findViewById(R.id.bindbutton_id); 
  33.         unbindButton = (Button) findViewById(R.id.unbindbutton_id); 
  34.  
  35.         sc = new ServiceConnection() { 
  36.             /*
  37.              * 只有在MyService中的onBind方法中返回一个IBinder实例才会在Bind的时候
  38.              * 调用onServiceConnection回调方法
  39.              * 第二个参数service就是MyService中onBind方法return的那个IBinder实例,可以利用这个来传递数据
  40.              */ 
  41.             @Override 
  42.             publicvoid onServiceConnected(ComponentName name, IBinder service) { 
  43.                 // TODO Auto-generated method stub 
  44.                 myService = ((MyService.LocalBinder) service).getService(); 
  45.                 String recStr = ((MyService.LocalBinder) service).stringToSend; 
  46.                 //利用IBinder对象传递过来的字符串数据(其他数据也可以啦,哪怕是一个对象也OK~~) 
  47.                 Log.i("TAG","The String is : " + recStr); 
  48.                 Log.i("TAG""onServiceConnected : myService ---> " + myService); 
  49.             } 
  50.  
  51.             @Override 
  52.             publicvoid onServiceDisconnected(ComponentName name) { 
  53.                 /* SDK上是这么说的:
  54.                  * This is called when the connection with the service has been unexpectedly disconnected
  55.                  * that is, its process crashed. Because it is running in our same process, we should never see this happen.
  56.                  * 所以说,只有在service因异常而断开连接的时候,这个方法才会用到*/ 
  57.                 // TODO Auto-generated method stub 
  58.                 sc = null
  59.                 Log.i("TAG""onServiceDisconnected : ServiceConnection --->" 
  60.                         + sc); 
  61.             } 
  62.  
  63.         }; 
  64.         startButton.setOnClickListener(new OnClickListener() { 
  65.  
  66.             @Override 
  67.             publicvoid onClick(View v) { 
  68.                 // TODO Auto-generated method stub 
  69.                 Intent intent = new Intent(ServiceTestActivity.this
  70.                         MyService.class); 
  71.                 startService(intent); 
  72.                 Log.i("TAG""Start button clicked"); 
  73.             } 
  74.         }); 
  75.  
  76.         stopButton.setOnClickListener(new OnClickListener() { 
  77.  
  78.             @Override 
  79.             publicvoid onClick(View v) { 
  80.                 // TODO Auto-generated method stub 
  81.  
  82.                 /*
  83.                  * Intent intent = new
  84.                  * Intent(LocalServiceTestActivity.this,MyService.class);
  85.                  * stopService(intent); 这种方法也是可以的哈~
  86.                  */ 
  87.  
  88.                 Intent intent = new Intent(); 
  89.                 intent.setAction("com.test.SERVICE_TEST"); 
  90.                 stopService(intent); 
  91.                 Log.i("TAG""Stop Button clicked"); 
  92.             } 
  93.         }); 
  94.  
  95.         bindButton.setOnClickListener(new OnClickListener() { 
  96.  
  97.             @Override 
  98.             publicvoid onClick(View v) { 
  99.                 // TODO Auto-generated method stub 
  100. //              Intent intent = new Intent(LocalServiceTestActivity.this, 
  101. //                      MyService.class);//这样也可以的 
  102.                 Intent intent = new Intent(); 
  103.                 intent.setAction("com.test.SERVICE_TEST"); 
  104.                 bindService(intent, sc, Context.BIND_AUTO_CREATE);//bind多次也只会调用一次onBind方法 
  105.                 Log.i("TAG""Bind button clicked"); 
  106.             } 
  107.         }); 
  108.  
  109.         unbindButton.setOnClickListener(new OnClickListener() { 
  110.  
  111.             @Override 
  112.             publicvoid onClick(View v) { 
  113.                 // TODO Auto-generated method stub 
  114.                 unbindService(sc); 
  115.                 // 这边如果重复unBind会报错,提示该服务没有注册的错误——IllegalArgumentException: 
  116.                 // Service not registered: null 
  117.                 // 所以一般会设置一个flag去看这个service 
  118.                 // bind后有没有被unBind过,没有unBind过才能调用unBind方法(这边我就不设置了哈~\(≧▽≦)/~啦啦啦) 
  119.                 Log.i("TAG""Unbind Button clicked"); 
  120.             } 
  121.         }); 
  122.     } 
  123. }</pre><br> 
  124. <br> 
  125. <pre></pre> 
  126. <br> 
  127. 相信开头的介绍和代码里的注释应该对大家理解和使用Service有所帮助哈~不过这边就先只讲LocalService吧,剩下的RemoteService就等下次在说喽~ 
  128. <p></p> 
  129. <p><span style="white-space: pre;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="white-space: pre;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="white-space: pre;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="white-space: pre;"><span style="line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="color: rgb(51, 51, 51); line-height: 24px; font-family: verdana,宋体,Arial; font-size: 13px;"><span style="white-space: pre;"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩识字量少怎么办 父母打架孩子该怎么办 小孩嘴唇里面烂怎么办 幼儿园小朋友很调皮怎么办 幼儿园小朋友上课调皮怎么办 孩子误冲游戏怎么办 遇到别的熊孩子怎么办 幼儿园遇到熊孩子怎么办 高铁上遇到熊孩子怎么办 幼儿爱打人家长怎么办 妈妈爱打孩子怎么办 35儿童爱打人怎么办? 一岁半宝宝太调皮怎么办 儿子高一不听话怎么办 小孩说了不听话怎么办 我的妈妈文盲怎么办 电脑键盘反拼音怎么办 小孩学习态度差怎么办 孩子不好好上学怎么办 小孩读书态度不好怎么办 幼儿园孩子不认识数字怎么办 一年级孩子拼音很差怎么办 孩子的拼音不好怎么办 小孩不会拼拼音怎么办 小孩拼音学不会怎么办 儿童l发音不准怎么办 小孩发音不标准怎么办 拼音l发音不准怎么办 孩子拼音声调分不清怎么办 小孩gk读成dt怎么办 拼音音调学不会怎么办 会拼音不会打字怎么办 大人拼音学不会怎么办 志愿服务经历少怎么办 医保报销发票丢失怎么办 费用发票丢失了怎么办 小孩乱拿东西怎么办 在家突然生了怎么办 二胎在家生的怎么办? 奶有一边没有怎么办 孩子应用题很弱怎么办