2017-12-01 7 views
-1

는 내가 가지고 그 코드의다음 활동으로 가기 전에 MyActivity가 GoogleApiClient 콜백을 기다리는 이유는 무엇입니까?

  GoogleApiClientUtility googleApiClientUtility = new GoogleApiClientUtility(
       app.getGoogleApiClient(), 
       new GoogleApiClient.ConnectionCallbacks() { 
        @Override 
        public void onConnected(@Nullable Bundle bundle) { 
         try { sleep(10000); } 
         catch (InterruptedException e) { e.printStackTrace(); } 
         Log.e("GoogleApiClient: %s", "onConnected"); 
         app.getGoogleApiClient().unregisterConnectionCallbacks(this); 
        } 
       } 
     ); 
     Log.e("GoogleApiClient: %s", "Connect"); 
     googleApiClientUtility.connect(); 

     Log.e("destination: %s", "main actvity"); 
     startActivity(new Intent(SplashActivity.this, MainActivity.class)); 
     this.finish(); 

나의 이해, 그것은 통과 직접 MainActivity 클래스로 이동합니다.

하지만 난 컴파일 때 주요 활동 클래스

더욱 이상한 내 로그 고양이로 이동하기 전에, 그것은 10 초를 기다린 이유는 다음과 같은 내 로그 보여

GoogleApiClient: Connect 
destination: main actvity <-- it should go to main activity here 
... wait 10 secs.... 
GoogleApiClient: onConnected; 

then after that it goes to main activity... 

사람 이유를 설명 할 수 있습니까? 및 onConnected 콜백을 대기하지 않고 사용자가 다른 활동으로 이동하도록하려면 어떻게해야합니까?

+0

잠을 제거하십시오. –

+0

나는 대기 시간을 시뮬레이트하기 위해 잠을 사용합니다. 정말 그 때문입니까? – AnD

+0

당신은 명시 적으로 활동을 10 초 동안 기다렸습니다. 예 –

답변

1

onConnected은 콜백이므로 보장 된 순서로 호출되지 않습니다. 대상 로그 메시지 전후에 로그를 인쇄 할 수 있습니다.

명시 적으로 ANR과 충돌하는 응용 프로그램을 일으킬 수있는 MainActivity 스레드를, 자고 있기 때문에 그것은 (응용 프로그램이 응답하지 것은)

당신이 연결을 위해 대기 할 경우, 이것은 대기

당신이 그것을 어떻게

GoogleApiClientUtility googleApiClientUtility = new GoogleApiClientUtility(
      app.getGoogleApiClient(), 
      new GoogleApiClient.ConnectionCallbacks() { 
       @Override 
       public void onConnected(@Nullable Bundle bundle) { 
        Log.e("GoogleApiClient: %s", "onConnected"); 
        // app.getGoogleApiClient().unregisterConnectionCallbacks(this); 
        Log.e("destination: %s", "main actvity"); 
        startActivity(new Intent(SplashActivity.this, MainActivity.class)); 
        SplashActivity.this.finish(); 
       } 
      } 
    ); 

그렇지 않으면 시간 경과를 시뮬레이션해야하는 이유가 명확하지 않습니다.

onStop 활동 중에 실제로는 unregisterConnectionCallbacks을 사용해야합니다.

+0

아니요, 연결을 기다리지 않으려 고합니다. 콜백이 진행되는 동안 주 활동을 계속 진행하고 싶습니다. 실행도 마찬가지로 – AnD

+0

좋아, 그럼 그냥 콜백에서 수면을 제거 –

+0

나는 대기 시간을 시뮬레이트하기 위해 잠을 사용하므로 콜백이 완료되기 전에 다음 활동으로 이동하는지 확인할 수 있습니다. – AnD