Activity和Service绑定

Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法

绑定代码如下

Activity类代码:

[java] view plaincopy
 
  1. <span style="font-size:16px;">package com.fox.Activity;  
  2.   
  3. import com.fox.Activity.service.Service1;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.ServiceConnection;  
  10. import android.os.Bundle;  
  11. import android.os.IBinder;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.widget.Button;  
  15. import android.widget.Toast;  
  16.   
  17. public class Activity1 extends Activity {  
  18.     private Button btn1 = null;  
  19.     private static String LOG="mp3";  
  20.   
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         btn1 = (Button) findViewById(R.id.button1);  
  26.         btn1.setOnClickListener(new btn1ClickListener());  
  27.         //开始绑定  
  28.         Intent intent = new Intent(Activity1.this,Service1.class);  
  29.         bindService(intent, conn, Context.BIND_AUTO_CREATE);  
  30.     }  
  31.       
  32.     private Service1 myservice = null;//绑定的service对象  
  33.     //连接对象,重写OnserviceDisconnected和OnserviceConnected方法  
  34.     public ServiceConnection conn= new ServiceConnection() {  
  35.           
  36.         @Override  
  37.         public void onServiceDisconnected(ComponentName name) {  
  38.             Log.i(LOG, "onServiceDisconnected>>>>>>>>");  
  39.             myservice = null;  
  40.         }  
  41.           
  42.         @Override  
  43.         public void onServiceConnected(ComponentName name, IBinder service) {  
  44.             Log.i(LOG, "onServiceConnected>>>>>>>>");  
  45.             myservice = ((Service1.MyBinder)service).getService();  
  46.             Log.i(LOG, myservice+">>>>>>>>");  
  47.         }  
  48.     };   
  49.   
  50.       
  51.     class btn1ClickListener implements View.OnClickListener {  
  52.   
  53.         @Override  
  54.         public void onClick(View v) {  
  55.             unbindService(conn);  
  56.         }  
  57.     }  
  58. }</span>  


Service类代码:

[java] view plaincopy
 
  1. <span style="font-size:16px;">package com.fox.Activity.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.Binder;  
  6. import android.os.IBinder;  
  7. import android.util.Log;  
  8.   
  9. public class Service1  extends Service{  
  10.     private final IBinder binder = new MyBinder();  
  11.     private static final String LOG="mp3";  
  12.       
  13.     @Override  
  14.     public IBinder onBind(Intent intent) {  
  15.         Log.i(LOG, "onBind............");  
  16.         return binder;  
  17.     }  
  18.     /** 
  19.      * 该类是获得Service对象 
  20.      * @author Administrator 
  21.      * 
  22.      */  
  23.     public class MyBinder extends Binder{  
  24.         public Service1 getService(){  
  25.             return Service1.this;  
  26.         }  
  27.     }  
  28.       
  29.     @Override  
  30.     public void onCreate() {  
  31.         Log.i(LOG, "oncreate............");  
  32.         super.onCreate();  
  33.     }  
  34.     @Override  
  35.     public void onStart(Intent intent, int startId) {  
  36.         Log.i(LOG, "onstart............");  
  37.         super.onStart(intent, startId);  
  38.     }  
  39.     @Override  
  40.     public int onStartCommand(Intent intent, int flags, int startId) {  
  41.         Log.i(LOG, "onstartcommand............");  
  42.         return super.onStartCommand(intent, flags, startId);  
  43.     }  
  44.     @Override  
  45.     public void onDestroy() {  
  46.         Log.i(LOG, "ondestory............");  
  47.         super.onDestroy();  
  48.     }  
  49.   
  50. }  
  51.   
  52. </span>  

 

开始绑定调用方法A.bindService()--->S.onCreate--->S.onBind---->>A.onServiceConnected绑定成功,并获得Service对象

结束绑定按钮的监听事件-->>unbindService(conn)关闭连接对象-->>S.destory()销毁该service 

注:结束绑定时是不会调用onServiceDisconnected()方法的;

http://blog.csdn.net/huqingwei0824/article/details/6869622

posted on 2015-11-17 16:12  %幻#影%  阅读(362)  评论(0编辑  收藏  举报

导航