Android Service两种启动方式

来源:互联网 发布:java web前端开发课程 编辑:程序博客网 时间:2024/05/09 01:44

1.Context.startService()方式启动 

①Context.startService()方式的生命周期: 启动时,startService –> onCreate() –> onStart()停止时,stopService –> onDestroy()如果调用者直接退出而没有停止Service,则Service 会一直在后台运行 Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法附代码

2.Context.bindService()方式启动:①Context.bindService()方式的生命周期: 绑定时,bindService -> onCreate() –> onBind()调用者退出了,即解绑定时,Srevice就会unbindService –>onUnbind() –> onDestory()Context.bindService()方式启动 Service的方法:绑定Service需要三个参数:bindService(intent, conn, Service.BIND_AUTO_CREATE);第一个:Intent对象第二个:ServiceConnection对象,创建该对象要实现它的onServiceConnected()和 onServiceDisconnected()来判断连接成功或者是断开连接第三个:如何创建Service,一般指定绑定的时候自动创建附代码

[java] view plaincopyprint?
  1. package com.dada.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. import com.dada.test.BindService.MyBinder;  
  15.   
  16. public class TestActivity extends Activity {  
  17.       
  18.     private boolean flag;  
  19.     private static final String TAG = "TestActivity";  
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         super.onCreate(savedInstanceState);   
  27.         setContentView(R.layout.main);   
  28.    
  29.         Button btnStart = (Button) findViewById(R.id.btnStart);   
  30.    
  31.         Button btnStop = (Button) findViewById(R.id.btnStop);   
  32.    
  33.           
  34.         btnStart.setOnClickListener(new View.OnClickListener() {   
  35.             @Override   
  36.             public void onClick(View v) {   
  37.                 //启动service 方式2   
  38.                 bindService();  
  39.             }   
  40.         });   
  41.           
  42.         btnStop.setOnClickListener(new View.OnClickListener() {   
  43.                
  44.             @Override   
  45.             public void onClick(View v) {   
  46.  //停止service 方式2   
  47.                 unBindService();  
  48.             }   
  49.         });   
  50.     }  
  51.       
  52.     //启动service 方式2   
  53.     //   
  54.     private void bindService(){  
  55.         Intent intent = new Intent(TestActivity.this,BindService.class);  
  56.         Log.i(TAG, "bindService()");  
  57.         bindService(intent, conn, Context.BIND_AUTO_CREATE);  
  58.     }  
  59.       
  60.     private void unBindService(){  
  61.         Log.i(TAG, "unBindService() start....");  
  62.         if(flag == true){  
  63.             Log.i(TAG, "unBindService() flag");  
  64.             unbindService(conn);  
  65.             flag = false;  
  66.         }  
  67.     }  
  68.       
  69. private ServiceConnection conn = new ServiceConnection() {  
  70.           
  71.         @Override  
  72.         public void onServiceDisconnected(ComponentName name) {  
  73.             // TODO Auto-generated method stub  
  74.             Log.i(TAG, "onServiceDisconnected()");  
  75.         }  
  76.           
  77.         @Override  
  78.         public void onServiceConnected(ComponentName name, IBinder service) {  
  79.             // TODO Auto-generated method stub  
  80.             Log.i(TAG, "onServiceConnected()");  
  81.             MyBinder binder = (MyBinder)service;  
  82.             BindService bindService = binder.getService1();  
  83.             bindService.MyMethod();  
  84.             flag = true;  
  85.         }  
  86.     };  
  87. }  

service

[java] view plaincopyprint?
  1. package com.dada.test;  
  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 BindService extends Service {  
  10.   
  11.     private static final String TAG = "BindService";  
  12.     private MyBinder myBinder = new MyBinder();  
  13.     public void MyMethod(){  
  14.         Log.i(TAG, "BindService-->MyMethod()");  
  15.     }  
  16.       
  17.     @Override  
  18.     public IBinder onBind(Intent intent) {  
  19.         Log.i(TAG, "BindService-->onBind()");  
  20.         return myBinder;  
  21.     }  
  22.       
  23.     public class MyBinder extends Binder{  
  24.           
  25.         public BindService getService1(){  
  26.             return BindService.this;  
  27.         }  
  28.     }  
  29.       
  30.     
  31.   
  32.     @Override  
  33.     public void onCreate() {  
  34.         Log.i(TAG, "BindService-->onCreate()");  
  35.         super.onCreate();  
  36.     }  
  37.   
  38.     @Override  
  39.     public void onStart(Intent intent, int startId) {  
  40.         Log.i(TAG, "BindService-->onStart()");  
  41.         super.onStart(intent, startId);  
  42.     }  
  43.   
  44.     @Override  
  45.     public void onDestroy() {  
  46.         Log.i(TAG, "BindService-->onDestroy()");  
  47.         super.onDestroy();  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean onUnbind(Intent intent) {  
  52.         Log.i(TAG, "BindService-->onUnbind()");  
  53.         return super.onUnbind(intent);  
  54.     }  
  55.       
  56. }  

运行日志

点击启动


点击停止


没有打出onServiceDisconnected的日志的原因:

注:SDK上是这么说的:This is called when the connection with the service has been 
unexpectedly disconnected -- that is, its process crashed. Because it is running in our same 
process, we should never see this happen.

所以说,只有在service因异常而断开连接的时候,这个方法才会用到

其他

由于Service 的onStart()方法只有在startService()启动Service的情况下才调用,故使用onStart()的时候要注意这点。

与 Service 通信并且让它持续运行

      如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService() 。当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了。


0 0
原创粉丝点击