Android GooglePlayServices context error

543 Views Asked by At

I'm trying to make a Google play services check for my android application. I followed a yt guide at this link: https://www.youtube.com/watch?v=i2lKeTJpaWc And created the following code:

public boolean servicesOK() {
    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {
        return true;
    }
    else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();
    }
    else {
        Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
    }
    return false;
}

The code executed correctly without any problems, so i decided to make it into an class file. Thats when the problems occurred; i get the following error-message:

"The method isGooglePlayServicesAvailable(Context) in the type GooglePlayServicesUtil is not applicable for the arguments (PlayServices)"

The complete code for the class:

package com.test.testmaps2;
import android.app.Dialog;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class PlayServices {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;


public boolean servicesOK() {
    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {
        return true;
    }
    else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();
    }
    else {
        Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
    }
    return false;
}

}

1

There are 1 best solutions below

0
Simas On BEST ANSWER

As the error suggests you need to pass Context to isGooglePlayServicesAvailable. For that you will probably want to fetch the context as an argument to your servicesOK method:

public boolean servicesOK(Context ctx) {
    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(ctx);

Then call this method from your activity like:

servicesOK(this);