且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google plus登录,登录时反复出现Android唱歌提示

更新时间:2023-12-01 16:01:16

protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }


onCreate(Bundle bundle){
mGoogleApiClient = new GoogleApiClient.Builder(YourActivity.this)
                .addConnectionCallbacks(YourActivity.this)
                .addOnConnectionFailedListener(YourActivity.this).addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN).build();
}



btn_gpluslogin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        signInWithGplus();
                    }
                });

private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }
    }

private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (IntentSender.SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
            return;
        }

        if (!mIntentInProgress) {
            // Store the ConnectionResult for later usage
            mConnectionResult = result;

            if (mSignInClicked) {
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError();
            }
        }

    }

@Override
    public void onConnected(Bundle arg0) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

        // Get user's information
        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
        boolean isSignedUp = pref.getBoolean("isSignedUp", false);
        if (!isSignedUp)
            getGProfileInformation();

        // Update the UI after signin
    }

private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            getGProfileInformation();
        } else {
            btn_gpluslogin.setVisibility(View.VISIBLE);
        }
    }

    /**
     * Fetching user's information name, email, profile pic
     */
    private void getGProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                Log.d("Google Info", currentPerson.toString());
                String personName = currentPerson.getDisplayName();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

            } else {
                Toast.makeText(getApplicationContext(),
                        "Person information is null", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }