advertisingidclient getadvertisingidinfo被主线程阻止

vatpfxk5  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(636)

我正在尝试等待advertisingidclient.getadvertisingidinfo(活动)的响应,但没有成功。在主线程完成之前,此方法不会响应。

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,等待时间更长(5s)

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()时。
原因是什么?如何做到这一点?

des4xlb0

des4xlb01#

要获取google广告id,您不需要运行该方法 getAdvertisingIdInfo 在主线程上。我使用async任务来管理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;
    }
}

您还需要添加到app build.gradle的dependencies行中

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

确保在androidsdk管理器上更新了“extrasgooglerepository”

a5g8bdjr

a5g8bdjr2#

在关于广告客户端的文献中,它说不要在主线程上使用。它将抛出一个异常。所以,如果你把它放到自己的线程中,很可能你会没事的。
广告客户参考

w46czmvw

w46czmvw3#

您看到的问题是,getadvertisingidinfo调用似乎没有足够长的时间来完成,这是由您如何等待以及runonuithread如何工作引起的。关键是runonuithread将在ui线程(在本例中是oncreate)上当前运行的代码之后对要运行的代码进行排队。用于“模拟等待”的休眠调用将让后台线程运行并执行其工作,但设置m\u info的最终操作将始终在oncreate完成后排队并执行。
一种解决方案是确保可以从多个线程安全地访问m\u信息,并且只需在后台线程上分配它。就不需要再读了。这将消除排队,并允许您的代码以最小的更改工作。
一个更好的解决方案是保持runouithread的使用,并取消用于等待的睡眠。您需要记住,在oncreate中m\u info始终为null,但是其他事件可以检查该值是否为非null,并根据需要使用它。
https://developer.android.com/reference/android/app/activity#runonuithread(java.lang.runnable)
在ui线程上运行指定的操作。如果当前线程是ui线程,则立即执行该操作。如果当前线程不是ui线程,则将操作发布到ui线程的事件队列。

093gszye

093gszye4#

根据文件(此处):
不幸的是,在这些情况下,使用com.google.android.gms.iid instanceid api或系统函数来创建应用程序范围的id是不合适的解决方案,因为id可能需要在应用程序之间共享。另一种解决方案是通过getid()方法使用advertisingidclient.info类提供的广告标识符。您可以使用getadvertisingidinfo(context)方法创建advertisingidclient.info对象,并调用getid()方法来使用标识符。注意这个方法是阻塞的,所以不应该从主线程调用它;有关此方法的详细说明,请参见此处。
这里还有:
public static adverisingidclient.info getadverisingidinfo(上下文)
检索用户的广告id并限制广告跟踪首选项。
无法在主线程中调用此方法,因为它可能会阻塞并导致anrs。如果在主线程上调用,则会引发illegalstateexception。
所以他们说它阻塞了。。。你需要把这段代码放在后台线程中。

相关问题