Skip to content

Instantly share code, notes, and snippets.

@allenchi
Created May 6, 2015 06:46
Show Gist options
  • Save allenchi/6c335428321b8a7530f6 to your computer and use it in GitHub Desktop.
Save allenchi/6c335428321b8a7530f6 to your computer and use it in GitHub Desktop.
Check Google Play Service
// check if a compatible play services is available on the device
// if not, show a dialog that takes the user to Google Play Store
// to install the required play service
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;
}
// show the error dialog
void showErrorDialog(int code) {
GooglePlayServicesUtil.getErrorDialog(code, this,
REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
}
// a request code, you can set the value to anything you like
static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 3333;
// handles play service error dialog result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_RECOVER_PLAY_SERVICES:
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_LONG).show();
finish();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment