【发布时间】:2016-09-25 09:03:31
【问题描述】:
目前,我的应用正在使用
- minSdkVersion 14
- targetSdkVersion 23
- 编译'com.google.android.gms:play-services-gcm:8.4.0'
- 编译 'com.google.android.gms:play-services-base:8.4.0'
- 编译 'com.google.android.gms:play-services-analytics:8.4.0'
- 编译 'com.google.android.gms:play-services-ads:8.4.0'
- 编译 'com.google.android.gms:play-services-drive:8.4.0'
我正在使用以下代码执行GoogleApiClient 连接。
public class GoogleApiClientFragment extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public interface ConnectionCallbacks {
void onConnected(GoogleApiClient googleApiClient, int action);
void onCancel(int action);
}
public static GoogleApiClientFragment newInstance(String accountName, int action) {
GoogleApiClientFragment googleApiClientFragment = new GoogleApiClientFragment();
Bundle bundle = new Bundle();
bundle.putString(INTENT_EXTRA_ACCOUNT_NAME, accountName);
bundle.putInt(INTENT_EXTRA_ACTION, action);
googleApiClientFragment.setArguments(bundle);
return googleApiClientFragment;
}
/**
* Handles resolution callbacks.
*/
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode.REQUEST_GOOGLE_API_CLIENT_CONNECT) {
if (resultCode == Activity.RESULT_OK) {
mGoogleApiClient.connect();
} else {
Activity activity = this.getActivity();
if (activity instanceof ConnectionCallbacks) {
ConnectionCallbacks connectionCallbacks = (ConnectionCallbacks)activity;
connectionCallbacks.onCancel(action);
}
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Bundle bundle = this.getArguments();
this.accountName = bundle.getString(INTENT_EXTRA_ACCOUNT_NAME);
this.action = bundle.getInt(INTENT_EXTRA_ACTION);
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())
.setAccountName(accountName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "GoogleApiClient connected");
Activity activity = this.getActivity();
if (activity instanceof ConnectionCallbacks) {
ConnectionCallbacks connectionCallbacks = (ConnectionCallbacks)activity;
connectionCallbacks.onConnected(mGoogleApiClient, action);
}
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString());
if (!connectionResult.hasResolution()) {
Utils.showLongToast("debug two " + connectionResult.toString());
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this.getActivity(), connectionResult.getErrorCode(), 0).show();
return;
}
try {
connectionResult.startResolutionForResult(this.getActivity(), RequestCode.REQUEST_GOOGLE_API_CLIENT_CONNECT);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
private String accountName = null;
private int action = -1;
private GoogleApiClient mGoogleApiClient;
public static final int ACTION_LOAD_FROM_CLOUD = 0;
public static final int ACTION_SAVE_TO_CLOUD = 1;
private static final String INTENT_EXTRA_ACCOUNT_NAME = "INTENT_EXTRA_ACCOUNT_NAME";
private static final String INTENT_EXTRA_ACTION = "INTENT_EXTRA_ACTION";
private static final String TAG = "GoogleApiClientFragment";
}
某些用户(并非所有用户)遇到以下问题。
在使用 Google Play 服务时遇到问题。如果问题 仍然存在,请联系开发者寻求帮助。
错误对话框是由于onConnectionFailed 中的GoogleApiAvailability.getInstance().getErrorDialog 代码造成的。
无论何时
mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())
.setAccountName(accountName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
被执行,它总是命中
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
当我使用 toString 打印出 connectionResult 时,它给出了
ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null, message=null}
我的一个用户具有以下规范。
- 索尼 Xperia z5
- Android 6
- Google Play 服务 9.0.83
- 他的设备中只有 1 个 Google 帐户
我什至尝试使用 Google Play Services 9.0.1 重新编译 APK。但是,同样的问题仍然存在。
关于如何解决问题的任何想法?
【问题讨论】:
-
关于最新版本的 Google Play 服务,我认为现在存在错误问题。如果可以帮助您,请尝试检查此SO question。
-
@KENdi 似乎不是同一个问题。不过还是谢谢。
-
您是否在遇到错误后尝试重新连接,因为我在文档中发现它说尝试重新连接后可能会正常。正如文档所指developers.google.com/android/reference/com/google/android/gms/…
-
你是在请求安卓 6 的权限吗?在这种情况下,应用程序不会崩溃,但 connectionResult 将失败。
-
@JpCrow,我已经检查过了,这不是问题。您可以通过自己安装应用程序“JStock”来检查它。
标签: android google-play-services google-signin