2011-08-15 8 views
4

작업이 성공적으로 시작된 경우 IntentService에 대해 알고 싶습니다. 가 실행을 유지하기 위해 IntentServicebindService()를 통해 결합하는 것이 가능으로IntentService가 시작되었는지 확인하는 방법

는 아마도 접근 방식은 서비스 대상에서 onStartCommand(..) 또는 onHandleIntent(..)의 호출에 startService(intent) 결과를 호출 여부를 확인하는 것입니다.

하지만 어떻게 활동에서 확인할 수 있습니까?

답변

2

나는 활동이 성공적으로 IntentService를 시작하면 알 수 싶습니다. 당신이 startService()를 호출 할 때 활동이나 서비스 중 하나에서 예외가없는 경우

은 다음 IntentService가 시작되었습니다. 이 bindService를 통해 IntentService를 결합하는 것이 가능으로

()

왜 계속 실행하려면?

+0

서비스가 시작되었는지 확인하고 싶습니다. 감사합니다 – cody

7

내 서비스가 실행 중인지 확인하는 방법은 다음과 같습니다. Sercive 클래스는 DroidUptimeService입니다.

private boolean isServiceRunning() { 
    ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE); 
    List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE); 

    if (serviceList.size() <= 0) { 
     return false; 
    } 
    for (int i = 0; i < serviceList.size(); i++) { 
     RunningServiceInfo serviceInfo = serviceList.get(i); 
     ComponentName serviceName = serviceInfo.service; 
     if (serviceName.getClassName().equals(DroidUptimeService.class.getName())) { 
      return true; 
     } 
    } 

    return false; 
} 
+0

감사하지만 내가 찾고 있어요 게 아니에요 - 내가 startService()가 호출되어 있는지 확인해야합니다, 서비스가 실제로 실행되고있는 경우는 아닙니다 ... – cody

+0

Hummm, 서비스 방송과 의도가 있어야 시작되었다고 말할 수 있습니다. 나에게 유일한 옵션처럼 보입니다. –

5

PendingIntent을 구성 할 때 플래그를 추가 할 수 있습니다. 반환 값이 null 인 경우 서비스가 시작되지 않습니다. 언급 된 플래그는 PendingIntent.FLAG_NO_CREATE입니다.

Intent intent = new Intent(yourContext,YourService.class); 
PendingIntent pendingIntent = PendingIntent.getService(yourContext,0,intent,PendingIntent.FLAG_NO_CREATE); 

if (pendingIntent == null){ 
    return "service is not created yet"; 
} else { 
    return "service is already running!"; 
} 
+0

예를 들어, AlarmManager를 사용하여 PendingIntent를 사용하여 서비스를 시작한 경우에만 작동합니다. 해당 FLAG에 대한 의사는 다음을 명시합니다. 설명 된 PendingIntent가 아직없는 경우이를 나타내는 대신 플래그를 생성합니다. 오래 실행되는 서비스는 작동하지 않습니다. – JavierSP1209

0

가 여기 내 서비스가 실행되고 있는지 내가 확인하기 위해 사용하는 방법입니다 ..

public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) { 
     ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE); 
     for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
      if (serviceClass.getName().equals(service.service.getClassName())) { 
       return service.started; 
      } 
     } 
     return false; 
    } 
관련 문제