GooglePlayServicesUtil与GoogleApi可用性


102

我正在尝试在我的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时,出现错误消息:

在此处输入图片说明


在哪里可以找到GoogleApiAvailability?我找不到
mcmillab '16

@mcmillab +1。我从8.1.0升级到8.4.0,然后GooglePlayServicesUtil消失了(对于“次要”更新来说这似乎是一种不好的做法),但是我没有看到它GoogleApiAvailability可以代替它。
spaaarky21 2016年

当更新至Firebase结帐时,此操作:etivy.com/…–戴维德罗兹
Dawid Drozd)

Answers:


203

我找到了解决方案。在中GoogleApiAvailability,所有方法都是公共方法,而在GooglePlayServicesUtil所有方法中都是静态公共函数。

因此,要使用GoogleApiAvailability,正确的方法是:

private boolean checkPlayServices() {
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    int result = googleAPI.isGooglePlayServicesAvailable(this);
    if(result != ConnectionResult.SUCCESS) {
        if(googleAPI.isUserResolvableError(result)) {
            googleAPI.getErrorDialog(this, result,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        }

        return false;
    }

    return true;
}

9
什么是:PLAY_SERVICES_RESOLUTION_REQUEST
萨满萨塔

12
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
Ferrrmolina

7
这是一个任意整数。您可以弥补价值。
Timmmm,

4
有些人喜欢在9000设置的值的东西
matthias_b_nz

12
整个Google Play服务库设计都是一团糟。这都是有缺陷的,加上缺乏文档,很难遵循。
mr5

64

不应再使用类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
您能否指出声称“不再使用类GooglePlayServicesUtil!”的消息来源。Google Play服务API令人困惑。
Lachezar

8
Google告诉您的方法是:在Javadoc的顶部,将GooglePlayServicesUtil中所有标记为已弃用的方法以及建议GOOGLE_PLAY_SERVICES_PACKAGEGoogleApiAvailability类中获取常量的方法称为 Google:不再使用GooglePlayServicesUtil该类。
亚历山大·法伯

3
如果设备使用的旧版Google Play服务GoogleApiAvailability不存在该类怎么办?如果我们甚至在条件表达式中静态引用该类,它是否会使应用程序崩溃?
凯文·克鲁姆维德

6
@Kevin Krumwiede GoogleApiAvailability是客户端库的一部分。因此它的代码已编译到应用程序中=>不用担心。
WindRider

9
您不应调用onActivityResult()。当另一个活动返回结果时,应从外部调用它。
Yar 2016年

10

您必须改为使用GoogleApiAvailability

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

this代表context


9

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

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;
}

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)

        }
    )

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.