Note/安卓/第九章:服务.md

197 lines
4.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 第九章:服务
## 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创建服务重写服务的三个生命周期方法
4MainActivity作为启动服务的组件
收获:
1.startService()开启的服务,其生命周期方法有三个
2.生命周期方法何时被调用
### 案例二:
演示通过bindService方法启动服务
1创建项目BindService
2复制图片和布局文件
3创建服务重写四个生命周期方法
4MainActivity作为启动服务的组件
收获
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-rawmusic,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();
}
```