I am trying to use Google Play Service in my Android app. As Google document says, we need to check if the Google API is available before using it. I have searched some way to check it. Here is what I got:
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; }
But when I go to Google Api GooglePlayServicesUtil page, https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil
I find all functions are deprecated. For example, the method
GooglePlayServicesUtil.isGooglePlayServicesAvailable (deprecated)
And Google recommends to use:
GoogleApiAvailability.isGooglePlayServicesAvailable.
However, when I try to use GoogleApiAvailability.isGooglePlayServicesAvailable, I get the error message:
Use GoogleApiAvailability.isGooglePlayServicesAvailable (Context) instead. Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.
It will direct them to one of the following places to either the Play Store if Google Play services is out of date or missing, or system settings if Google Play services is disabled on the device. error code returned by isGooglePlayServicesAvailable (Context) call.
Determines whether an error can be resolved via user action. If true, proceed by calling GoogleApiAvailability.getErrorDialog (Activity, int, int) and showing the dialog. Attempts to make Google Play services available on this device. If Play Services is already available, the returned Task may complete immediately.
Use GoogleApiAvailability.showErrorNotification (Context, int) instead. Displays a notification relevant to the provided error code. This method is similar to getErrorDialog (int, android.app.Activity, int), but is provided for background tasks that cannot or shouldn't display dialogs.
I have found the solution. In the GoogleApiAvailability
, all methods are public method, while in GooglePlayServicesUtil
all methods are static public function.
So to use GoogleApiAvailability, the right way is:
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With