检查是否有正确的Google Play服务可用:“很遗憾,应用程序已停止运行”


70

每次我在手机上运行应用程序时都会崩溃。有什么不对?它说不幸的是“ appname”已经停止工作。我还尝试了其他方法来检查googleplay服务,但它总是崩溃。我更新了Google Play服务,并且可以正常运行的Google Map v2正常运行。此代码有解决方案吗?它在运行android 4.1.2的手机和AVD上崩溃。

package com.example.checkgoogleplayproject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class MainActivity extends Activity {

    @Override
    protected void onResume() {
        super.onResume();

        // Getting reference to TextView to show the status
        TextView tvStatus = (TextView)findViewById(R.id.tv_status);

        // Getting status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status==ConnectionResult.SUCCESS)
            tvStatus.setText("Google Play Services are available");
        else{
            tvStatus.setText("Google Play Services are not available");
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

2
每当询问崩溃时,都发布完整的堆栈跟踪。
Gabe Sechan 2014年

1
非常感谢。我只是从堆栈跟踪中找出来的。我必须将此包含在我的Android清单中。<application> <meta-data android:name =“ com.google.android.gms.version” android:value =“ @ integer / google_play_services_version” /> ...
user3144836 2014年

Answers:


137

要检查是否GooglePlayServices可用,请使用GoogleApiAvailabilityisGooglePlayServicesAvailable(),作为GooglePlayServicesUtil不推荐使用isGooglePlayServicesAvailable()

public boolean isGooglePlayServicesAvailable(Context context){
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
    return resultCode == ConnectionResult.SUCCESS;  
}

更新:检查是否可用google play服务,如果不可用google play服务且错误可恢复,则打开对话框以解决错误。

public boolean isGooglePlayServicesAvailable(Activity activity) {
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int status = googleApiAvailability.isGooglePlayServicesAvailable(activity);
    if(status != ConnectionResult.SUCCESS) {
        if(googleApiAvailability.isUserResolvableError(status)) {
              googleApiAvailability.getErrorDialog(activity, status, 2404).show();
        }
        return false;
    }
    return true;
}

3
2404?在另一篇文章中,我找到了另一个号码。你能解释一下那个整数是做什么用的吗?
Anis LOUNIS,2013年

4
它的活动请求代码,可以是任何数字。当错误获取句柄时调用onActivityResult方法时,它将很有用。
Dhaval Patel

没有图书馆?
user924

18

我不知道将其发布在何处,但正确的Google Play服务检查工作流程中的困难令人难以置信,我正在使用一些自定义类,但您可能会得到一点...

protected boolean checkPlayServices() {
    final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity());
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity(),
                    PLAY_SERVICES_RESOLUTION_REQUEST);
            if (dialog != null) {
                dialog.show();
                dialog.setOnDismissListener(new OnDismissListener() {
                    public void onDismiss(DialogInterface dialog) {
                        if (ConnectionResult.SERVICE_INVALID == resultCode) activity().finish();
                    }
                });
                return false;
            }
        }
        new CSAlertDialog(this).show("Google Play Services Error",
                "This device is not supported for required Goole Play Services", "OK", new Call() {
                    public void onCall(Object value) {
                        activity().finish();
                    }
                });
        return false;
    }
    return true;
}

11

将此方法添加到您的启动屏幕;如果您没有更新或安装Google Play服务,则您的应用将完全无法启动。

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

并仅在简历上调用

@Override
protected void onResume() {
    super.onResume();
    if (checkPlayServices()) {
        startApp();
    }
}

对LA版本的西班牙语PUES IR一埃斯特链接medium.com/@pedro_varela/...
佩德罗·瓦雷拉

8

更改

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();

此方法已被弃用。使用他的GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getApplicationContext())
Kidus Tekeste

8

您在Google服务的新2016年Google示例中的读取文档中设置了更改checkPlayServices

在此处输入图片说明

if (checkPlayServices()) {
            // Start IntentService to register this application with GCM.
            Intent intent = new Intent(this, RegistrationIntentService.class);
            startService(intent);
        }



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

6

谢谢你们的回应。我只是从LogCat中弄清楚了。我必须将此包含在我的Android清单中。

<application>
<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
...


2
我认为这不能完全避免崩溃。
IgorGanapolsky

1

您可以像这样检查Play服务的状态:

/**
 * Check if correct Play Services version is available on the device.
 *
 * @param context
 * @param versionCode
 * @return boolean
 */
public static boolean checkGooglePlayServiceAvailability(Context context, int versionCode) {

    // Query for the status of Google Play services on the device
    int statusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);

    if ((statusCode == ConnectionResult.SUCCESS)
            && (GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE >= versionCode)) {
        return true;
    } else {
        return false;
    }
}

你可以只通过-1versionCode参数。


1
isGooglePlayServiceAvailable在api级别23中已弃用
克里希纳,

1

据我所知,这是在启动屏幕(如活动)中启动应用程序时检查Google Play服务所需的最少代码。与Dhavel的答案类似,但没有不必要的代码。

您可以通过简单地将自定义结果代码传递给getErrorDialog方法来验证它在所有情况下均有效。所有结果代码都在此处列出。使用onResume很重要,因为在更新Google Play中的Google Play服务等之后返回到应用程序后,它将被调用。

class MainActivity : AppCompatActivity() {

    override fun onResume() {
        super.onResume()
        if (checkGooglePlayServices()) {
            startActivity(Intent(this, HomeActivity::class.java))
        }
    }

    private fun checkGooglePlayServices(): Boolean {
        val availability = GoogleApiAvailability.getInstance()
        val resultCode = availability.isGooglePlayServicesAvailable(this)
        if (resultCode != ConnectionResult.SUCCESS) {
            val dialog = availability.getErrorDialog(this, resultCode, 0)
            dialog.show()
            return false
        }
        return true
    }

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