Whenever Google updates its Play Services APK, it’s important to update your app as well. Read on to learn about this quick process.

Tech mistake |If your app is using any Google API, then you need to make sure that your device has the 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 the user needs to access that part of your app.

Now there are two ways to check the 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 that occurred and the required action that will take you to the Play Store to install or update Google Play Services.

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

The article was originally published here.