2016-09-07 147 views
1

我想創建一個應用程序兼容到Android SDK 19(Kitkat),可以生成一個唯一的ID。我目前使用com.google.android.gms:play-services:7.0.0(因爲我看到它是奇巧兼容),我的代碼看起來像這樣ConnectionResult.SUCCESS,但仍然得到GooglePlayServicesNotAvailableException

final int answer = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); 
if (answer != ConnectionResult.SUCCESS) { 
    Log.w(TAG, "Google Play Services is NOT available. Status Code is " + answer); 
    return null; 
} 

// running in async 
try { 
    final AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context); 
    return advertisingIdInfo.getId(); 
} catch (Exception e) { 
    Log.w(TAG, "Unable to generate Advertising id info due to " + e.getMessage()); 
    e.printStackTrace(); 
} 

東西當運行這個對我的模擬器(的Nexus 5 + Android的19 +英特爾X86),我得到ConnectionResult.SUCCESS在我的if語句中,但仍然獲得GooglePlayServicesNotAvailableException

我想知道爲什麼,但似乎無法弄清楚。

任何想法爲什麼?

W/my.Class(2376):無法生成由於廣告的ID信息爲空

W/System.err的(2376):com.google.android.gms.common.GooglePlayServicesNotAvailableException

W/System.err的(2376):在com.google.android.gms.ads.identifier.AdvertisingIdClient.j(未知來源)

W/System.err的(2376):在com.google.android .gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown Source)

W/System.err的(2376):在my.Class $ 1 $ 1.doInBackground(Z.java:115)

W/System.err的(2376):在android.os.AsyncTask $ 2.call(的AsyncTask的.java:288) W/System.err的(2376):在java.util.concurrent.FutureTask.run(FutureTask.java:237)

W/System.err的(2376):在android.os。的AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)

W/System.err的(2376):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

W /系統.err(2376):在ja va.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)

W/System.err的(2376):在java.lang.Thread.run(Thread.java:841)

謝謝, 弗朗茨

+0

是谷歌播放服務安裝在您的模擬器上? –

回答

0

這可能是因爲大多數的GooglePlayServicesUtil類中的方法已過時,包括[isGooglePlayServicesAvailable][1]。您可以使用下面給出的代碼來檢查谷歌播放服務的可用性。

private boolean checkPlayServices() { 
     GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); 
     int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (apiAvailability.isUserResolvableError(resultCode)) { 
       apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) 
         .show(); 
      } else { 
       Log.i(TAG, "This device is not supported."); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
} 

更新: -你需要從7.0.0爲了使用上面給出的代碼升級發揮服務版本8.4.0。還請檢查this的答案。

相關問題