我試圖從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
可能是什麼原因?
我同意,谷歌的這個例子代碼就是這麼差! –
當我按照他們的文檔,我得到一個很好的phat lint錯誤'無法到達GooglePlayServicesAvailabilityException的catch塊。這個異常永遠不會從try語句體中拋出。在我的一個Windows開發人員的生活中,微軟至少記錄下他們的東西 - 並允許對這些文檔提供反饋! FFS –
或更好的,使用AsyncTask。 – RominaV