谷歌的问题加上登录android

时间:2015-07-02 16:15:14

标签: android google-plus google-play-services google-plus-signin google-signin

我已添加Google plus登录到我的应用。当我点击使用谷歌按钮登录时,该应用程序冻结,并停止响应。当我杀死应用程序并重新启动它时,登录发生没有任何问题。出了什么问题? 代码如下。 布局

            <com.google.android.gms.common.SignInButton
                android:id="@+id/sign_in_button"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:elevation="10dp"/>

,Java代码是

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launcher);
         mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
    mSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mGoogleApiClient.isConnecting()) {
                mSignInClicked = true;
                resolveSignInError();
            }
        }
    });
    mSignInButton.setSize(SignInButton.SIZE_WIDE);
}
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            startIntentSenderForResult(mConnectionResult.getResolution()
                    .getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            // The intent was canceled before it was sent. Return to the
            // default
            // state and attempt to connect to get an updated
            // ConnectionResult.
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

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

protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (!mIntentInProgress) {
        // Store the ConnectionResult so that we can use it later when the
        // user clicks
        // 'sign-in'.
        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) {
    Log.e("Connection","success");   
}

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub

}



protected void onActivityResult(int requestCode, int responseCode,
                                Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

logcat显示结果代码为-1,而onActivityResult中的intent为null。并再次调用mGoogleApi.connect()。该应用程序只是在该阶段冻结,并且不会触发回调。我究竟做错了什么?

2 个答案:

答案 0 :(得分:1)

我喜欢这样使用

private boolean mSignInClicked;
private GoogleApiClient mGoogleApiClient;

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this).addApi(Plus.API) 
    .addScope(Plus.SCOPE_PLUS_LOGIN).build();

在onCreate()...

中写上面的代码

gplus按钮的onClick方法

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

/**
 * Method to resolve any signin errors
 * */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (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) {
    // TODO Auto-generated method stub

    mSignInClicked = false;

    // Get user's information
    getProfileInformation();

}

@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();
}


private void getProfileInformation() {
    try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String reg_id = currentPerson.getId();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

            Log.e("Google plus responce", "Name: " + personName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + personPhotoUrl+"REG ID " +reg_id );

            personPhotoUrl = personPhotoUrl.substring(0,
                    personPhotoUrl.length() - 2)
                    + PROFILE_PIC_SIZE;
}

on ontivity for result

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("FbLogin", "Result Code is = " + resultCode +"");
    if (requestCode == RC_SIGN_IN) {
        if (resultCode != RESULT_OK) {
            mSignInClicked = false;
        }
        mIntentInProgress = false;
        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

自定义按钮

<ImageView
        android:id="@+id/btn_gplus"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_weight="1"
        android:background="@drawable/btn_cha_gmail" />

希望这对你有用.... Happeee Programming !!!!!!

答案 1 :(得分:0)

我也有同样的问题。我onActivityResult中存在一些逻辑错误。 检查您的onActivityResult方法。那么只有你的问题才能解决。