http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html

BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()->onBind()->onUnbind()->onDestroy()】

调用者Activity

MainActivity:

package com.test.bindservicedemo;import com.test.bindservicedemo.BindService.MyBinder;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.support.v4.app.NavUtils;public class MainActivity extends Activity{protected static final String TAG = "Service";private Button btn_StartBindService;private Button btn_StopBindService;private BindService bindService;// private boolean flag;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_StartBindService = (Button) findViewById(R.id.btn_StartBindService);btn_StopBindService = (Button) findViewById(R.id.btn_StopBindService);btn_StartBindService.setOnClickListener(listener);btn_StopBindService.setOnClickListener(listener);}private View.OnClickListener listener = new View.OnClickListener(){@Overridepublic void onClick(View v){// TODO Auto-generated method stubswitch (v.getId()){case R.id.btn_StartBindService:bind();break;case R.id.btn_StopBindService:unbind();default:break;}}};private void bind(){Intent intent = new Intent();intent.setClass(MainActivity.this, BindService.class);bindService(intent, conn, Context.BIND_AUTO_CREATE);}private ServiceConnection conn = new ServiceConnection(){@Overridepublic void onServiceDisconnected(ComponentName name){// TODO Auto-generated method stubLog.i(TAG, "Service-->onServiceDisconnected()!");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service){// TODO Auto-generated method stubbindService=((BindService.MyBinder)service).getService();bindService.MyMethod();Log.i(TAG, "Service-->onServiceConnected()!");}};private void unbind(){Log.i(TAG, "Service-->unbind()!");unbindService(conn);Log.i(TAG, "Service-->unbindService()!");}@Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}

服务BindService

BindService:

由于Android 中的Service使用了onBind 的方法去绑定服务,返回一个Ibinder对象进行操作,而我们要获取具体的Service方法的内容的时候,我们需要Ibinder对象返回具体的Service对象才能操作,所以说具体的Service对象必须首先实现Binder对象,这个样子的话我们才能利用bindService的方法对Service进行绑定,获取Binder对象之后获取具体的Service对象,然后才获取Service中的方法等等。所以我们需要注意的是bindService的方式去绑定服务获取的必定是实现了Binder的对象,所以这是我们必须使用Binder的方式去获取Service的方式而不是直接使用Service的类,这个是Android内部实现所约束的。

方法过程如下:

package com.test.bindservicedemo;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class BindService extends Service{protected static final String TAG = "Service";public class MyBinder extends Binder{public BindService getService(){return BindService.this;}};private final IBinder binder=new MyBinder(); @Overridepublic IBinder onBind(Intent intent)//在Service被绑定到其他程序上时被调用{// TODO Auto-generated method stubLog.i(TAG, "Service--onBind()");return binder;//这个返回的IBinder对象,和onServiceConnected的IBinder参数,是同一个东西。应用和Service之间就依靠这个IBinder通信}public void MyMethod(){ Log.i(TAG, "Service-->MyMethod()");}}
  1. Intent intent = new Intent(MainActivity.this,BindService.class)-->
  2. 新建了BindService对象 -->
  3. 新建了MyBinder对象-->
  4. bindService(intent, conn, Context.BIND_AUTO_CREATE);-->
  5. onBind()函数-->
  6. 传递MyBinder对象-->
  7. onServiceConnected()-->
  8. 通过传递的Binder对象获取刚刚和Binder对象对应的BindService 对象-->
  9. 调用Service中定义的方法.

这个其中必须通过Binder对象,因为是通过Binder对象来传递的,通过Binder对象获取Service对象,然后获取所需的服务,所以Service必须实现Binder,以便传递和使用。

更多相关文章

  1. MTK android配置LCD背光和LED,调试方法
  2. Android(安卓)kernel x86 编译方法
  3. android 扫描文件栈溢出问题,自己写的扫描文件方法
  4. 【Android习惯】文件、方法、变量命名规范参考(编辑中)
  5. java 自定义注解(翻译)
  6. Android(安卓)Runnable运行在哪个线程
  7. Android(安卓)实现中文按拼音排序方法
  8. Android屏幕截图实现
  9. Handler内存泄露原理及解决方法

随机推荐

  1. Design Widget
  2. 极光推送的使用
  3. Android Studio 百度地图开发
  4. Android(安卓)2.0 源码发布,已经成功移植
  5. Android GCM使用
  6. WIFI移植手记
  7. android Socket 长连接出错:android.syste
  8. Android轻松实现语音功能
  9. TabWidget/TabHost的两种使用方法
  10. Android(安卓)OTA本地自动升级实现