版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】10天学通Android开发(2-3)-核心组件Service绑定
通过startService开启的服务,当访问者关闭时,服务仍然存在;访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定;
如果使用Context.bindService()方法启动服务,则在服务未创建时,系统会调用服务的onCreate()方法,接着调用onBind()方法,这时就访问者与服务已经绑定了,主程序销毁时服务也会终止。1)绑定服务时,会自动创建服务。2)如果创建后并启动后再绑定,不会重新创建,一个Service只有一个实例3)同时启动和绑定服务时,解除绑定服务,但不会销毁关闭服务的,必须解除绑定并停止服务。4)通过StartService启动服务,当前Activity销毁,服务不会停止,通过BindService启动服务,当前Activity销毁,服务停止。
绑定与解绑定服务(1)Context.bindService(Intentservice,ServiceConnectionconn,BIND_AUTO_CREATE);//绑定服务(2)Context.unbindService(ServiceConnectionconn);
ServiceConnectionServiceConnection为一个接口,用于绑定和解绑定IBinder,因此需要创建一个类实现它;classXxxServiceConnectionimplementsServiceConnection{@OverridepublicvoidonServiceConnected(ComponentNamename,IBinderservice){//service为在onBind返回的IBinder//绑定Binder对象}@OverridepublicvoidonServiceDisconnected(ComponentNamename){//解绑定Binder对象}}
Service类classXxxServiceextendsService{privateIBinderbinder=newXxxBinder();publicIBinder
onBind(Intentintent){returnbinder;}publicintfun(inta){//服务提供的方法,但是不能直接调用...}privateclassXxxBinderextendsBinderimplementsIXxxBinder{//面向接口编程publicreturnfun1(inta){//对外暴露的APIreturn
fun(a);//内部调用Service的方法}}}
案例:绑定服务主要功能:Service实现不断输出1、2、3……的服务功能,Activity调用Service的公开方法,调出当时的一个值。继续上次服务的案例,增加绑定等功能。打开activity_main.xml,添加两个命令按钮,绑定服务和解除绑定:<Button
android:id="@+id/btnBindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"/>
<Button
android:id="@+id/btnUnbindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除绑定"/>2)MainActivity.java,添加相应代码:定义两按钮:
privateButtonbtnBindService,btnUnbindService;
通过查找得到这两个按钮:btnBindService=(Button)findViewById(R.id.btnBindService);
btnUnbindService=(Button)findViewById(R.id.btnUnbindService);3)添加事件侦听器:btnBindService.setOnClickListener(this);btnUnbindService.setOnClickListener(this);4)在onClick中添加判断分支:caseR.id.btnBindService:
bindService(serviceIntent,this,Context.BIND_AUTO_CREATE);
break;
caseR.id.btnUnbindService:
unbindService(this);
break;需要当前类实现ServiceConnection,加进继承ServiceConnection:publicclassMainActivityextendsActionBarActivityimplementsOnClickListener,ServiceConnection{同时产生了两接口方法,成功绑定的方法:@Override
publicvoidonServiceConnected(ComponentNamearg0,IBinderbind){
//TODOAuto-generCatedmethodstub
System.out.println("onServiceConnected");
}
解除绑定或Service崩溃时
@Override
publicvoidonServiceDisconnected(ComponentNamename){
//TODOAuto-generatedmethodstub
System.out.println("onServiceDisconnected");
}5)onBind要指定返回值,否则绑定时,其实没有真正绑定,onServiceConnected不会执行定义内部类MyServiceBinder扩展自Binder:publicclassMyServiceBinderextendsBinder{
publicMyServicegetService()
{
returnMyService.this;//取得服务的实例
}
}定义myservicebinder,并返回:privatefinalMyServiceBindermyservicebinder=newMyServiceBinder();publicIBinderonBind(Intentarg0){
//TODOAuto-generatedmethodstub
System.out.println("onBind");
returnmyservicebinder;
}6)服务内添加一输出:privateinti=0;
publicvoidstartTimer(){
if(timer==null){
timer=newTimer();
task=newTimerTask(){
@Override
publicvoidrun(){
i++;
System.out.println(i);
}
};
timer.schedule(task,1000,1000);
}
}
publicvoidstopTimer(){
if(timer!=null)
{
task.cancel();
timer.cancel();
task=null;
timer=null;
}
}
privateTimertimer=null;
privateTimerTasktask=null;
其中,每一秒钟执行:timer.schedule(task,1000,1000);7)onCreate、onDestroy添加startTimer、stopTimer:publicvoidonCreate(){
System.out.println("创建好了");
startTimer();
super.onCreate();
}
@Override
publicvoidonDestroy(){
System.out.println("被销毁了");
stopTimer();
super.onDestroy();
}8)取得服务实例:publicclassMyServiceBinderextendsBinder{
publicMyServicegetService()
{
returnMyService.this;//取得服务的实例
}
}9)公开一个方法,取得服务内部的数字(状态):publicintgetCurrentNum()
{
returni;
}10)回到主Activity,取得服务定义变量:privateMyServicemyService=null;取得实例:publicvoidonServiceConnected(ComponentNamearg0,IBinderbind){
//TODOAuto-generCatedmethodstub
System.out.println("onServiceConnected");
myService=((MyService.MyServiceBinder)bind).getService();
}11)添加按钮<Button
android:id="@+id/btnGetCurrentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GetCurrentNum"/>定义按钮:privateButtonbtnGetCurrentNumber;取得:btnGetCurrentNumber=(Button)findViewById(R.id.btnGetCurrentNumber);事件:
btnGetCurrentNumber.setOnClickListener(this);实现:caseR.id.btnGetCurrentNumber:
if(my
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 44033-2024铁矿尾矿利用率计算方法
- 五年级下册小学数学单元教学计划
- 《开关设备数字化车间运行管理模型指南gbt+41996-2022》详细解读
- 三年级上语文教材解读掌声-苏教版
- 《电磁兼容 试验和测量技术 供电系统及所连设备谐波、间谐波的测量和测量仪器导则GBT 17626.7-2017》详细解读
- 2022年度辽宁省安全员之C2证(土建安全员)考前练习题及答案
- 内部产品转SaaS能成功吗
- 2022年度辽宁省安全员之B证(项目负责人)基础试题库和答案要点
- 奥迪销售流程(提案成交交车现实客户跟踪潜在客户跟踪)NXPower
- 上市企业过度投资问题研究以比亚迪股份有限公司为例
- 新《高等教育管理学》期末考试复习题库(含答案)
- 青海省海西蒙古族藏族自治州格尔木市2023年三年级数学第二学期期末经典试题含解析
- 耶鲁布朗强迫症量表Y
- 小学科学大象版五年级下册全册教案(2023春)
- 篆书汉子对照表
- 木姜叶柯条形茶生产技术规程
- 房地产开发公司印章管理专项内部审计报告
- 发动机航线维护知到章节答案智慧树2023年中国民用航空飞行学院
- 中医内科学习题集
- 企业并购与资产重组智慧树知到答案章节测试2023年同济大学
- 土方平整施工组织设计
评论
0/150
提交评论