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

use*_*836 55 android

应用程序每次在手机上运行时都会崩溃.有什么不对?它说不幸的是"appname"已经停止工作了.我也尝试过其他方法来检查googleplay服务,但它总是崩溃.我更新了我的谷歌播放服务,并有一个正常工作的谷歌地图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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dha*_*tel 120

要检查是否GooglePlayServices可用,请使用GoogleApiAvailability.isGooglePlayServicesAvailable(),作为GooglePlayServicesUtil.已弃用.isGooglePlayServicesAvailable()

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

更新:检查谷歌播放服务是否可用,如果谷歌播放服务不可用且错误可恢复,则打开一个对话框以解决错误.

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 2404?在另一篇文章中,我发现了另一个号码.你能解释一下这个整数是什么吗? (3认同)
  • 它的活动请求代码,可以是任意数字.当onActivityResult方法在错误获取句柄时调用它将非常有用. (3认同)

Ren*_*tik 18

我不知道在哪里发布这个但是谷歌播放服务检查的正确工作流程的困难是令人兴奋的,我正在使用一些自定义类,但你可能得到一个点......

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;
}
Run Code Online (Sandbox Code Playgroud)


Ped*_*ela 10

将此方法添加到您的启动画面; 如果您没有更新或安装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;
    }
Run Code Online (Sandbox Code Playgroud)

并在简历上调用它

@Override
protected void onResume() {
    super.onResume();
    if (checkPlayServices()) {
        startApp();
    }
}
Run Code Online (Sandbox Code Playgroud)


Pon*_*ung 7

您在新的2016 Google google样本中为 read doc设置了更改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;
    }
Run Code Online (Sandbox Code Playgroud)


Sim*_*son 7

据我所知,这是在类似活动的启动屏幕中启动应用程序时检查 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) {
            availability.getErrorDialog(this, resultCode, 0).show()
            return false
        }
        return true
    }
    
}
Run Code Online (Sandbox Code Playgroud)


use*_*836 6

谢谢你的回复.我只是从我的LogCat中弄明白了.我必须在我的Android Manifest中包含它.

<application>
<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
...
Run Code Online (Sandbox Code Playgroud)

  • 我不认为这完全可以防止崩溃. (2认同)

小智 5

更改

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
Run Code Online (Sandbox Code Playgroud)

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();
Run Code Online (Sandbox Code Playgroud)