【问题标题】:AdvertisingIdClient getAdvertisingIdInfo hangs foreverAdvertisingIdClient getAdvertisingIdInfo 永远挂起
【发布时间】:2014-04-30 04:48:14
【问题描述】:

我正在尝试从 Google Play 服务 API 获取广告 ID。这是一个示例代码:

...
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 方法时,应用程序永远挂起(无论是否使用调试器)。

我正在使用 Windows ADT 22.3、Android SDK API 19、Google Play SDK rev。 16、Android 4.4.2 Nexus 设备。我正在集成 API,如下所述:https://developer.android.com/google/play-services/id.html

可能是什么原因?

【问题讨论】:

    标签: android google-play-services ads freeze


    【解决方案1】:

    我找到了原因。它不应该阻塞 onStart() 处理程序,因为阻塞的上下文会在 ID 设置获取中阻塞 Play API。固定代码如下所示:

    @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
                }
            });
        }
    }
    

    如果官方说明有这样的用法cmets会很有帮助。

    【讨论】:

    • 我同意,Google 在这方面的示例代码很糟糕!
    • 当我关注他们的文档时,我收到了一个很好的 phat lint 错误Unreachable catch block for GooglePlayServicesAvailabilityException. This exception is never thrown from the try statement body。在我作为 Windows 开发人员的一生中,微软至少正确地记录了他们的东西 - 并允许对文档进行反馈! FFS
    • 或者更好的是,使用 AsyncTask。
    • 这是唯一对我有用的方法。谢谢!
    【解决方案2】:

    不幸的是,getAdvertisingIdInfo 调用需要从后台线程完成,您不应在调用主线程时阻塞它。似乎没有同步获取 AdId 的选项。

    ---- 编辑----

    我可以通过在新线程上运行来实现这一点。这是最终对我有用的东西。它不再挂了(可能不是理想的)

    getGAIDSync(){
     final CountDownLatch  latch  = new CountDownLatch(1);
     new Thread(new Runnable() {
                @Override
                public void run() {               
                    getGoogleAdsIDAsyncTask.execute().get(5000, TimeUnit.MilliSecond);
                   latch.countDown();                
        }}).start();
        try {
            latch.await(5000,TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
             e.printStackTrace();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2011-09-27
      相关资源
      最近更新 更多