如何在Android 6+中解决Google Play服务错误?

时间:2017-03-22 06:18:53

标签: android google-play-services

当我使用Firebase时,我需要检查我是否在应用启动时拥有受支持的Google Play服务版本。 GooglePlayServicesUtil已弃用。所以,我使用了新的闪亮的GoogleApiAvailability api,我可以检测是否需要升级。

但是当我使用GoogleApiAvailability.getErrorDialog时却没有启动所需的意图。它没有做任何事情。互联网的拖网没有透露任何有用的代码示例。理想情况下,我想编写自己的自定义对话框并启动pendingIntent,但这也是难以捉摸的。我的代码:

    final GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    final int resultCode = googleAPI.isGooglePlayServicesAvailable(this);
    if (resultCode == ConnectionResult.SUCCESS) {
        if (isOnline()) {
            app.init(this, this);
        }
    } else {
            switch (resultCode) {
                case SERVICE_VERSION_UPDATE_REQUIRED:
                    Dialog updateDialog = googleAPI.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST, mOnCancelFinishApp);
                    updateDialog.setOnDismissListener(mOnDismissFinishApp);
                    updateDialog.setCanceledOnTouchOutside(false);
                    updateDialog.show();
                    ...

更新:我的gradle具有这些依赖关系

.....
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile('com.microsoft.aad:adal:2.0.1-alpha') {
        exclude group: 'com.android.support'
    }
    compile 'com.google.android.gms:play-services-location:10.0.0'
    compile('com.crashlytics.sdk.android:answers:1.3.12@aar') {
        transitive = true;
    }
    compile('com.crashlytics.sdk.android:crashlytics:2.6.7@aar') {
        transitive = true;
    }
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support:support-v4:25.0.0'
    compile 'com.google.android.gms:play-services-maps:10.0.0'
    compile 'com.google.android.gms:play-services-location:10.0.0'
    compile 'com.android.support:cardview-v7:25.0.0'
    compile 'com.google.android.gms:play-services-analytics:10.0.0'
    compile 'com.google.firebase:firebase-core:10.0.0'
    provided 'org.glassfish:javax.annotation:10.0-b28'
}
apply plugin: 'com.google.gms.google-services'

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        // These docs use an open ended version so that our plugin
        // can be updated quickly in response to Android tooling updates

        // We recommend changing it to the latest version from our changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }

2 个答案:

答案 0 :(得分:0)

请检查GooglePlayServices是否可用

Dialog errorDialog;

private boolean checkPlayServices() {

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();

        int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this);

        if (resultCode != ConnectionResult.SUCCESS) {
            if (googleApiAvailability.isUserResolvableError(resultCode)) {

                if (errorDialog == null) {
                    errorDialog = googleApiAvailability.getErrorDialog(this, resultCode, 2404);
                    errorDialog.setCancelable(false);
                }

                if (!errorDialog.isShowing())
                    errorDialog.show();

            }
        }

        return resultCode == ConnectionResult.SUCCESS;
    }

答案 1 :(得分:0)

请尝试这些更改:

....
apply plugin: 'io.fabric'

repositories {
maven { url 'https://maven.fabric.io/public' }


buildscript {
repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    // These docs use an open ended version so that our plugin
    // can be updated quickly in response to Android tooling updates

    // We recommend changing it to the latest version from our changelog:
    // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
    classpath 'io.fabric.tools:gradle:1.+'
 }
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')

compile('com.microsoft.aad:adal:2.0.1-alpha') {
    exclude group: 'com.android.support'
}
compile 'com.google.android.gms:play-services-location:10.0.0'
compile('com.crashlytics.sdk.android:answers:1.3.12@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.6.7@aar') {
    transitive = true;
}
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:support-v4:25.0.0'
compile 'com.google.android.gms:play-services-maps:10.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.google.android.gms:play-services-analytics:10.0.0'
compile 'com.google.firebase:firebase-core:10.0.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
}

apply plugin: 'com.google.gms.google-services'