Android BindService

   一般情况下我们使用startService(Intent service)来启动一个服务,但这种情况下无法得到Service对象的引用,通过bindService方法启动服务则可以实现此功能。下面给一个小例子演示一下:

 

1.调用者

 

Java代码   收藏代码
  1. package com.zhf.local;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.content.ComponentName;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.ServiceConnection;  
  9. import android.os.Bundle;  
  10. import android.os.IBinder;  
  11.   
  12. /** 
  13.  * 此例的目的就是拿到MyService的引用,从而可以引用其内部的方法和变量 
  14.  * @author Administrator 
  15.  * 
  16.  */  
  17. public class LocalServiceActivity extends Activity {  
  18.     /** Called when the activity is first created. */  
  19.     private MyService myService;  
  20.   
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.   
  26.         Intent intent = new Intent(this, MyService.class);  
  27.         bindService(intent, connection, Context.BIND_AUTO_CREATE);  
  28.     }  
  29.   
  30.     private ServiceConnection connection = new ServiceConnection() {  
  31.   
  32.         @Override  
  33.         public void onServiceDisconnected(ComponentName name) {  
  34.             myService = null;  
  35.         }  
  36.   
  37.         @Override  
  38.         public void onServiceConnected(ComponentName name, IBinder service) {  
  39.             myService = ((MyService.MyBinder) service).getService();  
  40.             System.out.println("Service连接成功");  
  41.             //执行Service内部自己的方法  
  42.             myService.excute();  
  43.         }  
  44.     };  
  45.   
  46.     protected void onDestroy() {  
  47.         super.onDestroy();  
  48.         unbindService(connection);  
  49.     };  
  50. }  

 

 

2.服务者

 

Java代码   收藏代码
  1. package com.zhf.local;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.Binder;  
  6. import android.os.IBinder;  
  7.   
  8. public class MyService extends Service {  
  9.     private final IBinder binder=new MyBinder();  
  10.     @Override  
  11.     public IBinder onBind(Intent intent) {  
  12.         return binder;  
  13.     }  
  14.       
  15.     public class MyBinder extends Binder{  
  16.         MyService getService(){  
  17.             return MyService.this;  
  18.         }  
  19.     }  
  20.       
  21.     public void excute(){  
  22.         System.out.println("通过Binder得到Service的引用来调用Service内部的方法");  
  23.     }  
  24.   
  25.     @Override  
  26.     public void onDestroy() {  
  27.         //当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用  
  28.         super.onDestroy();  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean onUnbind(Intent intent) {  
  33.         //当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用  
  34.         System.out.println("调用者退出了");  
  35.         return super.onUnbind(intent);  
  36.     }  
  37. }  

猜你喜欢

转载自sfshine.iteye.com/blog/1728638