当前位置:操作系统 > 安卓/Android >>

Android中Service初探

Service是在一段不定的时间运行在后台,不和用户交互应用组件。每个Service必须在manifest中 通过<service>来声明。可以通过contect.startservice和contect.bindserverice来启动。
Service和其他的应用组件一样,运行在进程的主线程中。这就是说如果service需要很多耗时或者阻塞的操作,需要在其子线程中实现。

 

service的两种模式

 


本地服务 Local Service 用于应用程序内部。
它可以启动并运行,直至有人停止了它或它自己停止。在这种方式下,它以调用Context.startService()启动,而以调用Context.stopService()结束。它可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。不论调用了多少次startService()方法,你只需要调用一次stopService()来停止服务。

用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好。

 


远程服务 Remote Service 用于android系统内部的应用程序之间。
它可以通过自己定义并暴露出来的接口进行程序操作。客户端建立一个到服务对象的连接,并通过那个连接来调用服务。连接以调用Context.bindService()方法建立,以调用 Context.unbindService()关闭。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载它。
可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可。
startService()/bindService()不是完全分离的.


生命周期
Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。


startService和bindService的区别
1. 使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。
如果打算采用Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。
如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。
采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。


2. 使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。
onBind()只有采用Context.bindService()方法启动服务时才会回调该方法。该方法在调用者与服务绑定时被调用,当调用者与服务已经绑定,多次调用Context.bindService()方法并不会导致该方法被多次调用。
采用Context.bindService()方法启动服务时只能调用onUnbind()方法解除调用者与服务解除,服务结束时会调用onDestroy()方法。

 

利用service进行音乐播放的例子

先看最终效果

 \


代码清单:

布局文件

[html]
<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:orientation="vertical"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    >   
    <TextView     
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content"    
       android:text="Welcome to my blog!"   
       android:textSize="16sp"/>      
    <TextView     
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content"    
       android:text="音乐播放服务"/>   
    <Button   
       android:id="@+id/startMusic"    
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"   
       android:text="开启音乐播放服务"/>   
    <Button   
       android:id="@+id/stopMusic"    
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"   
       android:text="停止音乐播放服务"/>   
   <Button   
      android:id="@+id/bindMusic"    
      android:layout_width="wrap_content"   
      android:layout_height="wrap_content"   
      android:text="绑定音乐播放服务"/>   
   <Button   
      android:id="@+id/unbindMusic"    
      android:layout_width="wrap_content"   
      android:layout_height="wrap_content"   
      android:text="解除 ——绑定音乐播放服务"/>   
</LinearLayout>   

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView   
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content"  
       android:text="Welcome to my blog!" 
       android:textSize="16sp"/>    
    <TextView   
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content"  
       android:text="音乐播放服务"/> 
    <Button 
       android:id="@+id/startMusic"  
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="开启音乐播放服务"/> 
    <Button 
   

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,