2014-12-02 69 views
2

的广告信息这是我们从Mopub和其他广告网络看到:广告SDK似乎无法获取设备

产生java.io.IOException:连接失败 com.google.android。 gms.ads.identifier.AdvertisingIdClient.g(未知 来源) com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(未知 来源)

他们都似乎有同样的问题。

奇怪的是,我们没有任何问题从我们的应用获取广告ID与以下来源。我们得到正确的广告ID,并且我们没有错误日志。 所有的SDK都遇到同样的问题(连接失败)。

任何帮助表示赞赏。

private void getAdvertisingId(AdvertisingIdHolder receiver) { 

    AdvertisingIdClient.Info adInfo = null; 
    String id = null; 
    boolean isLAT = false; 

    try { 
     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(App.getCtx()); 

     id = adInfo.getId(); 
     isLAT = adInfo.isLimitAdTrackingEnabled(); 
    } catch (IOException e) { 
     SLog.e("error", e); 
     // Unrecoverable error connecting to Google Play services (e.g., 
     // the old version of the service doesn't support getting AdvertisingId). 
    } catch (GooglePlayServicesNotAvailableException e) { 
     SLog.e("error", e); 
     // Google Play services is not available entirely. 
    } catch (GooglePlayServicesRepairableException e) { 
     e.printStackTrace(); 
    } 

    receiver.receive(id, isLAT); 
} 

回答

1

这段日子我经历了试验和错误,获取广告ID。最后我得到了它! 如果我们传入getApplicationContext()而不是当前活动的上下文,则可以解决连接错误。下面是我的工作代码:

private void getGaid() { 
    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       String gaid = AdvertisingIdClient.getAdvertisingIdInfo(
         getApplicationContext()).getId(); 
       if (gaid != null) { 
        Log.d("DEBUG", gaid); 
        // gaid get! 
       } 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (GooglePlayServicesRepairableException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (GooglePlayServicesNotAvailableException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 
} 

getGaid()可放入的onCreate()的onResume(),或视图的onClick(),只要该线程的主UI线程调用。

您可能需要的另一件事是更新谷歌播放服务库到最新版本。正如文中提到的here,IOException可能是由于旧版本的服务不支持获取AdvertisingId而导致的。

随意评论,如果有任何其他问题。