我的目标是开发一款可以与朋友一起测验音乐的游戏。您可以通过注册此Google+社区自行测试: https://plus.google.com/u/0/communities/100815426127955231967
我正在几台设备上测试我的应用程序:
我在所有设备上使用相同的Google用户。我的应用已注册,我在清单文件中包含了正确的行。
使用完全相同的代码,我可以成功连接到Nexus 7和Nexus 5棒棒糖设备上的Google Plus和游戏服务,但不能在Xoom android 4设备上连接。
以下是代码,连接请求位于Activity onCreate:
中@Override
protected void onCreate(Bundle savedInstanceState) {
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
if (mGoogleApiClient == null) {
Plus.PlusOptions plusOptions = new Plus.PlusOptions.Builder().addActivityTypes(
"http://schemas.google.com/AddActivity", "http://schemas.google.com/ReviewActivity").build();
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Games.API).addScope(Games.SCOPE_GAMES)
.addApi(Plus.API, plusOptions).addScope(Plus.SCOPE_PLUS_LOGIN)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
}
mGoogleApiClient.connect();
} else {
Toast.makeText(this, R.string.texteErreurGPlus, Toast.LENGTH_LONG).show();
}
}
对于Android 4设备,将使用以下消息调用onConnectionFailed回调:
GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{412f07c8: android.os.BinderProxy@413b0bd0}} :
@Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingConnectionFailure) {
// Already resolving
return;
} else if (result.hasResolution()) {
try {
mResolvingConnectionFailure = true;
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
showErrorDialog(result.getErrorCode());
mResolvingConnectionFailure = true;
}
}
这导致活动onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_RESOLUTION:
mResolvingConnectionFailure = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() &&
!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
} else {
mGoogleApiClient.connect();
}
break;
和无限循环,因为resultCode不等于RESULT_OK。
以下是Android Studio的gradle文件:
apply plugin: 'android'
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
applicationId "debut.blindtest"
minSdkVersion 16
targetSdkVersion 21
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':volley')
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.google.android.gms:play-services-plus:6.5.+'
compile 'com.google.android.gms:play-services-games:6.5.+'
compile files('libs/deezer-sdk-0.10.16.jar')
}
执行流程为:
例外:
01-07 22:38:01.260 4894-4894/debut.blindtest I/TagApplicationBlindTest﹕ onActivityResult : REQUEST_CODE_RESOLUTION : resultCode : 0
01-07 22:38:01.280 4935-4936/? D/dalvikvm﹕ GC_CONCURRENT freed 448K, 7% free 6800K/7303K, paused 24ms+3ms, total 55ms
01-07 22:38:01.280 4935-4935/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 22ms
01-07 22:38:01.280 4894-4894/debut.blindtest E/BaseGamesUtils﹕ No standard error dialog available. Making fallback dialog.
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/app/SharedElementCallback;)
01-07 22:38:01.380 4864-4864/? I/dalvikvm﹕ Could not find method android.app.Activity.setEnterSharedElementCallback, referenced from method android.support.v4.app.q.setEnterSharedElementCallback
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to resolve virtual method 80: Landroid/app/Activity;.setEnterSharedElementCallback (Landroid/app/SharedElementCallback;)V
01-07 22:38:01.380 4864-4864/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/app/SharedElementCallback;)
01-07 22:38:01.380 4864-4864/? I/dalvikvm﹕ Could not find method android.app.Activity.setExitSharedElementCallback, referenced from method android.support.v4.app.q.setExitSharedElementCallback
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to resolve virtual method 81: Landroid/app/Activity;.setExitSharedElementCallback (Landroid/app/SharedElementCallback;)V
01-07 22:38:01.380 4864-4864/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
01-07 22:38:01.380 4864-4864/? I/dalvikvm﹕ Could not find method android.app.Activity.finishAfterTransition, referenced from method android.support.v4.app.q.supportFinishAfterTransition
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to resolve virtual method 46: Landroid/app/Activity;.finishAfterTransition ()V
01-07 22:38:01.380 4864-4864/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006
01-07 22:38:01.380 4864-4864/? I/dalvikvm﹕ Could not find method android.app.Activity.postponeEnterTransition, referenced from method android.support.v4.app.q.supportPostponeEnterTransition
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to resolve virtual method 77: Landroid/app/Activity;.postponeEnterTransition ()V
01-07 22:38:01.380 4864-4864/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006
01-07 22:38:01.380 4864-4864/? I/dalvikvm﹕ Could not find method android.app.Activity.startPostponedEnterTransition, referenced from method android.support.v4.app.q.supportStartPostponedEnterTransition
01-07 22:38:01.380 4864-4864/? W/dalvikvm﹕ VFY: unable to resolve virtual method 89: Landroid/app/Activity;.startPostponedEnterTransition ()V
01-07 22:38:01.380 4864-4864/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006
01-07 22:38:01.410 4894-4898/debut.blindtest D/dalvikvm﹕ GC_CONCURRENT freed 325K, 6% free 7193K/7623K, paused 14ms+38ms, total 115ms
01-07 22:38:01.410 4935-4936/? D/dalvikvm﹕ GC_CONCURRENT freed 460K, 8% free 6851K/7367K, paused 19ms+12ms, total 83ms
01-07 22:38:01.410 4935-4935/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 41ms
01-07 22:38:01.450 4864-4864/? E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.util.e.a
01-07 22:38:01.450 4864-4864/? W/dalvikvm﹕ VFY: unable to resolve check-cast 35 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/util/e;
01-07 22:38:01.450 4864-4864/? D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x0010
01-07 22:38:01.460 4864-4864/? E/SignInActivity﹕ SignInActivity must be started with startActivityForResult
01-07 22:38:01.530 4935-4936/? D/dalvikvm﹕ GC_CONCURRENT freed 457K, 7% free 6916K/7431K, paused 18ms+5ms, total 61ms
你能帮助我发现我应该做些什么来与Android 4设备兼容吗?
答案 0 :(得分:1)
通过在onConnectionFailed()
中自动连接,您将会根据需要继续尝试连接多次,直到获得有效连接为止。这意味着即使用户不想登录(例如,他们在登录对话框中点击取消),也会不断地反复提示他们。
相反,Google+ Sign In guide使用mSignInClicked
变量来帮助确定onConnectionFailed()
是否应该立即重新连接(未连接的人的理想情况是onConnectionFailed()
被调用但是您尚未拨打startResolutionForResult()
- 只需保存ConnectionResult
,直到用户登录为止。
如果您想要保留初始自动登录尝试,可以考虑Google Play Games Services samples中使用的方法,例如TrivialQuest,onConnnectionFailed()
方法如下所示:
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed() called, result: " + connectionResult);
if (mResolvingConnectionFailure) {
Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving.");
return;
}
if (mSignInClicked || mAutoStartSignInFlow) {
mAutoStartSignInFlow = false;
mSignInClicked = false;
mResolvingConnectionFailure = BaseGameUtils.resolveConnectionFailure(this,
mGoogleApiClient, connectionResult, RC_SIGN_IN,
getString(R.string.signin_other_error));
}
showSignInBar();
}
他们使用BaseGameUtils辅助方法更轻松地连接到Google服务(注意:没有特别的游戏服务相关 - 它与所有GoogleApiClient
个连接同样有效。)< / p>
答案 1 :(得分:1)
我删除了行&#34; android:launchMode =&#34; singleInstance&#34;&#34;在我的清单文件中。经过几次构建/运行循环后,它开始工作。 我只需要重新设计我的应用程序来改变我的活动。
答案 2 :(得分:0)
确保您拥有权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />