当我尝试通过Google Play服务进行连接时出现此错误。
我遵循本指南,不超过四小时前,https://developer.android.com/google/play-services/setup.html
有人可以帮助我吗?
更新:
在我的设备中更新Google Play服务后,我不再出现此错误,但仍无效。
这是我的代码:
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
GoogleApiClient mGoogleApiClient;
int REQUEST_ACHIEVEMENTS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.setAccountName("account name (i hid) ")
.build();
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
}
@Override
protected void onStart() {
Log.d("MYTAG", "onStart()");
super.onStart();
mGoogleApiClient.connect();
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
Log.d("MYTAG", "Result: " + result);
GooglePlayServicesUtil.getErrorString(result);
}
/* protected void onResume(){
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if( result == ConnectionResult.SUCCESS){
Log.d("MYTAG", "SUCCESS");
GooglePlayServicesUtil.getErrorString(result);
}else{
if( result == ConnectionResult.SERVICE_MISSING){
Log.d("MYTAG", "service missing");
}
if( result == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
Log.d("MYTAG", "Service version update required");
}
if( result == ConnectionResult.SERVICE_DISABLED){
Log.d("MYTAG", "Service disabled");
}
if( result == ConnectionResult.SERVICE_INVALID){
Log.d("MYTAG", "Service invalid");
}
}
}*/
protected void onStop() {
Log.d("MYTAG", "onStop()");
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.one:
Log.d("MYTAG", "ACHIEVEMENT BUTTON CLICKED");
//BaseGameUtils.showAlert(this, getString(R.string.you_won));
if (mGoogleApiClient.isConnected()) {
// unlock the first achievement.
Games.Achievements.unlock(mGoogleApiClient, "here there is my achivements id ( i hid) ");
}else{
Log.d("MYTAG", "NOT CONNECTED");
}
break;
case R.id.two:
startActivityForResult(Games.Achievements.getAchievementsIntent(mGoogleApiClient), REQUEST_ACHIEVEMENTS);
break;
case R.id.three:
mGoogleApiClient.connect();
break;
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
Log.d("MYTAG", arg0 + "");
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
}
log cat中没有更多错误,但如果我按下按钮2则会出现此错误:
01-04 23:49:41.725: E/AndroidRuntime(2211): java.lang.IllegalStateException: GoogleApiClient must be connected.
如果我按下按钮3,没有消息,没有错误。
但是结果 GooglePlayServicesUtil.isGooglePlayServicesAvailable(本); 等于0,(成功)。
答案 0 :(得分:1)
要验证Google Play服务版本,请致电isGooglePlayServicesAvailable()。如果结果代码为SUCCESS,那么Google Play服务APK是最新的,您可以继续建立连接。但是,如果结果代码为SERVICE_MISSING,SERVICE_VERSION_UPDATE_REQUIRED或SERVICE_DISABLED,则用户需要安装更新。因此,请调用GooglePlayServicesUtil.getErrorDialog()并将结果错误代码传递给它。这将返回您应该显示的对话框,该对话框提供有关错误的相应消息,并提供一个操作,使用户可以通过Google Play商店安装更新。
同样,如果您通过GoogleApiClient
进行连接,则使用onConnectionFailed()
中返回给您的结果调用startResolutionForResult()将创建相同的对话框并要求用户升级其Google Play服务。