AdvertisingIdClient getAdvertisingIdInfo挂起永远 [英] AdvertisingIdClient getAdvertisingIdInfo hangs forever

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

问题描述

我试图让广告ID从谷歌播放服务API。下面是一个示例code:

I'm trying to get advertising ID from Google Play services API. Here is a sample code:

...
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方法,应用(含调试器无论与否)挂起,直到永远。

When I'm calling getAdvertisingIdInfo method, application hangs forever (no matter with debugger or not).

我使用Windows ADT 22.3,Android的SDK API 19日,谷歌播放SDK转。 16,安卓4.4.2的Nexus设备。我整合API如下描述:<一href="https://developer.android.com/google/play-services/id.html">https://developer.android.com/google/play-services/id.html

I'm using Windows ADT 22.3, Android SDK API 19, Google Play SDK rev. 16, Android 4.4.2 Nexus devices. I'm integrating API as described here: https://developer.android.com/google/play-services/id.html

可能是什么原因?

推荐答案

我找到了原因。它不应该阻止ONSTART()处理程序,因为阻塞情况下玩积木API中的ID设置获得。固定code是这样的:

I found the reason. It shouldn't block onStart() handler because blocked context blocks Play API in ID settings obtaining. Fixed code looks like this:

@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);
                finished(adInfo);
            } catch (...) {
                // All exceptions blocks
            }

            finished(null);
        }
    });

    thr.start();
}

private void finished(final AdvertisingIdClient.Info adInfo){
    if(adInfo!=null){
        // In case you need to use adInfo in UI thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Do some stuff with adInfo
            }
        });
    }
}

这将是有益的,如果官方说明了这种用法的意见。

It would be helpful if official instructions had such usage comments.

这篇关于AdvertisingIdClient getAdvertisingIdInfo挂起永远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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