Service的两种启动方法

刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别。

startService 和 bindService

startService被形容为我行我素,而bindService被形容为 不求同日生,但求同日死。

因为startService启动的service,service会一直在后台运行,即使调用者已经死掉。而且如果调用者下一次再起来还可以去调用stopService。

而bindService只会onCreate,这时调用者和service是绑定在一起的关系,如果调用者退出了,service就会调用onUnbind--> 然后 onDestroy 自杀。

下面分别是两种启动方法的代码:

1.Intent intent = new Intent("com.homer.service.musicService");

   startService(intent);

2.Intent intent = new Intent("com.homer.bind.bindService");

   bindService(intent, sc, Context, BIND_AUTO_CREATE);

原文地址:https://www.cnblogs.com/yingzi/p/3326604.html