197 lines
4.6 KiB
Markdown
197 lines
4.6 KiB
Markdown
|
# 第九章:服务
|
|||
|
|
|||
|
## 9.1 服务概述
|
|||
|
|
|||
|
Android的四大组件:Activity ContentProvider BroadcastReceiver Service
|
|||
|
|
|||
|
Service 服务:长时间在后台运行,而且没有用户界面
|
|||
|
|
|||
|
## 9.2 服务的创建
|
|||
|
|
|||
|
包上右键单击:new -->Service-->Service
|
|||
|
|
|||
|
清单文件中产生一段代码,用来注册服务的
|
|||
|
|
|||
|
```xml
|
|||
|
<service
|
|||
|
android:name=".MyService"
|
|||
|
android:enabled="true"
|
|||
|
android:exported="true"></service>
|
|||
|
```
|
|||
|
|
|||
|
## 9.3 服务的生命周期方法
|
|||
|
|
|||
|
启动服务的方式有两种: startService() bindService()
|
|||
|
|
|||
|
关闭服务的方法有两种: stopService() unbindService()
|
|||
|
|
|||
|
服务的生命周期方法: onCreate() onCreate()
|
|||
|
onStartCommand() onBind() onUnbind()
|
|||
|
onDestroy() onDestroy()
|
|||
|
|
|||
|
## 9.4 服务的启动方式
|
|||
|
|
|||
|
### 案例一:
|
|||
|
|
|||
|
演示startService() stopService() 开启和关闭服务
|
|||
|
|
|||
|
(1)创建项目StartService
|
|||
|
(2)复制图片和布局文件
|
|||
|
(3)创建服务,重写服务的三个生命周期方法
|
|||
|
(4)MainActivity作为启动服务的组件
|
|||
|
|
|||
|
收获:
|
|||
|
|
|||
|
1.startService()开启的服务,其生命周期方法有三个
|
|||
|
|
|||
|
2.生命周期方法何时被调用
|
|||
|
|
|||
|
### 案例二:
|
|||
|
|
|||
|
演示通过bindService方法启动服务
|
|||
|
|
|||
|
(1)创建项目BindService
|
|||
|
(2)复制图片和布局文件
|
|||
|
(3)创建服务,重写四个生命周期方法
|
|||
|
(4)MainActivity作为启动服务的组件
|
|||
|
|
|||
|
收获
|
|||
|
1. 通过bindService方法启动服务时,会回调服务中的onCreate onBind 两个生命周期方法
|
|||
|
2. 通过unbindService方法关闭服务时,会回调服务中的onUnbind onDestroy两个生命周期方法
|
|||
|
|
|||
|
总结:
|
|||
|
|
|||
|
启动服务的两种方式:startServive() bindService()
|
|||
|
|
|||
|
两种方式的不同:bindService()方式启动的服务,组件和服务之间可以进行通信
|
|||
|
|
|||
|
> 如何通信?
|
|||
|
> 启动服务的组件:MainActivity
|
|||
|
> 服务:MyService
|
|||
|
|
|||
|
```java
|
|||
|
@Override
|
|||
|
public IBinder onBind(Intent intent) {
|
|||
|
System.out.println("onBind");
|
|||
|
return new MyBinder();
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
> onBind方法返回值是IBinder接口对象,通过该接口对象,可以实现组件和服务之间的通信。
|
|||
|
>
|
|||
|
> onBind方法的返回值返回到哪里去了?返回给了onServiceConnected方法的IBinder iBinder形参
|
|||
|
|
|||
|
```java
|
|||
|
class MyConn implements ServiceConnection{
|
|||
|
@Override
|
|||
|
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
|||
|
}
|
|||
|
@Override
|
|||
|
public void onServiceDisconnected(ComponentName componentName) {
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
总结:
|
|||
|
1.留下1Bug:当解绑服务之后,不能再调用服务中的方法了????
|
|||
|
|
|||
|
2.服务Service
|
|||
|
|
|||
|
(1)创建Service
|
|||
|
(2)启动服务的两种方式:startService bindService
|
|||
|
(3)启动服务的两种方式之间的不同:(重点)
|
|||
|
生命周期方法不同
|
|||
|
bindService启动的服务,启动服务的组件和服务之间可以通信。
|
|||
|
如何通信?
|
|||
|
|
|||
|
## 9.5服务的通信
|
|||
|
|
|||
|
### 案例 MusicPlayer
|
|||
|
|
|||
|
1.创建项目
|
|||
|
|
|||
|
2.res-new-Directory-raw,music,mp3复制
|
|||
|
|
|||
|
3.复制图片和布局文件
|
|||
|
|
|||
|
4.创建按钮的背景选择器 drawable --> new --> Drawable Resource file --> btn_bg_selector
|
|||
|
|
|||
|
启动服务的组件:MainActivity 服务MusicService:播放、暂停、继续播放、拉动进度条
|
|||
|
|
|||
|
5.创建服务MusicService
|
|||
|
|
|||
|
6.MainActivity绑定服务:bindService()
|
|||
|
|
|||
|
### 案例问答:
|
|||
|
|
|||
|
1.MainActivity和MusicSerivce之间如何进行通信?(MainActivity如何调用MusicSerivce的方法?)
|
|||
|
|
|||
|
答:MusicService.MusicControl control:
|
|||
|
|
|||
|
2.MusicService.MusicControl control如何完成初始化?
|
|||
|
|
|||
|
答:
|
|||
|
|
|||
|
```java
|
|||
|
class Myconn implements ServiceConnection {
|
|||
|
@Override
|
|||
|
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
|||
|
control = (MusicService.MusicControl) iBinder;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onServiceDisconnected(ComponentName componentName) {
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
3.
|
|||
|
|
|||
|
```java
|
|||
|
@Override
|
|||
|
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
|||
|
control = (MusicService.MusicControl) iBinder;
|
|||
|
}中IBinderiBinder对应的实参是什么?
|
|||
|
```
|
|||
|
|
|||
|
答:
|
|||
|
|
|||
|
```java
|
|||
|
@Override
|
|||
|
public IBinder onBind(Intent intent) {
|
|||
|
return new MusicControl();
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
4.服务中的音乐总时长和当前进度,是如何传递给MainActivity?
|
|||
|
|
|||
|
答:Hanler对象
|
|||
|
|
|||
|
5.进度条跑起来的代码在哪里?
|
|||
|
|
|||
|
```java
|
|||
|
seekBar,setMax(duration);
|
|||
|
seekBar.setProgress(currentPosition);
|
|||
|
```
|
|||
|
|
|||
|
6.完成进度条拖动的代码在哪里?
|
|||
|
|
|||
|
答:在进度条的单击事件中
|
|||
|
|
|||
|
```java
|
|||
|
int progress:=seekBar,getProgress();
|
|||
|
control.seekTo(progress);
|
|||
|
```
|
|||
|
|
|||
|
7.如何设置当音乐播放完之后,动画也停止?
|
|||
|
|
|||
|
答:在进度条的单击事件中
|
|||
|
|
|||
|
```java
|
|||
|
if (i == seekBar.getMax()) {
|
|||
|
animator.pause();
|
|||
|
}
|
|||
|
```
|
|||
|
|