如何将GoogleapicLient连接到Google Play服务



我面临着创建Google API连接的问题,我在活动中创建了googleapiclient连接,但在fragment中是Connectiong。出现片段时,显示连接失败。

googleapiclient的代码是

  mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

上面的代码放置在片段中的oncreateview((方法中。

连接回电话为

   @Override
public void onConnected(@Nullable Bundle bundle) {
    Toast.makeText(getActivity(),"Connected Successfully",Toast.LENGTH_SHORT).show();
    Log.e("SUCCESSFULL", "Connection successful");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if(connectionResult.hasResolution())
    {
        try {
            connectionResult.startResolutionForResult(getActivity(),RESOLVE_CONNECTION_REQUEST_CODE);
            Toast.makeText(getActivity(),"Connected Failed",Toast.LENGTH_SHORT).show();
            Log.e("SUCCESSFULL", "Connection Failed.....");
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    }
    else {
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(),getActivity(),0);
    }
}

如何解决此问题并使Google API客户端连接在Fragment中。

您必须在onstart((方法中调用mgoogleapiclient.connect((。

@Override
    protected void onStart() {
        Log.d(TAG, "OnStart");
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "Onstop");
        super.onStop();
        mGoogleApiClient.disconnect();
    }

相关内容

  • 没有找到相关文章

最新更新