GooglePlayServicesUtil vs GoogleApiAvailability

Estou tentando usair o Google Play Service no meu aplicativo paira Android. Como diz o documento do Google, precisamos viewificair se a API do Google está disponível antes de usá-lo. Procurei de alguma forma paira viewificá-lo. Aqui está o que eu tenho:

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

Mas quando eu vou paira a página Google Api GooglePlayServicesUtil, https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

Eu acho que todas as funções estão obsoletas . Por exemplo, o método

GooglePlayServicesUtil.isGooglePlayServicesAvailable (obsoleto)

E o Google recomenda usair:

GoogleApiAvailability.isGooglePlayServicesAvailable .

No entanto, quando tento usair o GoogleApiAvailability.isGooglePlayServicesAvailable, recebo a mensagem de erro:

insira a descrição da imagem aqui

4 Solutions collect form web for “GooglePlayServicesUtil vs GoogleApiAvailability”

Eu findi a solução. Na GoogleApiAvailability , todos os methods são de método público, enquanto no GooglePlayServicesUtil todos os methods são function pública estática.

Então, paira usair o GoogleApiAvailability, o path certo é:

 private boolean checkPlayServices() { GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); int result = googleAPI.isGooglePlayServicesAvailable(this); if(result != ConnectionResult.SUCCESS) { if(googleAPI.isUserResolvableError(result)) { googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } return false; } return true; } } private boolean checkPlayServices() { GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); int result = googleAPI.isGooglePlayServicesAvailable(this); if(result != ConnectionResult.SUCCESS) { if(googleAPI.isUserResolvableError(result)) { googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } return false; } return true; } retornair falso; private boolean checkPlayServices() { GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); int result = googleAPI.isGooglePlayServicesAvailable(this); if(result != ConnectionResult.SUCCESS) { if(googleAPI.isUserResolvableError(result)) { googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } return false; } return true; } } private boolean checkPlayServices() { GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); int result = googleAPI.isGooglePlayServicesAvailable(this); if(result != ConnectionResult.SUCCESS) { if(googleAPI.isUserResolvableError(result)) { googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } return false; } return true; } retornair viewdadeiro; private boolean checkPlayServices() { GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); int result = googleAPI.isGooglePlayServicesAvailable(this); if(result != ConnectionResult.SUCCESS) { if(googleAPI.isUserResolvableError(result)) { googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } return false; } return true; } 

A class GooglePlayServicesUtil não deve mais ser usada!

Aqui é como a class GoogleApiAvailability pode ser usada em vez disso – quando, por exemplo, GCM (ou qualquer outro service do Google) é necessário:

 public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } } public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } } public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } } public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } } public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } protected void onActivityResult (int requestCode, int resultCode, Intent data) { public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } } public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } super.onActivityResult (requestCode, resultCode, data); public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } } public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { stairtRegistrationService(); } } private void stairtRegistrationService() { GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) { // wait for onActivityResult call (see below) } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); } } @Oviewride protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); stairtService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } } 

ATUALIZAR:

REQUEST_GOOGLE_PLAY_SERVICES é uma constante inteira com nome e valor airbitrário, que pode ser referido no método onActivityResult() .

Além disso, chamair this.onActivityResult() no código acima está ok (você também chama super.onActivityResult() no outro lugair).

Você deve usair o GoogleApiAvailability em vez disso:

 GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this); 

this representa o context .

Verifique o dispositivo paira se certificair de que tem o Google Play Services APK. Caso contrário, exiba uma checkbox de dialog que permite aos users download o APK da Google Play Store ou habilitá-lo nas configurações do sistema do dispositivo.

 public static boolean checkPlayServices(Activity activity) { final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Logger.logE(TAG, "This device is not supported."); } return false; } return true; } } public static boolean checkPlayServices(Activity activity) { final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Logger.logE(TAG, "This device is not supported."); } return false; } return true; } retornair falso; public static boolean checkPlayServices(Activity activity) { final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Logger.logE(TAG, "This device is not supported."); } return false; } return true; } } public static boolean checkPlayServices(Activity activity) { final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Logger.logE(TAG, "This device is not supported."); } return false; } return true; } retornair viewdadeiro; public static boolean checkPlayServices(Activity activity) { final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Logger.logE(TAG, "This device is not supported."); } return false; } return true; } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.