【发布时间】:2015-01-15 10:43:59
【问题描述】:
我正在尝试等待 AdvertisingIdClient.getAdvertisingIdInfo(activity) 的响应,但没有成功。在主线程完成之前,此方法永远不会响应。
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import java.io.IOException;
public class MyActivity extends Activity {
private Activity m_activity = null;
private AdvertisingIdClient.Info m_info = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// start the thread with the getAdvertisingIdInfo()
startGoogleAdvertisingIdRequest(this);
// simulate a waiting loop, others app init, ...
for (int i=0; i<20; i++) {
SystemClock.sleep(100);
}
// get the uuid
String uuid = getGoogleAdvertisingId();
// call a method who need the uuid
Log.i("UUID", "receive uuid: " + uuid);
}
public String getGoogleAdvertisingId() {
String uuid = null;
if (m_info != null) {
if (!m_info.isLimitAdTrackingEnabled()) {
uuid = m_info.getId();
} else {
uuid = "another uuid";
}
} else {
uuid = "another uuid";
}
return uuid;
}
public void startGoogleAdvertisingIdRequest(final Activity activity) {
m_activity = activity;
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity) == ConnectionResult.SUCCESS) {
new Thread(new Runnable() {
@Override
public void run() {
AdvertisingIdClient.Info adInfo = null;
try {
Log.i("UUID", "before google request");
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(activity);
Log.i("UUID", "after google request");
} catch (IOException e) {
Log.w("UUID", "getAdvertisingIdInfo IOException: " + e.getMessage());
} catch (GooglePlayServicesNotAvailableException e) {
Log.w("UUID", "GooglePlayServicesNotAvailableException: " + e.getMessage());
} catch (Exception e) {
Log.w("UUID", "GooglePlayServicesException: " + e.getMessage());
} finally {
finished(adInfo);
}
}
}).start();
}
}
private void finished(final AdvertisingIdClient.Info adInfo){
if(adInfo != null){
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
m_info = adInfo;
Log.i("UUID", "runOnUiThread id: " + adInfo.getId());
}
});
}
}
}
这段代码的Logcat
11:29:52.103 30810-30828/com.example.testuuid I/UUID﹕ before google request
11:29:54.107 30810-30810/com.example.testuuid I/UUID﹕ receive uuid: another uuid
11:29:54.127 30810-30828/com.example.testuuid I/UUID﹕ after google request
11:29:54.151 30810-30810/com.example.testuuid I/UUID﹕ runOnUiThread id: d5dc3bfb-4756-490c-8f8e-2bedfb5e827a
相同的 logcat 等待时间更长(5 秒)
11:36:14.215 31413-31436/com.example.testuuid I/UUID﹕ before google request
11:36:19.225 31413-31413/com.example.testuuid I/UUID﹕ receive uuid: another uuid
11:36:19.293 31413-31436/com.example.testuuid I/UUID﹕ after google request
11:36:19.315 31413-31413/com.example.testuuid I/UUID﹕ runOnUiThread id: d5dc3bfb-4756-490c-8f8e-2bedfb5e827a
每次getAdvertisingIdInfo(),在另一个线程中,都会被主线程阻塞。
是什么原因?以及如何做到这一点?
【问题讨论】:
-
你考虑过使用AsyncTask吗?
-
@Fildor:是的,我试试AsyncTask,问题是一样的。
-
那么我的猜测是
m_info.getId();正在阻塞。 -
m_info.getId() 未在此处调用(回退到“另一个 uuid”,因为 m_info 为空,因为响应来得太晚了)。
-
您是否尝试过将对 isGooglePlayServicesAvailable 的调用放入线程中?
标签: java android multithreading google-play-services