Android GooglePlayServices上下文错误

时间:2014-11-19 17:20:35

标签: java android class android-context

我试图为我的Android应用程序进行Google Play服务检查。 我在此链接上关注了指南:https://www.youtube.com/watch?v=i2lKeTJpaWc 并创建了以下代码:

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 method isGooglePlayServicesAvailable(Context) in the type GooglePlayServicesUtil is not applicable for the arguments (PlayServices)"

该课程的完整代码:

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 个答案:

答案 0 :(得分:0)

由于错误提示您需要将Context传递给isGooglePlayServicesAvailable。为此,您可能希望获取上下文作为servicesOK方法的参数:

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

然后从您的活动中调用此方法,如:

servicesOK(this);