如果我有以下代码:如果服务多次启动会发生什么?
Intent intent = new Intent(this,DownloadService.class);
for(int i=0;i<filesArray.length;i++){
startService(intent);
}
在这段代码DownloadService
延伸IntentService
。
所以现在当我打电话startService(intent)
这是不是意味着我开始了新的服务,每次startService(intent)
被称为或是否意味着DownloadService
运行一次,然后每次我打电话时startService(intent)
它只是通过不同意图与不同的startId。
这是否有意义,这是哪种情况?
谢谢。它还表示:“startService()方法立即返回,Android系统调用服务的onStartCommand()方法。如果服务尚未运行,系统首先调用onCreate(),然后调用onStartCommand()。因此,如果服务已经在运行,那么系统将跳过onCreate()方法并仅调用onStartCommand()或者什么? – bytebiscuit
我真的不知道,但我很确定是这种情况(如果服务已经创建,使得很多人不会调用'onCreate()')。应该很容易确定是否在两个回调中都放置了Log.i(),并检查LogCat。 –
刚刚做到了这一点......奇怪的是,它在最后一次startService()调用后启动了onCreate! – bytebiscuit