activity绑定服务

来源:互联网 发布:ubuntu veloview 编辑:程序博客网 时间:2024/05/22 11:02

我直接贴出代码 读者慢慢体会吧

接口

[java] view plain copy
  1. public interface ServiceInterface {  
  2.     public void show();  
  3. }  

Service类

[java] view plain copy
  1. import android.app.Service;  
  2. import android.content.Intent;  
  3. import android.os.Binder;  
  4. import android.os.IBinder;  
  5. import android.util.Log;  
  6. import android.widget.Toast;  
  7.   
  8. public class Service_01 extends Service {  
  9.   
  10.     private final String TAG = "Service--->";  
  11.   
  12.     private final IBinder binder = new MyBinder();  
  13.       
  14.     MyBinder mybinder = new MyBinder();  
  15.       
  16.     public class MyBinder extends Binder implements ServiceInterface {  
  17.         /*show只是测试方法,可以不要*/  
  18.         public void show() {  
  19.             Toast.makeText(Service_01.this"MyName is Service_01",  
  20.                     Toast.LENGTH_LONG).show();  
  21.         }  
  22.         /*返回service服务,方便activity中得到*/  
  23.         Service_01 getService() {  
  24.             return Service_01.this;  
  25.         }  
  26.     }  
  27. /////////////////以上是为了测试绑定功能而做的准备  
  28.       
  29. /*  绑定时执行*/  
  30.     @Override  
  31.     public IBinder onBind(Intent intent) {  
  32.         Log.d(TAG, "onBind");  
  33.         return binder;  
  34.     }  
  35.     /*只在创建时执行一次*/  
  36.     @Override  
  37.     public void onCreate() {  
  38.         super.onCreate();  
  39.         Log.d(TAG, "onCreate");  
  40.     }  
  41.     /*断开绑定或者stopService时执行*/  
  42.     @Override  
  43.     public void onDestroy() {  
  44.         super.onDestroy();  
  45.         Log.d(TAG, "onDestroy");  
  46.     }  
  47.   
  48.      /* 当内存不够时执行改方法 */  
  49.      @Override  
  50.      public void onLowMemory() {  
  51.      super.onLowMemory();  
  52.      onDestroy();// 注销该service  
  53.      }  
  54.   
  55.     /* 当从新尝试绑定时执行 */  
  56.     @Override  
  57.     public void onRebind(Intent intent) {  
  58.         super.onRebind(intent);  
  59.         Log.d(TAG, "onRebind");  
  60.     }  
  61.   
  62.     @Override  
  63.     public void onStart(Intent intent, int startId) {  
  64.         super.onStart(intent, startId);  
  65.         Log.d(TAG, "onStart");  
  66.     }  
  67.   
  68.     /* ,每次startService都会执行该方法,而改方法执行后会自动执行onStart()方法 */  
  69.     @Override  
  70.     public int onStartCommand(Intent intent, int flags, int startId) {  
  71.         Log.d(TAG, "onStartCommand-->flags=" + flags + "  startId=" + startId);  
  72.           
  73.         return super.onStartCommand(intent, flags, startId);  
  74.     }  
  75.     /*断开绑定时执行*/  
  76.     @Override  
  77.     public boolean onUnbind(Intent intent) {  
  78.         Log.d(TAG, "onUnbind");  
  79.         return super.onUnbind(intent);  
  80.     }  
  81. }  

Activity测试类

[java] view plain copy
  1. import android.app.Activity;  
  2. import android.content.ComponentName;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.content.ServiceConnection;  
  6. import android.os.Bundle;  
  7. import android.os.IBinder;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class TestService extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.     /*注册按钮*/  
  16.     private Button start = null;  
  17.     private Button stop = null;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         start = (Button)findViewById(R.id.button_01);  
  23.         start.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 /*绑定service*/  
  28.                 Intent bindIntent = new Intent(TestService.this, Service_01.class);  
  29.                 bindService(bindIntent, sconnection, Context.BIND_AUTO_CREATE);  
  30.             }  
  31.         });  
  32.           
  33.         stop = (Button)findViewById(R.id.button_02);  
  34.         stop.setOnClickListener(new OnClickListener() {  
  35.               
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 /*断开绑定service*/  
  39.                 unbindService(sconnection);  
  40.             }  
  41.         });  
  42.     }  
  43.     private Service_01 serviceBinder;  
  44.    /* 注册接口方法*/  
  45.     ServiceInterface mService = null;  
  46.    /* 绑定service监听*/  
  47.     ServiceConnection sconnection = new ServiceConnection() {  
  48.         /*当绑定时执行*/  
  49.         public void onServiceConnected(ComponentName name, IBinder service) {  
  50.             Log.d("activity--->""已绑定到service");  
  51.             mService = (ServiceInterface) service;  
  52.             if (mService != null) {  
  53.                 mService.show();//测试方法  
  54.             }  
  55.             Intent intent = new Intent();//这里只是为了下面传intent对象而构建的,没有实际意义  
  56.             /*绑定后就可以使用Service的相关方法和属性来开始你对Service的操作*/  
  57.             serviceBinder = ((Service_01.MyBinder) service).getService();  
  58.             /*比如:你可以掉用Service的onStartCommand()方法*/  
  59.             serviceBinder.onStartCommand(intent, 00);//0,0是我随意的参数  
  60.               
  61.         }  
  62.         /*当断开绑定时执行,但调用unbindService()时不会触发改方法*/  
  63.         public void onServiceDisconnected(ComponentName name) {  
  64.             Log.d("activity--->""已断开绑定service");  
  65.         }  
  66.     };  
0 0
原创粉丝点击