Android中的Service

来源:互联网 发布:梅雨知时节作品 编辑:程序博客网 时间:2024/05/31 15:18

一、概述

首先,service是安卓中四大组件之一,主要是用于为程序开启一个服务。可能有童鞋没听说过服务,我下面给大家简单介绍一下,大家一定知道后台服务吧,就是程序关闭后依然运行的"进程",比如你关掉QQ后,当接收到新的消息,后台服务会向前端发出提示,这时你可以通过这个服务再次打开对应的窗体。


二、分类

service分为本地服务和远程服务。

本地服务是指依附在主线程的独立线程,如QQ的关闭后仍然接收消息。(我们常用)

远程服务指的是独立进程,一般较少出现,多是系统服务。

三、血缘基因


看上去service和Thread很像是吧?实际这两个根本没有一点血缘关系,相当于后妈带来的一个兄弟。


Thread:程序的最小执行单元,这里不多介绍,但是Android中的线程依然是采取时间片轮转的方式抢占资源

Service:android的一种机制,它的运行依附在主进程的 main 线程上的。如:onCreate,onStart 这些函数在被系统调用的时候都是在主进程的 main线程上运行的。

相信童鞋们又迷茫了,那我们为什么要用 Service 呢?举个例子,如果我们需要我们的程序每隔一段时间就跟服务器做一次数据同步,而数据同步是在Activity没有创建的时候执行,而Thread必须在Activity 的 OnCreate执行之后,才能创建和执行,并且与该程序同在,程序销毁Thread销毁。这时候Service站了出来,它解决了这个困难。

四、生命周期

onCreate(创建)  onStart(启动)  onDestroy(销毁)  onBind (绑定)

系统可以创建多个Service,但对于同名Service之创建一次

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

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

3.启动后又绑定的服务生命周期先启动后绑定的Service,仍然只会调用一次onCreate,因为它们是同一Service。Service将会一直运行,直到解除绑定后再执行停止(有先后顺序),绑定状态下无法调用停止,但是系统可以给清掉。绑定后退出程序,会报一个错误(可忽略),此时service依然在运行状态,只是不再是绑定的。

注意:

1.作为一个优秀的程序员,你必须要控制好生命周期,在合适的时间接触绑定和关掉服务,以避免异常出现和资源浪费。

2.绑定的service的生命周期,要注意Activity的生命周期,在Activity销毁的时候,service也会销毁,比如屏幕翻转,屏幕后台被清掉。


----------------------------------------------------华丽的分割线----------------------------------------------------


五、代码实现

1.创建自己的Service

/*** * 继承Service的子类,重写他的四个方法 * @author bo * */public class MyService extends Service{private String TAG = "MyService";private MyBinder mBinder = new MyBinder();/** * 绑定使用 */@Overridepublic IBinder onBind(Intent arg0) {return mBinder;}/*** * 创建 */@Overridepublic void onCreate() {super.onCreate();Log.d(TAG, "OnCreate() executed!");}/**** * 启动 */@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand() executed!");return super.onStartCommand(intent, flags, startId);}/*** * 删除 */@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy() executed!");}/**** * 继承抽象类的对象,用来返回IBinder
 * Binder是Ibinder的子类 * @author bo * */public class MyBinder extends Binder{public void startDownloadMp3(){Log.d(TAG, "startDownloadMp3() executed!");}}}
2.注册,配置AndroidManifest.xml

添加以下代码,和注册Activity一致

<service android:name="com.ambow.service.MyService">            </service>

3.启动、停止、绑定、解绑

启动

Intent intent = new Intent(this,MyService.class);startService(intent);


停止

Intent intent = new Intent(this,MyService.class);stopService(intent);


绑定、解绑

/*** * 创建ServiceConnection对象,用于绑定 */private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyService.MyBinder myBind = (MyService.MyBinder)service;myBind.startDownloadMp3();}};

----------------------------------------------------------------------

Intent intent = new Intent(this,MyService.class);bindService(intent, conn, BIND_AUTO_CREATE);//绑定unbindService(conn);//解绑





2 0