2014-04-30 4 views
9

Google Play 서비스 API에서 광고 ID를 얻으려고합니다.AdvertisingIdClient getAdvertisingIdInfo가 영원합니다.

... 
import com.google.android.gms.ads.identifier.AdvertisingIdClient; 
import com.google.android.gms.common.GooglePlayServicesNotAvailableException; 
import com.google.android.gms.common.GooglePlayServicesRepairableException; 
... 

public class MyActivity extends Activity { 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Thread thr = new Thread(new Runnable() { 
      @Override 
      public void run() { 
      try { 
       Context ctx = MyActivity.this.getApplicationContext(); 
       AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(ctx); 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (GooglePlayServicesRepairableException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (GooglePlayServicesNotAvailableException e) { 
       e.printStackTrace(); 
      } 
     }); 

     thr.start(); 
     synchronized (thr) { 
      try { 
       thr.join(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

내가 getAdvertisingIdInfo 메소드를 호출하고있어, 응용 프로그램이 중단 영원히 (상관없이 디버거 여부) : 다음은 샘플 코드입니다.

Windows ADT 22.3, Android SDK API 19, Google Play SDK rev.를 사용하고 있습니다. 16, Android 4.4.2 Nexus 기기 여기에 설명 된대로 API를 통합하려고합니다. https://developer.android.com/google/play-services/id.html

어떤 이유가있을 수 있습니까?

답변

10

이유를 찾았습니다. 차단 된 컨텍스트가 ID 설정 획득에서 Play API를 차단하기 때문에 onStart() 핸들러를 차단해서는 안됩니다. 고정 코드는 다음과 같습니다.

@Override 
protected void onStart() { 
    super.onStart(); 
    Thread thr = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       Context ctx = MyActivity.this.getApplicationContext(); 
       AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(ctx); 
       finished(adInfo); 
      } catch (...) { 
       // All exceptions blocks 
      } 

      finished(null); 
     } 
    }); 

    thr.start(); 
} 

private void finished(final AdvertisingIdClient.Info adInfo){ 
    if(adInfo!=null){ 
     // In case you need to use adInfo in UI thread 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       // Do some stuff with adInfo 
      } 
     }); 
    } 
} 

공식적인 지침에 사용법 설명이있는 경우 도움이됩니다.

+2

Google의 샘플 코드는 매우 열악합니다. –

+0

그리고 내가 그들의 문서를 따라 가면 phat lint 에러 인'GooglePlayServicesAvailabilityException에 대한 Unreachable catch 블록 '이 생깁니다. 이 예외는 try 문 본문에서 결코 throw되지 않습니다. Windows 개발자의 평생 동안 Microsoft는 최소한 문서를 제대로 문서화했으며 문서에 대한 피드백을 허용했습니다. FFS –

+0

아니면 AsyncTask를 사용하십시오. – RominaV

1

불행히도 getAdvertisingIdInfo 호출은 백그라운드 스레드에서 수행해야하며, 호출하는 동안 주 스레드를 차단하면 안됩니다. AdId를 동 기적으로 가져올 수있는 옵션이없는 것처럼 보입니다. 나는 새 스레드에서 실행하여이를 달성 할 수

---- 편집 ----. 마침내 나를 위해 일하는 것이 있습니다. 더 이상 매달려 있지 않습니다. 이상이 아닐 수도 있습니다.

getGAIDSync(){ 
final CountDownLatch latch = new CountDownLatch(1); 
new Thread(new Runnable() { 
      @Override 
      public void run() {    
       getGoogleAdsIDAsyncTask.execute().get(5000, TimeUnit.MilliSecond); 
       latch.countDown();     
    }}).start(); 
    try { 
     latch.await(5000,TimeUnit.MILLISECONDS); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

} 
관련 문제