我想捕获onDisconnected回调,但是onConnectionSuspended和onConnectionFailed都没有被调用。
public class mAct extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
//i'm using this:
protected void createClient() {
if (gso == null) {
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.requestScopes(Drive.SCOPE_APPFOLDER)// required for App Folder sample
.requestEmail()
.requestIdToken("MYKEY.apps.googleusercontent.com")
.build();
}
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addApi(Drive.API)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
//this function calls `onConnected`
protected void connect() {
if (mGoogleApiClient != null) {
if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}
}
}
//but this is not
protected void disconnect() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
}
//to signout I use this
protected void signoutWorker() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
//some setup and exit
disconnect();
}
});
//this works
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "onConnected()");
}
//this not works
@Override
public void onConnectionSuspended(int cause) {
}
//this not works too
Override
public void onConnectionFailed(ConnectionResult result) {
}
}据我所知,这个新的应用程序接口阻止了在调用mGoogleApiClient.disconnect时调用onConnectionFailed。我可以在disconnect()函数中使用回调吗?这是正常行为吗?
这就是:
protected void disconnect() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
myCallbackhere();//this callback
}
}发布于 2017-10-15 20:39:27
这两个方法都不是disconnect的回调方法。
@Override
public void onConnectionSuspended(int cause) {
}当你的应用程序与谷歌Play服务包(不一定是互联网)断开连接时,会调用
onConnectionSuspended。
@Override
public void onConnectionFailed(ConnectionResult result) {
}当方法失败时,会调用
和onConnectionFailed,如DriveApi.newDriveContents等。
它支持检查错误或显示errorDialog,或在有解决方案时重试
不幸的是,disconnect没有回调
但是googleDrive注销方法不在网络范围内。所以你不用担心在不知情的情况下注销
只需在disconnect()之后调用您的方法
尝试断开连接(),在没有wifi、数据的情况下进行连接
此googleTopic可能会对您有所帮助
https://stackoverflow.com/questions/35200209
复制相似问题