2014-07-11 8 views
1

AlarmManager를 사용하여 매 30 초마다 주 서비스를 시작하는 유지 보수 IntentService가 있습니다. IntentService를 사용하는 이유는 MainService가 백그라운드 스레드에서 실행되기를 원하기 때문입니다.새로운 서비스를 시작하는 IntentService - 어떤 스레드에서?

내 질문은 - IntentService가 startService (new Intent (this, MainService.class))를 사용하여 새 서비스를 시작하면 어떤 스레드가 MainService? IntentService의 스레드 또는 UI 스레드?

여기 내 코드가 있습니다. 미리 감사드립니다!

/** 
* A service that maintains all the required parts of Smoove alive. In case of a 
* system startup or a crash of the main service, WatchDogService restarts the 
* required service 
*/ 
public class WatchDogService extends IntentService { 

// Holds the alarm manager instance. 
AlarmManager alarmMgr = null; 


public WatchDogService() { 
    super("WatchDogService"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    log.info("WatchDogService onHandleIntent"); 
    Intent intentMainService = new Intent(this, MainService.class); 
    intentMainService.addFlags(Intent.FLAG_FROM_BACKGROUND); 
    startService(intentMainService); 

    } 

    if(!isRegisteredToAlarmManager){ 
     alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     registerToAlarmManager(); 
    } 
} 

// Registers the service to the alarm manager. to start in every INTERVAK 
// seconds. 
private void registerToAlarmManager() { 
    // Build the intent. 
    log.info("entered registerToAlarmManager"); 
    Intent intent = new Intent(this.getApplicationContext(),WatchDogService.class); 
    PendingIntent pendingIntent = PendingIntent.getService(
      this.getApplicationContext(), 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    // Pull the alarm manager service to register the service. 
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 0, 
      INTERVAL * 1000, pendingIntent); 

    isRegisteredToAlarmManager = true; 
} 

@Override 
public IBinder onBind(Intent arg0) { 
    return null; 
} 

은}

답변

3

서비스 항상 프로세스의 주요 스레드를 실행합니다. startService()을 실행하는 스레드는 기껏해야 중요하지 않습니다 (최악의 경우 문제가 발생할 수 있지만 호출 된 서비스가 해당 스레드에서 실행되지는 않음).

(그냥 명확하게하기 위해 IntentService - 배경 스레드에서 onHandleIntent() 만 호출).

+0

아마도 * 메인 스레드 * – Blackbelt

+0

@blackbelt에서 * 실행하면 더 분명 할 것입니다. 요점을 더 분명하게하려고 노력했다. – matiash

+1

새 서비스가 메인 스레드인지 확인하십시오. Looper.getMainLooper(). getThread() == Thread.currentThread(); 또는 인쇄 ID : Thread.currentThread(). getId() – Prakash

관련 문제