GooglePlayServicesUtil与GoogleApiAvailability

时间:2015-06-24 02:40:50

标签: android

我正在尝试在我的Android应用中使用Google Play服务。正如Google文档所述,我们需要在使用之前检查Google API是否可用。我已经搜索了一些方法来检查它。这是我得到的:

private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                PLAY_SERVICES_RESOLUTION_REQUEST).show();
    } else {
        Log.i(TAG, "This device is not supported.");
        finish();
    }
    return false;
}
return true;
}

但是当我去Google Api GooglePlayServicesUtil页面时, https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

我发现所有功能都已弃用。例如,方法

GooglePlayServicesUtil.isGooglePlayServicesAvailable (已弃用)

Google建议使用:

GoogleApiAvailability.isGooglePlayServicesAvailable

但是,当我尝试使用GoogleApiAvailability.isGooglePlayServicesAvailable时,收到错误消息:

enter image description here

6 个答案:

答案 0 :(得分:197)

我找到了解决方案。在import maya.cmds as cmds # Will return EVERYTHING selected all_selected = cmds.ls(sl=True) # Will filter out and return just the verts # from everything selected just_the_selected_verts = cmds.filterExpand(sm=31) 中,所有方法都是公共方法,而在GoogleApiAvailability中,所有方法都是静态公共函数。

所以要使用GoogleApiAvailability,正确的方法是:

GooglePlayServicesUtil

答案 1 :(得分:61)

不应再使用班级GooglePlayServicesUtil了!

以下是如何使用课程GoogleApiAvailability - 例如,当需要GCM(或任何其他Google服务)时:

public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        startRegistrationService();
    }
}

private void startRegistrationService() {
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int code = api.isGooglePlayServicesAvailable(this);
    if (code == ConnectionResult.SUCCESS) {
        onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
    } else if (api.isUserResolvableError(code) &&
        api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) {
        // wait for onActivityResult call (see below)
    } else {
        Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_GOOGLE_PLAY_SERVICES:
            if (resultCode == Activity.RESULT_OK) {
                Intent i = new Intent(this, RegistrationService.class); 
                startService(i); // OK, init GCM
            }
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

<强>更新

REQUEST_GOOGLE_PLAY_SERVICES是一个具有任意名称和值的整数常量,可以在onActivityResult()方法中引用。

另外,在上面的代码中调用this.onActivityResult()是可以的(你也可以在另一个地方调用super.onActivityResult())。

答案 2 :(得分:8)

您必须使用 GoogleApiAvailability

GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this);

this代表context

答案 3 :(得分:8)

  

检查设备以确保其具有Google Play服务APK。如果   它没有,显示一个允许用户下载APK的对话框   来自Google Play商店或在设备的系统中启用它   设置。

public static boolean checkPlayServices(Activity activity) {
    final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                    .show();
        } else {
            Logger.logE(TAG, "This device is not supported.");
        }
        return false;
    }
    return true;
}

答案 4 :(得分:0)

我在BaseActivity类中添加了这个有趣的地方,以便在所有地方使用

    fun checkGooglePlayServices(okAction : ()-> Unit , errorAction: (msg:String, isResolved:Boolean)-> Unit){
    val apiAvailability = GoogleApiAvailability.getInstance()
    val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            apiAvailability.getErrorDialog(
                this,
                resultCode,
                PLAY_SERVICES_RESOLUTION_REQUEST
            ).show()
             // dialoe when click on ok should let user go to install/update play serices


            errorAction("dialog is shown" , true)

        } else {
          "checkGooglePlayServices  This device is not supported.".log(mTag)
            errorAction("This device is not supported",false)
        }
    }else{
        okAction()
    }
}

companion object {
    const val PLAY_SERVICES_RESOLUTION_REQUEST = 1425
}

像这样使用它

    (activity as? BaseActivity)?.checkGooglePlayServices({
        // ok so start map
        initializeMap()
    },
        { msg, isResolved ->
            if (!isResolved)
                context?.show(msg)

        }
    )

或者您可以根据需要自定义它。

答案 5 :(得分:0)

只需在两个方法之间放置一个.getInstance()就可以了。