Skip to content

Instantly share code, notes, and snippets.

@xizzhu
Created February 7, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xizzhu/8860381 to your computer and use it in GitHub Desktop.
Save xizzhu/8860381 to your computer and use it in GitHub Desktop.
Check Availability of Google Play Services
public class MainActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
// code that doesn't require Google Play services
if (!checkGooglePlayServices())
return;
// code that requires Google Play services
}
private boolean checkGooglePlayServices() {
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (result == ConnectionResult.SUCCESS) {
// the device has the latest Google Play services installed
return true;
} else {
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(result, this, 8964,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
});
if (errorDialog != null) {
// the problem can be fixed
// e.g. the user needs to enable or download the latest version
errorDialog.show();
} else {
// for some reason, the problem can't be fixed
// you should provide some notice to the user
}
return false;
}
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// you don't have to check it here,
// since the onResume() callback will be invoked anyway
if (requestCode == 8964) {
// this is invoked when user finishes operation
if (resultCode == Activity.RESULT_OK) {
// problem fixed, now you can use Google Play services
} else {
// problem not fixed, e.g. user decides not to install the latest version
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment