1

Android 애플리케이션이 있습니다. 하나 개의 활동에서 나는이 같은 서비스를 시작 다음과 같이IntentService STICKY가 파괴되었지만 서비스가 Android 2.3.4에서 로깅 데이터를 유지합니다.

Intent startIntent = new Intent(_context, HandlingService.class); 
_context.startService(startIntent); 

HandlingService가 정의 :

HandlingService 내부 그리고
public class HandlingService extends IntentService 

나는이 있습니다

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, startId, startId); 
    Log.v(ApplicationName,"HandlingService.onStartCommand"); 
    if ((flags & START_FLAG_RETRY) == 0){ 
     Log.v(ApplicationName,"Service is restarting"); 
    } 
    return START_STICKY; 
} 

이를 :

@Override 
protected void onHandleIntent(Intent sourceIntent) { 
    Log.v(ApplicationName, "HandlingService.onHandleIntent"); 
    sendSomething(); 
} 

그리고 마지막으로 : 나는 HandlingService를 참조

Intent stopIntent = new Intent(CallingActivity.this, HandlingService.class); 
CallingActivity.this.stopService(stopIntent); 

:

protected void sendSomething() { 
    while (numberOfTry > 0) { 
     numberOfTry++; 
     Log.v(ApplicationName,"HandlingService.sending Something. Try# " + numberOfTry); 
    } 
} 

numberOfTry 내가이 부르는 취소 버튼을 클릭 서비스를 시작하는 활동에서 그런 1.

에서 시작 .OnDestroy가 호출되었지만 "HandlingService.sending Something"으로 로그를 계속 볼 수 있습니다. # "과 증가 숫자를 시도

질문 :.?!. 그것이 내가 이미 stopService하기 위해 호출을 중지하면 살아 유지하는 이유

감사를 미리 기예르모에

답변

6

documentation for IntentService이 명시 적으로해야한다고 파생 클래스 하지 재정 onStartCommand()은. 당신은 아마 IntentService의 내부 역학을 엉망으로하고 있습니다.

어느 것이 Service에서 직접 클래스를 파생하거나 FRA를 사용 IntentService가 이미 당신에게 (시작/중지 자체를 위해)주는 작업입니다. IntentService javadoc의에서

+0

하지만 OnStartCommand를 재정의하지 않으면 어떻게하면 어떨까요? 왜 그들이 그것을 오버라이드 할 수있게 만들지 만 문서에서는 "하지 마라"??? – polonskyg

+2

'IntentService'는 자체 시작/중지 의미론을 가지고 있습니다. 작업자 스레드를 시작하고 대기열에서 수행 한 작업을 처리하고 완료되면 자체 작업을 중지합니다. 어떤 것도 무시할 필요가 없습니다. 문서화 된대로 작동합니다.작동 방식이 마음에 들지 않는다면 서비스를 확장하는 자체 서비스를 만들면됩니다. 'IntentService'가 이미 제공하고있는 시작/중지 의미는 무엇이 잘못 되었습니까? 당신이하려고하는 것은'IntentService'의 기본 행동으로 작동하지 않는 것입니까? –

+0

조건에 도달 할 때까지 서비스가 계속 실행되어야하며 한 번만 실행하고 싶지 않습니다. – polonskyg

2

공공 무효 setIntentRedelivery (부울 사용)

설정 의도 재 전달 환경 설정. 일반적으로 원하는 의미로 생성자에서 호출됩니다.

onStartCommand (Intent, int, int)는 START_REDELIVER_INTENT를 반환하므로 onHandleIntent (Intent)가 반환되기 전에이 프로세스가 종료되면 프로세스가 다시 시작되고 의도가 다시 전달됩니다. 여러 개의 인 텐트가 전송 된 경우 가장 최근의 인 텐트 만 재전송 할 수 있습니다.

enabled (기본값) 인 경우 onStartCommand (Intent, int, int)는 START_NOT_STICKY를 반환하고 프로세스가 종료되면 Intent가 함께 사망합니다.