检查是否有正确的Google Play服务:“不幸的是,应用程序已停止工作”

应用程序崩溃,每次我在我的手机上运行它。 有什么不对? 它说不幸的是“appname”已经停止工作。 我也尝试其他方法来检查谷歌播放服务,但它总是崩溃。 我更新了我的谷歌播放服务,并有一个工作谷歌地图V2完美的工作。 这个代码的任何解决scheme? 它在运行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; } } 

Solutions Collecting From Web of "检查是否有正确的Google Play服务:“不幸的是,应用程序已停止工作”"

要检查GooglePlayServices可用,请使用GoogleApiAvailabilityisGooglePlayServicesAvailable() ,作为GooglePlayServicesUtilisGooglePlayServicesAvailable() 已弃用

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

更新:检查谷歌播放服务是否可用,如果谷歌播放服务不可用,错误是可恢复的,然后打开一个对话框来解决一个错误。

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

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

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

更改

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

 int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext(); 

谢谢你们的回应。 我只是从我的LogCat中弄明白了。 我必须将其包含在我的Android Manifest中。

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

您在阅读文档中设置更改checkPlayServices在新的2016年在这里input链接描述

在这里输入图像说明

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

将此方法添加到您的Splash Screen; 如果您没有更新或安装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(); } } 

您可以像这样检查Play Services的存在:

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

你可以传递-1来获得versionCode参数。