Google Play游戏服务确实无法连接

时间:2017-07-09 19:19:00

标签: java android google-play-games

我在我的项目中添加了Google Play游戏,但它没有连接也没有显示任何错误。这里我的MainActivity扩展了Activity

private GoogleApiClient mGoogleApiClient;
private static final int REQUEST_RESOLVE_ERROR = 1001;
private boolean mResolvingError = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_menu);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();
}

@Override
public void onConnected(Bundle bundle){
}

@Override
public void onConnectionSuspended(int i){
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result){
    if(mResolvingError){
        return;
    } else if(result.hasResolution()){
        try{
            mResolvingError = true;
            result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (IntentSender.SendIntentException e){
            mGoogleApiClient.connect();
        }
    } else{
        GoogleApiAvailability.getInstance().showErrorNotification(this, result.getErrorCode());
        mResolvingError = true;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if(requestCode == REQUEST_RESOLVE_ERROR){
        mResolvingError = false;
        if(resultCode == RESULT_OK){
            if(!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()){
                mGoogleApiClient.connect();
            }
        }
    }
}

@Override
protected void onStart(){
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop(){
    if(mGoogleApiClient.isConnected()){
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

在Mainifest.xml中:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    ...
    ...
    ...
</application>

在我的gradle中:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-ads:11.0.2'
compile 'com.google.android.gms:play-services-games:11.0.2'
}

我在项目中实施Google Play游戏服务的做法是否错误?我真的不知道是什么错误。请帮忙

0 个答案:

没有答案