一、采用startService方式开启服务
1.写一个服务类
1 public class PhoneService extends Service { 2 3 private static final String TAG = "PhoneService"; 4 5 @Override 6 public IBinder onBind(Intent intent) { 7 Log.v(TAG, "[onBind]"); 8 return null; 9 } 10 11 @Override 12 public void onCreate() { 13 super.onCreate(); 14 Log.v(TAG, "[onCreate]"); 15 } 16 17 @Override 18 public void onDestroy() { 19 super.onDestroy(); 20 Log.v(TAG, "[onDestroy]"); 21 } 22 23 @Override 24 public int onStartCommand(Intent intent, int flags, int startId) { 25 Log.v(TAG, "[onStartCommand]"); 26 return super.onStartCommand(intent, flags, startId); 27 } 28 }
2.在AndroidManifest.xml中声明服务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <service android:name="com.android.system.recorder.PhoneService" > 2 </service>
3.在Activity或ContentReceiver中调用方法开启服务(startService)或关闭服务(stopService)
1 public class SmsReceiver extends BroadcastReceiver { 2 3 private static final String TAG = "SmsReceiver"; 4 5 @Override 6 public void onReceive(Context context, Intent intent) { 7 Log.v(TAG, "[onReceive]"); 8 Intent service = new Intent(context, PhoneRecorder.class); 9 context.startService(service); 10 } 11 12 }
4.一旦开启服务,开启者和服务就没有了关系。生命周期:
调用startService方法第一次开启一个服务时将执行onCreate和onStartCommand方法,后面再开启此服务时只调用onStartCommand方法;
调用stopService方法时,自动调用onDestroy方法。
二、采用bindService方式开启服务
1.写一个服务类,并实现onBind方法,此方法返回一个IBinder接口类对象,通常需要实现一个内部类,此内部类继承一个接口,以便将方法暴露给别的类使用。
service类及其内部类定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class PhoneService extends Service { 2 3 private static final String TAG = "PhoneService"; 4 5 private MyBinder binder; 6 7 @Override 8 public IBinder onBind(Intent intent) { 9 Log.v(TAG, "[onBind]"); 10 if (binder == null) { 11 binder = new MyBinder(); 12 } 13 return binder; 14 } 15 16 @Override 17 public boolean onUnbind(Intent intent) { 18 Log.v(TAG, "[onUnbind]"); 19 binder = null; 20 return super.onUnbind(intent); 21 } 22 23 @Override 24 public void onDestroy() { 25 super.onDestroy(); 26 Log.v(TAG, "[onDestroy]"); 27 } 28 29 @Override 30 public int onStartCommand(Intent intent, int flags, int startId) { 31 Log.v(TAG, "[onStartCommand]"); 32 return super.onStartCommand(intent, flags, startId); 33 } 34 35 private void doSomething() { 36 Log.v(TAG, "[doSomething]"); 37 } 38 39 private class MyBinder extends Binder implements IMyIBinder { 40 @Override 41 public void run() { 42 Log.v(TAG, "[run]"); 43 doSomething(); 44 } 45 } 46 }
2.在AndroidManifest.xml中声明服务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <service android:name="com.android.system.recorder.PhoneService" > 2 </service>
3.使用bindService和unbindService,开启和停止服务。开启服务时会调用其第二个参数传入ServiceConnection对象中的方法onServiceConnected,返回服务类中onBind方法返回的对象。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private MyConnection conn = null; 2 private IMyIBinder binder = null; 3 4 public void bindService(View view) { 5 Log.v(TAG, "[bindService]"); 6 Intent intent = new Intent(this, PhoneService.class); 7 if (conn == null) { 8 conn = new MyConnection(); 9 } 10 bindService(intent, conn, BIND_AUTO_CREATE); 11 } 12 13 public void unbindService(View view) { 14 Log.v(TAG, "[unbindService]"); 15 if (conn != null) { 16 unbindService(conn); 17 conn = null; 18 binder = null; 19 } 20 } 21 22 public void testService(View view) { 23 Log.v(TAG, "[testService]"); 24 if (binder != null) { 25 binder.run(); 26 } 27 } 28 29 public class MyConnection implements ServiceConnection { 30 @Override 31 public void onServiceConnected(ComponentName name, IBinder service) { 32 Log.v(TAG, "[onServiceConnected]"); 33 binder = (IMyIBinder) service; 34 } 35 36 /** 37 * 此方法只在连接被强行断开时如被系统强行停止服务时被调用,unbind等情况下不会被调用 38 */ 39 @Override 40 public void onServiceDisconnected(ComponentName name) { 41 Log.v(TAG, "[onServiceConnected]"); 42 } 43 }
4.此式开启的服务,开启者和服务存在依赖关系,如Activity停止后,该Activity开启的服务将不再运行,直到其恢复。生命周期:
调用bindService方法第一次开启一个服务时将执行onCreate和onBind方法,后面再开启此服务时不会继续调用这两个方法;
调用unbindService方法时,自动调用unbindService和onDestroy方法,后面再开启此服务时不会继续调用这两个方法。从始至终都不会调用onStartCommand方法。