GoogleApiClient的多个实例

时间:2016-01-03 09:09:17

标签: android google-plus google-fit

上下文

当我的应用首次启动时,它会要求用户连接到 Google Fit ,在下一步(另一项活动)中,他可以连接到 Google加

当我接受连接到这两个API时,一旦进入我的应用程序,我就可以断开 Fit Plus ,但是,我不知道为什么,当连接 Plus 客户端时,无法与 Fit 断开连接,如果我与 Plus 断开连接,则效果很好,但 > Fit 客户端也会断开连接。

当应用程序启动时,如果我决定只连接到 Fit 而不是 Plus ,它会按预期工作,我的意思是我可以正确地从断开连接配合

问题

我认为可能有GoogleApiClient的多个实例,但我的印象是两个客户都是“已连接”。

API客户端之间是否存在一种层次结构?

修改

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    /**
     * Create the fitness client - this is mandatory to use the app
     */
    buildFitnessClient();

    /**
     * Get the result of Google Plus connection
     */
    // If skip button is clicked, the user does not want to connect to G+
    Boolean isSkipClicked = getSharedPreferences("ISSKIPCLICKED", MODE_PRIVATE).getBoolean("isSkipClicked", false);
    if(isSkipClicked){
        // The user has the possibility to connect to G+ through the menu
    }
    // else, the user is connected to G+, rebuilt the client
    else{
        buildPlusClient();
        mPlusClient.connect();
    }
}

buildFitnessClient()

private void buildFitnessClient() {
    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.RECORDING_API)
            .addApi(Fitness.CONFIG_API)
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
            .addScope(new Scope((Scopes.FITNESS_NUTRITION_READ_WRITE)))
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                                        @Override
                                        public void onConnected(Bundle bundle) {
                                            Log.i(TAG, "Connected to Fitness API!!!");
                                            // Now you can make calls to the Fitness APIs.
                                            // Put application specific code here.

                                            mClient.connect();
                                        }

                                        @Override
                                        public void onConnectionSuspended(int i) {
                                            // If your connection to the sensor gets lost at some point,
                                            // you'll be able to determine the reason and react to it here.
                                            if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                                Log.i(TAG, "Connection lost.  Cause: Network Lost.");
                                            } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                                Log.i(TAG, "Connection lost.  Reason: Service Disconnected");
                                            }
                                        }
                                    }
            )
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                                               // Called whenever the API client fails to connect.
                                               @Override
                                               public void onConnectionFailed(ConnectionResult result) {
                                                   Log.i(TAG, "Connection failed. Cause: " + result.toString());
                                                   if (!result.hasResolution()) {
                                                       // Show the localized error dialog
                                                       GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
                                                               Main2Activity.this, 0).show();
                                                       return;
                                                   }
                                                   // The failure has a resolution. Resolve it.
                                                   // Called typically when the app is not yet authorized, and an
                                                   // authorization dialog is displayed to the user.
                                                   if (!authInProgress) {
                                                       try {
                                                           Log.i(TAG, "Attempting to resolve failed connection");
                                                           authInProgress = true;
                                                           result.startResolutionForResult(Main2Activity.this, REQUEST_OAUTH);
                                                       } catch (IntentSender.SendIntentException e) {
                                                           Log.e(TAG, "Exception while starting resolution activity", e);
                                                       }
                                                   }
                                               }
                                           }
            )
            .build();
}

buildPlusClient 方法:

public void buildPlusClient(){
    /**
     * Handle the connection to Google Plus client
     */

    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile()
            .build();

    GoogleApiClient.OnConnectionFailedListener unresolvedConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.d(TAG, "onConnectionFailed:" + connectionResult);
        }
    };

    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    mPlusClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, unresolvedConnectionFailedListener /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

下面,与Gplus和/或Gfit断开连接的方法:

private void signOutFit(){
            if(mClient.isConnected()){
                Fitness.ConfigApi.disableFit(mClient)
                        .setResultCallback(new ResultCallback<Status>() {
                            @Override
                            public void onResult(Status status) {
                                if (status.isSuccess()) {
                                    //Log.d(TAG, "Disconnect success");
                                    Toast.makeText(Main2Activity.this,"Disconnected from Google Fit",Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(Main2Activity.this,"Impossible to disconnect from Fit",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
            }
}

private void signOutPlus(){
    if(mPlusClient.isConnected()){
        Auth.GoogleSignInApi.signOut(mPlusClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if(status.isSuccess()){
                            Toast.makeText(Main2Activity.this, "Disconnect from Google Plus", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(Main2Activity.this, "Impossible to disconnect from Google Plus", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }        
}

0 个答案:

没有答案