While the provided answers will work for an emulator, this could be a real issue on a real device and these solutions will not work on a real device. This could happen on an old device that has an outdated version of Google Play Services.
To overcome this issue, the Google Play Services library has a set of APIs to check the availability of Google Play Services, mainly the GoogleApiAvailability
class provides a few methods to handle availability issues such as an outdated version.
Now, to detect whether a device has Google Play Services available or not, what I normally do is create a helper method to run the check
private boolean checkPlayServices(){
GoogleApiAvailability gaa = GoogleApiAvailability.getInstance();
int result = gaa.isGooglePlayServicesAvailable(getApplicationContext());
if(result != ConnectionResult.SUCCESS){
if(gaa.isUserResolvableError(result)){
gaa.getErrorDialog(this,result, REQUEST_PLAY_SERVICES_RESOLUTION).show();
}
return false;
}
return true;
}
This helper method checks whether Google Play Services is available or not by inspecting the error code returned from isGooglePlayServicesAvailable
. Then it calls isUserResolvableError
to determine if the error is resolvable by the user (e.g. by updating Google Play Services). If it is resolvable, then a dialog is displayed for the user to confirm he wants the system to resolve the error. Then call it on the Activity's onCreate
method, I run that check as below
//I normally assume Google Play services is available
private boolean playServicesAvailable = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_selector);
//check for rare case where Google Play Services is not available
playServicesAvailable = checkPlayServices();
if(!playServicesAvailable){
//hide UI elements and turn off features that rely on Google Play Services
}
}
Hope it helps some out there