Android:GooglePlus Button永远不会停止旋转

时间:2013-12-03 11:10:51

标签: android authentication google-plus

我是Android新手,我尝试将GooglePlus Button应用到我原生的Android应用中。 但每当我点击+1按钮时,我都会得到一个无尽的旋转圆圈。 (我已经设置了Google API设置)。

public class about extends Activity implements
    GooglePlayServicesClient.ConnectionCallbacks,  GooglePlayServicesClient.OnConnectionFailedListener {
// The request code must be 0 or greater.
    private static final int PLUS_ONE_REQUEST_CODE = 0;
    private static final String TAG = "google+";
    private static final String URL = "https://google.com/+Developer";
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private PlusOneButton mPlusOneButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.about);
    mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
    mPlusClient = new PlusClient.Builder(this, this, this)
            .clearScopes()
            .build();

    // Progress bar to be displayed if the connection failure is not resolved.
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");
}


@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mConnectionProgressDialog.isShowing()) {
        // The user clicked the sign-in button already. Start to resolve
        // connection errors. Wait until onConnected() to dismiss the
        // connection dialog.
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (IntentSender.SendIntentException e) {
                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) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mConnectionResult = null;
        mPlusClient.connect();
    }
}

@Override
protected void onResume() {
    super.onResume();
    // Refresh the state of the +1 button each time the activity receives focus.
    mPlusOneButton.initialize(mPlusClient, URL, PLUS_ONE_REQUEST_CODE);
}


@Override
public void onConnected(Bundle connectionHint) {
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
}

@Override
public void onDisconnected() {
     Log.d(TAG, "disconnected");
}

}

我很困惑。请帮助我。先谢谢你, 托拜厄斯

更正后的代码:

  public class about extends Activity implements
    GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
// The request code must be 0 or greater.
    private static final int PLUS_ONE_REQUEST_CODE = 0;
    private static final String TAG = "google+";
    private static final String URL = "https://google.com/+Developer";
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private PlusOneButton mPlusOneButton;
    private ConnectionResult mConnectionResult;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.about);

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

    mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
    // Progress bar to be displayed if the connection failure is not resolved.
    //mConnectionProgressDialog = new ProgressDialog(this);
    //mConnectionProgressDialog.setMessage("Signing in...");
    }

@Override
protected void onStart() {
    super.onStart();
    mPlusClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mPlusClient.disconnect();
}

@Override
protected void onResume() {
    super.onResume();
    // Refresh the state of the +1 button each time the activity receives focus.
    mPlusOneButton.initialize(mPlusClient, URL, PLUS_ONE_REQUEST_CODE);
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    mConnectionResult = connectionResult;

   if(!mPlusClient.isConnected()) {
        try {
            mConnectionResult.startResolutionForResult(this, PLUS_ONE_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            mConnectionResult = null;
            mPlusClient.connect();
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mConnectionResult = null;
        mPlusClient.connect();
    }
}


@Override
public void onConnected(Bundle connectionHint) {
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
}


@Override
public void onDisconnected() {
     Log.d(TAG, "disconnected");
 }
}

0 个答案:

没有答案