GoogleApiClient无法连接

时间:2016-11-28 07:22:16

标签: android google-api-client

我选择一个帐户登录谷歌api客户端后按下菜单键,然后我点击此应用程序到前面,googleapiclient不再连接。当然我在片段的onstart()中调用了connect()方法,在onStop()中调用了disconnect()。   如果帐户已成功登录,现在登录时我不需要选择帐户,按下菜单键并单击它,应用程序将成功登录!

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(Settings.VAL_GOOGLE_REQ_ID_TOKEN).requestEmail().build();
        mGoogleApiClient = new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

1 个答案:

答案 0 :(得分:0)

您是否在片段或活动中使用Google Api客户端 如果要使用enableAutoManage,则必须使您的活动扩展FragmentActivity。它所做的回调是自动管理GoogleApiClient所必需的。因此,最简单的解决方案是将FragmentActivity扩展到您的活动中。然后你的演员阵容不会失败并导致应用程序在运行时崩溃。

备用解决方案是自己管理api客户端。您将从构建器中删除enableAutoManage行,并确保自己连接/断开客户端。最常见的地方是onStart()/ onStop()。有点像...

@覆盖

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
        .addApi(Places.GEO_DATA_API)
        .addConnectionCallbacks(this).build();

}

@覆盖 protected void onStart(){

super.onStart();
mGoogleApiClient.connect();

}

@覆盖 protected void onStop(){

super.onStop();
mGoogleApiClient.disconnect();

}