检查 Google Play Service 是否可用: “不幸的是,应用程序已停止工作”

每次在手机上运行应用程序都会崩溃。有什么问题吗?不幸的是“ Appname”已经停止工作了。我还尝试了其他方法来检查 Googleplay 服务,但它总是崩溃。我更新了我的谷歌播放服务,并有一个工作谷歌地图 v2完美工作。对这个代码有什么解决方案吗?它在运行安卓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;
}
}
75459 次浏览

改变

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

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();

谢谢你们的回复,我刚从我的 LogCat 上看出来的。 我不得不把这个包含在我的 Android 清单中。

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

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

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

你可以这样检查播放服务:

/**
* 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参数。

要检查 GooglePlayServices是否可用,请使用 GoogleApiAvailability.isGooglePlayServicesAvailable(),作为 GooglePlayServicesUtil.isGooglePlayServicesAvailable() 不赞成

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

您在新的2016谷歌服务的谷歌样本中在 read doc 中设置了 change checkPlayServices

enter image description here

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 Services,您的应用程序根本不会启动。

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

据我所知,这是在启动应用程序时检查 Google Play 服务所需的最小代码,比如在活动界面中。与达维尔的答案相似,但没有不必要的代码。

您可以通过简单地将自定义结果代码传递给 getErrorDialog方法来验证它在所有情况下都是有效的。所有的结果代码都列出了 给你。使用 onResume 非常重要,因为它是在更新 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
}
    

}