Android的Service的初接触

刚刚看了一点与Service相关的书籍,做做笔记,希望也能帮到有需要的人。

 

首先,开发Service和Activity差不多,也是需要两个步骤:

一、定义一个继承Service的子类

二、在AndroidManifest.xml文件中配置该Service

 

Service的生命周期方法如下:

1、abstract IBinder onBind(Intent intent):该方法是Service子类必须实现的方法。该方法返回一个IBinder对象,应用程序可以通过该对象与Service组件通讯

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

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

4、int onStartCommand(Intent intent, int flags, int startId):客服端调用startService(Intent)方法启动Service时都会回调该方法

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

 

Android中启动Service的两种方法:

1、通过Context的startService()方法:通过该方法启动的Service,访问者与Service之间没有关联,即使访问者退出了,Service也是在继续运行。

2、通过Context的bindService()方法:使用该方法启动的Service,访问者与Service绑定在一起,当访问者退出了之后,Service也退出停止运行。

 

posted @ 2014-07-31 00:09  zds_song  阅读(147)  评论(0编辑  收藏  举报