배고픈 개발자 이야기

안드로이드 백그라운드 [Service]를 디버깅하는 방법 본문

언어/Android Studio

안드로이드 백그라운드 [Service]를 디버깅하는 방법

이융희 2019. 12. 4. 15:09
728x90

android 용 백그라운드로 돌아가는 응용 프로그램을 빌드 및 설치하였습니다.

일반적인 안드로이드 앱 디버그 모드로 프로젝트에 breakpoint를 걸었을 때 백그라운드 서비스는 멈추지 않기 때문에

디버거가 연결되도록(intent 날릴 때) 코드에서 선언적으로 대기하는 방법을 사용해 보겠습니다.

 

그렇게 하려면 다음 step을 따라야 합니다.

1. 서비스 내 method 시작부분에 다음과 같이 코드를 추가합니다.

/* (non-Javadoc)    
 * @see android.app.Service#onCreate()
 */
@Override
public void onCreate() {
    super.onCreate();
    //whatever else you have to to here...
    android.os.Debug.waitForDebugger();  // this line is key
}

 

2. break points를 waitForDebugger을 선언한 이후 어디든지 걸어줍니다.

 

3. IDE의 debug버튼을 통해 app을 launch 합니다.

 

4. adb를 launch하고 다음 command와 함께 서비스를 실행합니다.

  • cd $PLATFORM_TOOLS
  • adb shell
  • am startservice -n                                                                                                                            com.google.android.apps.gtalkservice/com.google.android.gtalkservice.service.GTalkService

ex) main->java->com.google.test->start 코드가 있으면

am startservice -n com.google.test/.start를 입력하여 서비스를 실행합니다.

 

다음으로 디버그 아이콘에서 오른쪽에 있는 Attach Debugger to Android Process를 선택하여

디버깅할 백그라운드 서비스를 선택하면 실행중인 프로젝트에서 디버깅을 정상적으로 해볼 수 있습니다.

 

 

Comments