2013-07-28 34 views
2

当我打电话this.startService(new Intent(this, Some_Service.class));
我知道onCreate()被调用,其次是onStartCommand()服务在onStartCommand()完成后会自动停止吗?

bur如果onStartCommand()中的代码已完成,会发生什么情况?
Service自动拨打onDestroy()自行停止吗?
或者我必须拨打this.stopService(new Intent(this, Some_Service.class));来实现吗?

回答

2

如果您通过致电startService()启动服务,则必须致电stopService()stopSelf()停止该服务。如果您想在完成一些工作后停止服务,则可能需要使用IntentService

0

onStartCommand() - 当另一个组件(如活动)请求启动该服务时,系统通过调用startService()来调用此方法。一旦执行此方法,服务就会启动并可无限期地在后台运行。如果您执行此操作,则通过致电stopSelf()stopService(),您的责任是在其工作完成后停止服务。 (如果您只想提供绑定,则不需要实现此方法。)

Read Here