由主线程阻止的AdvertisingIdClient getAdvertisingIdInfo [英] AdvertisingIdClient getAdvertisingIdInfo blocked by main thread

查看:341
本文介绍了由主线程阻止的AdvertisingIdClient getAdvertisingIdInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图等待AdvertisingIdClient.getAdvertisingIdInfo(活动)的响应没有成功。这个方法永远不会响应,直到主线程结束。

I'm trying to wait the response of AdvertisingIdClient.getAdvertisingIdInfo(activity) without success. This method never response until the main thread has finished.

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 p>

Logcat of this code

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

具有更多等待时间(5秒)的相同logcat

Same logcat with more waiting time (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被主线程阻止。

Each time the getAdvertisingIdInfo(), who is in another thread, is blocked by the main thread.

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

What is the reason ? and how to do this ?

推荐答案

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

To get the google ad ID you need not to run the method getAdvertisingIdInfo on the main thread. I use Async Task to manage the extraction of the google ad 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在线的依赖性

You also need to add to the app build.gradle on the dependencies the line

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

请确保您已在Android SDK管理器中更新EXTRAS Google存储库

And be sure you have on the Android SDK manager the "EXTRAS Google Repository" updated

这篇关于由主线程阻止的AdvertisingIdClient getAdvertisingIdInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆