下面是绑定模式启动Service例程:

Android四大组件Service之bindService启动_第1张图片

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)并创建一个实例 binderpublic class MyBinder extends Binder{  // 创建一个Binder实例public BindServiceText getService()   // 返回当前服务对象{return BindServiceText.this;}}// 2.返回一个binder实例 , 传入到onServiceConnected 的IBinder参数中@Overridepublic IBinder onBind(Intent arg0) {  //传入到onServiceConnected 的IBinder参数中// TODO Auto-generated method stubreturn 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; @Overrideprotected 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.iboundStart:bindService();break;case R.id.iboundStop:unbindService();break;default:break;}}};private void bindService() {// TODO Auto-generated method stubIntent intent2 = new Intent(IboundService.this,BindServiceText.class);bindService(intent2, conn, Context.BIND_AUTO_CREATE);}private void unbindService() {// TODO Auto-generated method stubif(isConnected){unbindService(conn);}}private ServiceConnection conn= new ServiceConnection()  {// 建立绑定时启动,IBinder对象(binder)参数是在IBinder onBind();得到的对象,《------@Overridepublic void onServiceConnected(ComponentName arg0, IBinder binder) {// TODO Auto-generated method stubMyBinder mybinder=(MyBinder)binder;  // 获取的是binderService类中onBind返回的binder对象// BindServiceText Service = mybinder.getService();Service.MyMethod();isConnected=true;}@Overridepublic void onServiceDisconnected(ComponentName arg0) {// TODO Auto-generated method stubisConnected=false;}};}


Androidmanifest.xml

 




 

更多相关文章

  1. android中AudioRecord采集音频的参数说明
  2. android animation的两种应用实例
  3. Android Activity切换动画详解及实例
  4. android实现开机自动播放音乐实例(Broadcast、Service)
  5. Android开发:观察者模式应用实例
  6. Android Jetpack应指南学习笔记8——Navigation(二)使用safe arg
  7. IntentService通过HandlerThread单独开启一个线程来处理所有Inte

随机推荐

  1. Android开机自启动程序设置及控制方法
  2. Android随笔之——Android时间、日期相关
  3. Android工程师怒斥360拦截弹窗广告
  4. Android(安卓)动画实现弹幕效果
  5. 用Android模拟器体验angry birds
  6. Android 页面回调跳转(startActivityForRe
  7. 64位 ubuntu android SDK 无法新建AVD,adb
  8. Android(安卓)view的预加载占位效果
  9. 从零开始--系统深入学习android(实践-让我
  10. Android绘图之drawText绘制文本相关(4)