gpt4 book ai didi

java - AdvertisingIdClient getAdvertisingIdInfo 被主线程阻塞

转载 作者:太空狗 更新时间:2023-10-29 12:39:47 26 4
gpt4 key购买 nike

我正在尝试等待 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()都被主线程阻塞。

这是什么原因?以及如何做到这一点?

最佳答案

要获取谷歌广告 ID,您无需在主线程上运行方法 getAdvertisingIdInfo。我使用 Async Task 来管理 Google 广告 ID 的提取。

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
String GAID; // this is the String of the Google Ad ID that you'll receive upon onPostExecute

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new GetGAIDTask().execute();
}

private class GetGAIDTask extends AsyncTask<String, Integer, String> {

@Override
protected String doInBackground(String... strings) {
AdvertisingIdClient.Info adInfo;
adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MainActivity.this.getApplicationContext());
if (adInfo.isLimitAdTrackingEnabled()) // check if user has opted out of tracking
return "did not found GAID... sorry";
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
return adInfo.getId();
}

@Override
protected void onPostExecute(String s) {
GAID = s;
}
}

您还需要在依赖项上向应用程序 build.gradle 添加行

compile 'com.google.android.gms:play-services-ads:7.8.0'

并确保您在 Android SDK 管理器上更新了“EXTRAS Google Repository”

关于java - AdvertisingIdClient getAdvertisingIdInfo 被主线程阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com