2015-07-03 128 views

回答

0

目前還不清楚您是否正在檢查設備上是否安裝了GooglePlayServices。我沒有看到您鏈接的代碼中的支票。

This webpage介紹如何驗證GooglePlayServices是否安裝在設備上以及如何處理故障。這些信息並不完全是最新的,但仍然提供了一個很好的處理輪廓。 GooglePlayServicesUtil類提供了用於檢查PlayServices是否已安裝的方法,並顯示一個對話框以允許用戶安裝它(如果缺失)。

一些來自網頁代碼:

private boolean checkPlayServices() { 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (status != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(status)) { 
      showErrorDialog(status); 
     } else { 
      Toast.makeText(this, "This device is not supported.", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 

private int REQUEST_CODE = 1234; 

private void showErrorDialog(int code) { 
    GooglePlayServicesUtil.getErrorDialog(code, this, REQUEST_CODE).show(); 
} 
+0

不工作,請檢查我的代碼:http://pastebin.com/RpvjEmKv –

+0

活動生命週期方法'onStart()'在'onResume()'之前被調用。你在'onStart()'中調用'connect()',所以它在'onResume()'中的'checkPlayServices()'之前被調用。 –

+0

好的,這段代碼改變了我的錯誤。不,我有ConnectionResult.getErrorCode()= 4 –

0

我在這裏玩的一些代碼,給你的建議:

strings.xml: 

<string name="lbl_play_service">This application needs Google Play Service, you must install it first.</string> 
    <string name="play_service_url">market://details?id=com.google.android.gms</string> 
<string name="play_service_web">https://play.google.com/store/apps/details?id=com.google.android.gms</string> 

Some javas: 

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

/** 
* To confirm whether the validation of the Play-service of Google Inc. 
*/ 
private void checkPlayService() { 
    final int isFound = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (isFound == ConnectionResult.SUCCESS) { 
     //Device has updated version of play-service. 
    } else { 
     //You need to store to update play-service. 
     new Builder(this).setTitle(R.string.application_name).setMessage(R.string.lbl_play_service).setCancelable(
       false).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.dismiss(); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setData(Uri.parse(getString(R.string.play_service_url))); 
       try { 
        startActivity(intent); 
       } catch (ActivityNotFoundException e0) { 
        intent.setData(Uri.parse(getString(R.string.play_service_web))); 
        try { 
         startActivity(intent); 
        } catch (Exception e1) { 
         //Ignore now. 
        } 
       } finally { 
        finish(); 
       } 
      } 
     }).create().show(); 
    } 
} 
+0

「new Builder」沒有找到 –

+0

@christianrusso你試過這些代碼嗎? – TeeTracker

+0

我複製你的代碼和IDE說「無法解析符號生成器」 –