实现ConnectionCallbacks、OnConnectionFailedListener,然后设置普通按钮。
btnGoogle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (internetChecker.isConnectingToInternet() == false) {
Toast.makeText(getApplicationContext(),getString(R.string.interet_connectivity),Toast.LENGTH_SHORT).show();
} else {
signInWithGplus();
}
}
});
/**
* 登录谷歌账号
* */
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
/**
* 解决登录错误的方法
* */
private void resolveSignInError() {
try {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
} catch (Exception e) {
// TODO: 处理异常
Log.d("error",e.toString());
return;
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
Log.d("result.toString()", result.toString());
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,0).show();
return;
}
if (!mIntentInProgress) {
// 存储 ConnectionResult 以备后用
mConnectionResult = result;
if (mSignInClicked) {
// 用户已经点击了“登录”,所以我们尝试解决所有错误,直到用户登录或取消。
resolveSignInError();
}
}
}
@Override
public void onConnected(Bundle arg0) {
Log.d("onConnected", "onConnected::"+session.getIsRequestLogout());
if (!session.getIsRequestLogout()) {
mSignInClicked = false;
// 获取用户信息
getGoogleProfileInformation();
// 登录后更新UI
}
else
{
signOutFromGplus();
}
/**
* 获取用户信息:姓名、电子邮件、个人资料图片
* */
private void getGoogleProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
GoogleId=currentPerson.getId();
UserName = currentPerson.getDisplayName();
UserEmail = Plus.AccountApi.getAccountName(mGoogleApiClient);
/String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.e("login", "Name: " + personName + ", plusProfile: "
+ personGooglePlusProfile + ", email: " + email
+ ", Image: " + personPhotoUrl);/
} else {
Toast.makeText(getApplicationContext(),getString(R.string.google_profile_error), Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
Toast.makeText(getApplicationContext(), getString(R.string.google_suspended_account), Toast.LENGTH_LONG).show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
if (requestCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
在连接中断时,重新连接Google Api客户端,并显示悬浮账户的信息。当请求代码为RC_SIGN_IN时,如果结果不是RESULT_OK,则将mSignInClicked设置为false。如果Google Api客户端未连接,则重新连接。
}
我还有一个问题,请参考此答案Android Google+登录 "发生了内部错误"