Ensure Google Play Services APK is updated

If your app is using any Google API in your project, then you need to make sure that your device has Google Play Services APK updated.

Firstly, you have to decide the accurate position where you need to insert the code to update Google Play Services.

If your app is entirely based on Google Play Services, then you can do this at the time your app launches initially. However, if only a part of your app uses Google Play Services, then you should do it whenever user needs to access that part of your app.

Now there are two ways to check availability of Google Play Services :

  1. Using GooglePlayServicesUtil
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if(status != ConnectionResult.SUCCESS)
{
        if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED)
        {
            Toast.makeText(context,"Please udpate your google play services",Toast.LENGTH_SHORT).show();
        }
}

But the problem with this approach is that some methods and fields of GooglePlayServicesUtil have been deprecated.

So, the second and better approach (which google also recommends) is

2. Using GoogleApiAvailability

GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();

int result = googleAPI.isGooglePlayServicesAvailable(activity);

if(result != ConnectionResult.SUCCESS)
{
   if (googleAPI.isUserResolvableError(result)) 
   {
      googleAPI.getErrorDialog(activity, result, AppConstant.PLAY_SERVICES_RESOLUTION_REQUEST).show();
   }
}

This will show you a Dialog containing an appropriate message about the error occured and the required action that will take you to play store to install or update Google Play Services.

Finally, your device will have Google Play Services up-to-date!!

Leave a Reply