Google+连接对话框出现两次

时间:2013-08-25 21:48:34

标签: android authentication google-plus

我仍在努力让Google+登录正常工作,并遇到一些基本的连接问题。显然,当我有时连接时,我得到两个"选择一个帐户"对话框。我做错了什么?

package com.example.mymy;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.PlusClient.OnAccessRevokedListener;
import com.google.android.gms.plus.model.people.Person;

public class MainActivity extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener {

    private static final int REQUEST_CODE_RESOLVE_ERR = 7;
    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;
    private String TAG = "GmailLogin";
    private TextView mTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        if(status != ConnectionResult.SUCCESS) return;

//      mPlusClient = new PlusClient.Builder(this, this, this)
//              .setVisibleActivities("http://schemas.google.com/AddActivity",
//                                    "http://schemas.google.com/BuyActivity").build();

        mPlusClient = new PlusClient.Builder(this, this, this).build();

        mConnectionProgressDialog = new ProgressDialog(this);
        mTV = (TextView) findViewById(R.id.hello);

        // sign in button
        SignInButton pBut = (SignInButton) findViewById(R.id.sign_in_button);
        pBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!mPlusClient.isConnected()) mPlusClient.connect();

                if (mConnectionResult == null) {
                    mConnectionProgressDialog.show();
                } else {
                    try {
                        mConnectionResult.startResolutionForResult(MainActivity.this, REQUEST_CODE_RESOLVE_ERR);
                    } catch (SendIntentException e) {
                        // Try connecting again.
                        mConnectionResult = null;
                        if (!mPlusClient.isConnected()) mPlusClient.connect();
                    }
                }
            }
        });

        // revoke button
        Button revoke = (Button) findViewById(R.id.sign_revoke);
        revoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mPlusClient.isConnected()) {
                    return;
                }

                mConnectionProgressDialog.setMessage("Revoking access...");
                // Prior to disconnecting, run clearDefaultAccount().
                mPlusClient.clearDefaultAccount();
                mPlusClient.revokeAccessAndDisconnect(new OnAccessRevokedListener() {
                    public void onAccessRevoked(ConnectionResult status) {
                        mConnectionProgressDialog.dismiss();
                        // mPlusClient is now disconnected and access has been revoked.
                        // Trigger app logic to comply with the developer policies
                        mTV.setText("[revoke] onAccessRevoked ... revoke done.");
                    }
                });

            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.d(TAG, "[onConnectionFailed] result.hasResolution():"+ result.hasResolution());
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                if (!mPlusClient.isConnected()) mPlusClient.connect();
            }
        }
        // Save the result and resolve the connection failure upon a user click.
        mConnectionResult = result;
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        Log.d(TAG, "[onActivityResult] req:" + requestCode + "  resp:"+ responseCode);
        if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
            mConnectionResult = null;
            if (!mPlusClient.isConnected()) mPlusClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle a) {
        mConnectionProgressDialog.setMessage("Connected!");
        // http://stackoverflow.com/questions/1520887
        mConnectionProgressDialog.dismiss();
        String acctName = mPlusClient.getAccountName();
        Person person = mPlusClient.getCurrentPerson();
        int gender = 66;
        if (person.hasGender()) {
            gender = person.getGender();
        }
        //Toast.makeText(this, name + " is connected.", Toast.LENGTH_LONG).show();
        mTV.setText("[onConnected] acct:" + acctName + "...gender:" + gender);
    }

    @Override
    public void onDisconnected() {
        mTV.setText("[onDisconnected]");
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (mPlusClient.isConnected()) {
            mTV.setText("[onStart] already connected"); 
        } else {
            mConnectionProgressDialog.setMessage("Signing in...");
            mPlusClient.connect();
            mTV.setText("[onStart] connect");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mPlusClient.isConnected()) 
            mPlusClient.disconnect();
        mTV.setText("[onStop]");
    }

}

这是xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hello" />

    <Button
        android:id="@+id/sign_revoke"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sign_in_button" 
        android:text="Revoke Access"/>

</RelativeLayout>

0 个答案:

没有答案