第5章后台服务与系统服务调用
第5章 后台服务与系统服务调用
5.1后台服务Service
5.1后台服务Service
>Android系统的Service是一种类似于Activity 的组件,但Service没有用户操作界面,也不能 自己启动,其主要作用是提供后台服务调用。 Service.不像Activity那样,当用户关闭应用界 面就停止运行,Service会一直在后台运行,除 非另有明确命令其停止。 >通常使用Service为应用程序提供一些只需在后 台运行的服务,或不需要界面的功能,例如, 从Internet下载文件、控制Video播放器等
➢ Android系统的Service是一种类似于Activity 的组件,但Service没有用户操作界面,也不能 自己启动,其主要作用是提供后台服务调用。 Service不像Activity那样,当用户关闭应用界 面就停止运行,Service会一直在后台运行,除 非另有明确命令其停止。 ➢ 通常使用Service为应用程序提供一些只需在后 台运行的服务,或不需要界面的功能,例如, 从Internet下载文件、控制Video播放器等
Service的生命周期中只有三个阶段: onCreate,onStartCommand,onDestroy. 方法 说明 onCreate() 创建后台服务。 onStartCommand (Intent intent, 启动一个后台服务。 int flags,int startId) onDestroy() 销毁后台服务,并删除所有调用。 sendBroadcast(Intentintent) 继承父类Context的sendBroadcast() 方法,实现发送广播机制的消息。 onBind(Intent intent) 与服务通信的信道进行绑定,服务程序 必须实现该方法。 onUnbind(Intentintent) 撤销与服务信道的绑定
方 法 说 明 onCreate() 创建后台服务。 onStartCommand (Intent intent, int flags, int startId) 启动一个后台服务。 onDestroy() 销毁后台服务,并删除所有调用。 sendBroadcast(Intent intent) 继承父类Context的sendBroadcast() 方法,实现发送广播机制的消息。 onBind(Intent intent) 与服务通信的信道进行绑定,服务程序 必须实现该方法。 onUnbind(Intent intent) 撤销与服务信道的绑定。 Service的生命周期中只有三个阶段: onCreate, onStartCommand, onDestroy
>通常Service?要在一个Activity中启动,调用 Activity的startService(Intent)方法启动 Service. >若要停止正在运行的Service,则调用 Activityl的stopService(Intent).方法关闭 Service。 >方法startService()和stopService()均继承 于Activity.及Service共同的父类 android.content.Context
➢ 通常Service要在一个Activity中启动,调用 Activity的startService(Intent)方法启动 Service。 ➢ 若要停止正在运行的Service,则调用 Activity的stopService(Intent)方法关闭 Service。 ➢ 方法startService()和stopService()均继承 于Activity及Service共同的父类 android.content.Context