




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 美术课题申报书项目类型
- 合建房屋合同范本
- 厂房清洁报价合同范本
- 课题立项申报计划书范文
- 合同权益转让合同范例
- 合同范本押金退回
- 课题申报书哪里有
- 品牌宣传服务合同范本
- 理疗课题申报书格式范文
- 哪里医学课题申报书
- 2024年江苏卫生健康职业学院单招职业适应性测试题库及答案解析0
- 《中国陶瓷史》课件-3-陶与瓷
- 数学教育的国际比较与交流
- 2023年4月自考00160审计学试题及答案含解析
- 案卷评查培训课件模板
- 2021年安徽省公务员录用考试《行测》真题及答案
- 光伏车棚设计方案
- 大学生春季心理健康教育
- 市级优质课一等奖《说和做》-七年级语文下册同步课件(统编版)
- 《合同能源管理介绍》课件
- 水准仪使用方法及原理课件
评论
0/150
提交评论