付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 危重病人神经系统疾病护理
- 中职内科护理:护理工作中的伦理决策
- 2020过敏反应急诊管理临床实践指南
- 2018+专家共识:高尿酸血症和高心血管风险患者的诊断和治疗
- 2026医院心肌梗死患者健康教育
- 初中八年级地理“大国动脉·智慧畅流”复习导学案
- 本科口腔医学专业《牙周病学》课程思政融合教学设计
- 校园安全防护与应急处理指南
- 卒中溶栓治疗的护理效果评价
- 初中《道德与法治》八年级上册亦师亦友深度学习教案
- 2025年衡阳市南岳区事业单位人员招聘考试试题及答案详解
- 江西师范大学科学技术学院2026年人事招聘(第二批)笔试模拟试题及答案详解
- 2026西藏交通发展集团有限公司校园招聘备考题库及一套完整答案详解
- 2026中国工商银行新疆分行星令营暑期实习笔试参考题库及答案详解
- 贵州省粮食储备集团有限公司笔试试题
- 事故水池操作规程
- 2026教科版小学三年级科学下册期末复习自测卷及答案(3套)
- 2026广东广州市越秀区建设街招聘辅助人员1人备考题库含答案详解(精练)
- 锅炉热力设计计算表
- 2025-2026学年第二学期学校“教研组长工作述职报告”:履职尽责推动教研发展
- 卫生院医疗安全奖罚制度
评论
0/150
提交评论