24 绑定服务

服务的生命周期
●与其他组件不同的是,Service不能自己主动运行,需要调用相应的方法来启动。
●启动服务的方法是Context.startService()和Context.bindService()。 使用不同的方法启动服务,服务的生命周期也会不同。

●1、 startService方 式开启服务的生命周期
服务会执行onCreate() --> onStartCommand()方法,服务处于运行状态,
直到自身调用stopSelf()方法或者其他组件调用stopService()方法时服务停止,
最终被系统销毁。
服务会长期的在后台运行,并且服务的状态与开启者的状态没有关系。
●2、 bindService方 式开启服务的生命周期
服务会执行onCreate() > onBind()方法,服务处于绑定状态,客户端
通过unbindService()方法关闭连接,解除绑定时,系统将直接销毁服务。
服务与开启者的状态有关,当调用者销毁了,服务也会被销毁。

bind方式启动服务中
●当程序使用startService()和stopService()启动、 关闭服务时,服务与调用者之间基本不存在太多的关联,也无法与访问者进行通信、数据交互等。如果服务需要与调用者进行方法调用和数据交互时,应该使用bindService()和unbindService()启动、关闭服务。
●bindService(Intent service,ServiceConnection conn, int flags)
●Intent对象用于指定要启动的Service
●ServiceConnection对 象用于监听调用者与Service的连接状态。
●flags指定绑定时是否自动创建Service(如果Service还未创建)。

创建服务
2.1普通模式:自定义类继承Service; AndroidManifest.xml中注册服务

intent=new Intent (MainActivity. this,MyService.class);
startService(intent);
Log.i("service=" ,"服务启动成功");

2.2绑定模式:
构建IBinder对象; 自定义IBinder类继承Binder类;添加自定义方法。

class MyIBinder extends Binder {
//方法:与服务端的数据局传送。
pub1ic MyService test() {
System. out. println("音乐开始播放");
return MyService. this;
}
}

onBind方法返回自定义IBinder对象。

服务端:绑定服务

//1.构建Intent对象
intent=new Intent (MainActivity. this ,MyService.class);
//调用绑定方法
onCreate-> onBinder->问调是ServiceConncion接口里的onServiceConnected
//MyService s=new Myservice() ;
bindservice (intent ,conn,Service.BIND AUTO_ CREATE) ;

创建自定义连接类,实现ServiceConnection接口 重写接口方法。

class MyServiceConnection implements ServiceConnection {
//服务端与服务连接成功系统公调用该方法: IBinder service传递过来。
@override
pub1ic void onServi ceConnected (ComponentName name, IBinder service) {
System. out. print In("onServiceConnected....");
MyService .MyIBinder binder= (MyService .MyIBinder) service;
binder .test();
}
//意外中断连接调用该方法:解绑不公调用该方法。
@override
pub1ic void onServiceDisconnected (ComponentName name) {
System. out . printIn("onServiceDisconnected....");
}
}

远程服务
注册文件配置: android:process=":remote"
和主线程分离;
服务销毁
如果启动服务: startService),bindService(intent,conn, Service.BIND AUTO_ CREATE);同时都使用了,只单击某一个stopService,unbindService停止都不会调用onDestroy0;必须这个停方法都点击一次。

本地服务通信和远程服务通信

本地服务通信
在进行本地服务通信时,可以使用Service类提供的IBinder onBind(Intent intent)方法,该方法返回的IBinder对象会作为参数传递给ServiceConnection类中onServiceConnected(ComponentName name,IBinder service)方法,这样访问者就可以通过IBinder对象与Service进行通信。

远程服务通信
远程服务通信是通过AIDL(Android Interface Definition Language)实现的,它是一种接口定义语言(Interface Definition Language),其语法格式非常简单,与Java中定义接口很相似。

package cn.itcast.service;
interface IService{
String getName();
int getPrice();
}

定义好AIDL接口之后,接着需要在应用程序中创建Service的子类。该Service的onBind()方法所返回的IBinder对 象应该是ADT所生成的IService.Stub的子类。

创建前台服务

1.构建消息管理器

2.构建消息通道

3.创建消息通道

4.构建消息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值