Android四大组件Service之bindService启动
2015-11-22 12:42
495 查看
下面是绑定模式启动Service例程:
BindService代码:
Activity代码:
Androidmanifest.xml
BindService代码:
package com.example.ibcondservice02; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class BindServiceText extends Service { //Service 处理函数 //1.创建一个binder实例 private MyBinder binder = new MyBinder(); // 创建一个binder类,类中能返回Service 对象(MyBinder)并创建一个实例 binder public class MyBinder extends Binder{ // 创建一个Binder实例 public BindServiceText getService() // 返回当前服务对象 { return BindServiceText.this; } } // 2.返回一个binder实例 , 传入到onServiceConnected 的IBinder参数中 @Override public IBinder onBind(Intent arg0) { //传入到onServiceConnected 的IBinder参数中 // TODO Auto-generated method stub return binder; // 返回binder对象 } public void MyMethod() // 创建一个公共的方法 { System.out.println("BinderService!!!"); } }
Activity代码:
package com.example.ibcondservice02; import com.example.ibcondservice02.BindServiceText.MyBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class IboundService extends Activity { private Button StartButton; private Button StopButton; private Boolean isConnected=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ibound_service); StartButton = (Button)findViewById(R.id.iboundStart); StopButton = (Button)findViewById(R.id.iboundStop); StartButton.setOnClickListener(listener); StopButton.setOnClickListener(listener); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.iboundStart: bindService(); break; case R.id.iboundStop: unbindService(); break; default: break; } } }; private void bindService() { // TODO Auto-generated method stub Intent intent2 = new Intent(IboundService.this,BindServiceText.class); bindService(intent2, conn, Context.BIND_AUTO_CREATE); } private void unbindService() { // TODO Auto-generated method stub if(isConnected) { unbindService(conn); } } private ServiceConnection conn= new ServiceConnection() { // 建立绑定时启动,IBinder对象(binder)参数是在IBinder onBind();得到的对象,《------ @Override public void onServiceConnected(ComponentName arg0, IBinder binder) { // TODO Auto-generated method stub MyBinder mybinder=(MyBinder)binder; // 获取的是binderService类中onBind返回的binder对象 // BindServiceText Service = mybinder.getService(); Service.MyMethod(); isConnected=true; } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub isConnected=false; } }; }
Androidmanifest.xml
<service android:name=".BindServiceText"></service>
相关文章推荐
- Android GifView 的用法
- android屏幕适配详解
- android 官网常用网址
- Android四大组件Service之StartService启动
- Android写文件到SDCard的一般过程和代码
- 【Android】图片操作工具类(ImageUtil.java)
- android四大组件之Service服务之总体概述
- 给android studio安装新字体,如mac系统的monaco字体
- android 4怎么打开usb调试?
- android两次点击返回键退出和按钮退出对话框
- 【Android】android:padding属性设置对ImageButton无效问题
- 正常编写的程序出现androidmainfest.xml missing错误怎么做
- Android GreenDao数据库框架使用
- android中实现view可以滑动的六种方法
- Android Activity生命周期以及Fragment生命周期的区别与分析
- Android setLayerType 硬件加速问题
- Android属性动画---Property Animation
- android109 结构体,联合体,枚举,自定义
- Android的一些总结
- Android按钮单击事件的四种常用写法总结