2013-09-30 95 views
9


我試圖在使用之前檢查谷歌Play服務APK的可用性。我有一個 設備,該軟件包已過期(日誌中顯示「... Google Play服務已過期,需要3225100,但找到了3136134」)。
下面的代碼應處理這種情況並顯示一個對話框,提示用戶進行更新。對於一種原因不明的,以我 行Android GooglePlayServicesUtil.getErrorDialog()不顯示對話框

GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 

立即返回,沒有顯示出對話框(而不是阻塞的UI事件UI線程)。
您能否介紹一下可能發生的事情以及如何糾正代碼以便顯示對話框?

@Override 
protected void onResume() { 
    super.onResume(); 

    // Check device for Play Services APK. If check succeeds, proceed with 
    // GCM registration. 
    if (checkPlayServices()) { 
     gcm = GoogleCloudMessaging.getInstance(this); 
     regid = getRegistrationId(context); 

     if (regid == null || regid.length() == 0) { 
      registerInBackground(); 
     } else { 
      this.user.setGCMRegistrationId(regid); 
     } 
    } else { 
     Log.i(TAG, "No valid Google Play Services APK found."); 
    }  
} 

/** 
* Check the device to make sure it has the Google Play Services APK. If 
* it doesn't, display a dialog that allows users to download the APK from 
* the Google Play Store or enable it in the device's system settings. 
*/ 
private boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
     } else { 
      Log.i(TAG, "This device is not supported."); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
}  


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    case PLAY_SERVICES_RESOLUTION_REQUEST: 
     if (resultCode == RESULT_CANCELED) { 
     Toast.makeText(this, "Google Play Services must be installed.", 
      Toast.LENGTH_SHORT).show(); 
     finish(); 
     } 
     return; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
}  

回答

0

當我在我的項目中不正確地包含Google Play Services Library時,我遇到了一些類似於您的代碼的奇怪行爲。您必須將作爲項目導入,google-play-services_lib目錄您必須在您的類路徑中包含google-play-services.jar。原因是該項目包含一系列資源,包括爲getErrorDialog()顯示適當對話框所需的資源。

+0

我做了這兩件事,'getErrorDialog'仍然沒有任何作用。 – theblang

+0

最後按照建議做了,問題消失了。感謝提示。 – quirkfly

+0

我仍然看到相同的問題。 Sho9uld圖書館出口?它可以在模擬器中工作,不過 – Jorge