




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
绑定本地Service并与之通信2<!--制作可以显示不同字体、不同颜色的文本、URL的界面-->任务陈述3<!--点击“绑定Service”按钮,会先后弹出如下提示框-->任务陈述4<!--点击“获取Service状态”按钮,会弹出如下提示框-->任务陈述5<!--点击“解除绑定”按钮,会弹出如下提示框-->任务陈述6<!--新建一个Android工程,命名为BindService--><!--创建一个可实现返回数据的服务,在res下创建服务的源代码类BindService-->public
class
BindService
extends
Service<!--定义onBinder方法所返回的对象-->private
MyBinder
binder
=
new
MyBinder();<!--通过继承Binder来实现IBinder类-->public
class
MyBinder
extends
Binder
{
public
StringgetData()
{
return
"DatacomefromService";
}
}任务实施7<!--必须实现的方法,返回IBinder对象-->public
IBinderonBind(Intentintent)
{
System.out.println("ServiceisBinded");
//
返回IBinder对象
return
binder;
}<!--Service被创建时回调该方法-->public
void
onCreate()
{
super.onCreate();
Toast.makeText(BindService.this,
"ServiceisCreated",Toast.LENGTH_LONG).show();
}任务实施8<!--Service被断开连接时回调该方法-->public
boolean
onUnbind(Intentintent)
{
Toast.makeText(BindService.this,
"ServiceisUnbinded",Toast.LENGTH_LONG).show();
return
true;
}<!--Service被关闭之前回调-->
public
void
onDestroy()
{
super.onDestroy();
Toast.makeText(BindService.this,
"ServiceisDestroyed",Toast.LENGTH_LONG).show();
}任务实施9<!--在AndroidManifest.xml中对服务进行注册--><service
android:name=".BindService">
<intent-filter>
<!--
为该Service组件的intent-filter配置action-->
<action
android:name="org.crazyit.service.BIND_SERVICE"
/>
</intent-filter></service><!--在layout下添加布局文件main.xml,在onCreate方法中修改如下代码-->
setContentView(R.layout.main);任务实施10<!--在Activity的源代码中实现绑定Service,并获取Service传递的数据-->public
class
MainActivity
extends
Activity{
Button
bind,
unbind,
getServiceStatus;<!--所启动的Service的IBinder对象-->
BindService.MyBinder
binder;<!--定义一个ServiceConnection对象-->private
ServiceConnection
conn
=
new
ServiceConnection(){<!--当该Activity与Service连接成功时回调该方法-->public
void
onServiceConnected(ComponentNamename,IBinderservice){
System.out.println("--ServiceConnected--");任务实施11<!--获取Service的onBind方法所返回的MyBinder对象-->binder
=(BindService.MyBinder)service;
}<!--当该Activity与Service断开连接时回调该方法-->
public
void
onServiceDisconnected(ComponentNamename){
System.out.println("--ServiceDisconnected--");
}
};<!--获取程序界面中的start、stop、getServiceStatus按钮-->bind
=(Button)findViewById(R.id.bind);
unbind
=(Button)findViewById(R.id.unbind);
getServiceStatus
=(Button)findViewById(R.id.getServiceStatus);任务实施12<!--创建启动Service的Intent-->final
Intentintent=
new
Intent();<!--为Intent设置Action属性-->
intent.setAction("org.crazyit.service.BIND_SERVICE");
bind.setOnClickListener(new
OnClickListener(){
public
void
onClick(Viewsource){<!--绑定指定Serivce-->
bindService(intent,
conn,Service.BIND_AUTO_CREATE);任务实施13<!--解除绑定Serivce-->unbindService(conn);<!--获取、并显示Service的count值-->getServiceStatus.setOnClickListener(new
OnClickListener(){
public
void
onClick(Viewsource){
//
获取、并显
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 肝病相关课件题目
- 各地市中考数学试卷
- 葛军出的安徽省数学试卷
- 肝炎中医课件
- 德强中考数学试卷
- 二模江西数学试卷
- 肛裂中医课件下载
- 德艺期中数学试卷
- 丰台区2024数学试卷
- 2025年04月重庆医科大学附属第二医院整形与颌面外科科室秘书编外岗位招聘1人笔试历年专业考点(难、易错点)附带答案详解
- 成都某污水处理厂施工组织设计
- 广告制作交货进度计划及保障措施
- 2025年中职基础会计试题
- 三年级数学五千以内加减混合两步运算题竞赛测试口算题
- 2025至2030中国生物反馈仪行业产业运行态势及投资规划深度研究报告
- 【公开课】牛顿第二定律+课件+-2024-2025学年高一上学期物理人教版(2019)必修第一册+
- 预防错混料培训
- 2024年江苏省响水县卫生局公开招聘试题带答案
- 2025年云南省中考地理试卷真题(含答案)
- 粤港澳大湾区青少年国情教育实践基地(虎门渡口西岸物业提升改造项目)可行性研究报告
- 人教版三年级数学下学期期末复习试卷含答案10套
评论
0/150
提交评论