Service简介

创建时需要

1.)定义一个继承Service的子类

2.)在AndroidManifest.xml文件中配置Service

生命周期及主要回调方法

abstract IBinder onBind(Intent intent)

  该方法是Service子类必须实现的方法,该方法返回一个IBinder对象,应用程序通过它来跟Service组件通信

void onCreate():当gai Service第一次被创建后将立即回调该方法

void onDestroy() :当该Service被关闭之前将会回调该方法

void onStartCommand(Intent intent,int flags,int startId):该方法早期的版本是void onStart(Intent intent,int startId),

          每次客户端调用StartService(Intent)方法启动该Service时都会回调该方法

boolean onUnbind(Intent intent):当该service上绑定的所有客户端都断开连接时将会回调该方法

AndroidManifest.xml中中配置:

<service android:name = ".serviceName">

  <intent-filter>

    <!--为该service组件的intent-filter配置action-->

    <action android:name="org.crazyit.service.SERVICE_NAME"/>

  </intent-filter>

</service>

service启动方式:

1.)通过context的 startService()->访问者与service之间没有关联,即使访问者退出,service仍然正常运行

    ----对应结束service的方法为stopService();

2.)通过context的 bindService()->访问者与service绑定在一起,访问者一旦退出service也终止

    ----对应结束service的方法为unbindService

 

 

posted @ 2013-02-20 13:49  nibl  阅读(200)  评论(0编辑  收藏  举报