




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】bindservice使用例子
MainActivity.java如下packagecom.example.bindservice;importcom.example.bindservice.BindService.MyBinder;importandroid.os.Bundle;importandroid.os.IBinder;importandroid.app.Activity;importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.content.ServiceConnection;importandroid.util.Log;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassMainActivityextendsActivity{ privateButtonstartBtn; privateButtonstopBtn; privatebooleanflag; privatestaticfinalStringTAG="BindService"; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); flag=false; startBtn=(Button)this.findViewById(R.id.startBtn); stopBtn=(Button)this.findViewById(R.id.stopBtn); startBtn.setOnClickListener(listener); stopBtn.setOnClickListener(listener); } privateOnClickListenerlistener=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
switch(v.getId()){
caseR.id.startBtn:
bindService();
break;
caseR.id.stopBtn:
unBind();
break;
default:
break;
}
}
};
privatevoidbindService(){
Intentintent=newIntent(MainActivity.this,BindService.class);
bindService(intent,conn,Context.BIND_AUTO_CREATE);
}
privatevoidunBind(){
if(flag==true){
Log.i(TAG,"BindService-->unBind()");
unbindService(conn);
flag=false;
}
}
privateServiceConnectionconn=newServiceConnection(){
@Override
publicvoidonServiceDisconnected(ComponentNamename){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonServiceConnected(ComponentNamename,IBinderservice){
//TODOAuto-generatedmethodstub
MyBinderbinder=(MyBinder)service;
BindServicebindService=binder.getService();
bindService.MyMethod();
flag=true;
}
};
@Override publicbooleanonCreateOptionsMenu(Menumenu){ //Inflatethemenu;thisaddsitemstotheactionbarifitispresent. getMenuInflater().inflate(R.menu.main,menu); returntrue; } }BindService.java如下packagecom.example.bindservice;importandroid.app.Service;importandroid.content.Intent;importandroid.os.Binder;importandroid.os.IBinder;importandroid.util.Log;publicclassBindServiceextendsService{ privatestaticfinalStringTAG="BindService"; publicvoidMyMethod(){ for(inti=0;i<100;i++) { Log.i(TAG,"BindService-->MyMethod()"); }
} @Override publicIBinderonBind(Intentarg0){ //TODOAuto-generatedmethodstub returnmyBinder; } publicclassMyBinderextendsBinder{
publicBindServicegetService(){
returnBindService.this;
}
} privateMyBindermyBinder=newMyBinder();}strings.xml如下<?xmlversion="1.0"encoding="utf-8"?><resources>
<stringname="app_name">BindService</string>
<stringname="action_settings">Settings</string>
<stringname="hello_world">Helloworld!</string> <stringname="stopBtn">stopBtn</string> <stringname="startBtn">startBtn</string></resources>main.xml如下<RelativeLayoutxmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="18dp"
android:text="@string/startBtn"/>
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/startBtn"
android:layout_below="@+id/startBtn"
android:layout_marginTop="29dp"
android:text="@string/stopBtn"/></RelativeLayout>AndroidManifest.xml如下<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="/apk/res/android"
package="com.example.bindservice"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.bindservice.MainActivity"
android:label="@string/app_name">
<intent-fi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《创新题改编选例》课件
- 幼儿园科学课件:火箭上天【模板课件】
- 线路规划问题讲解
- 本溪市燃气公司社会责任
- 煤质与环境保护
- 城市绿地公园土地租赁合同
- 集团化成本控制与优化
- 医疗器械公司职工餐厅租赁意向书
- 乳制品行业合同风险防范
- 市场调研与分析方法的通知
- 市场营销(第2版) 课件 王永贵 第1、2章-市场与市场营销概述及发展、营销环境与市场感知
- 国有集团公司中层及员工履职追责问责处理办法模版
- 低温雨雪冰冻灾害应急救援准备
- 工程项目成本管理实论项目成本管控的方圆之道
- 人教版九年级化学第一至四单元测试卷及答案
- 草地调查规划学智慧树知到课后章节答案2023年下东北农业大学
- 2023北京第二次高中学业水平合格性考试信息技术试卷试题(答案详解)
- 《创新创业基础》教学课件合集
- 第三章油层对比新
- 2024届安徽省合肥中学科大附中数学九上期末复习检测试题含解析
- 低温低温处理对隔膜性能的影响
评论
0/150
提交评论