0
我正在使用IntentService
来调用Web服务。IntentService在检查服务是否正在运行时返回False
如何,我打电话来启动意图服务:
if (isMyConServiceRunning(TaskService.class,context) == false) {
Log.d(TAG,"STARTING TaskService");
context.startService(TasksIntentUploadDownload);
}
isMyConServiceRunning方法:
private static boolean isMyConServiceRunning(Class<?> serviceClass,Context context)
{
try {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().toLowerCase(Locale.ENGLISH).contains(service.service.getClassName().toLowerCase(Locale.ENGLISH))) {
return true;
}
}
}
catch(Exception e)
{}
return false;
}
isMyConServiceRunning
总是返回false
(该服务没有运行),因此服务正在启动多个倍,但我注意到,当我重写方法OnStartCommand
并将Web服务调用OnStartCommand
而不是OnHandleIntent
并使OnStartCommand
返回START_STICKY
,则isMyConServiceRunning
将认识到该服务正在运行。
运行OnStartCommand
中的任务是不是很糟糕?
如果是,我该如何修复多个呼叫来运行IntentService
?
为什么你检查服务已经运行? –
我正在检查服务是否没有运行,然后如果它不是我开始服务 –
,并且上面一直返回false –