试试这个...
在 OnCreate..
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
在Activity中添加这三个方法...调用登录和需要时退出
private void signInWithGplus() {
Log.i("call", "signinwithgoogle");
mGoogleApiClient.connect();
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
private void resolveSignInError() {
Log.i("call", "resolvesigninerror");
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
static void signOutFromGplus() {
Log.i("call", "signoutfromgoogle");
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
// Toast.makeText(getApplicationContext(), "sign out from google",
// Toast.LENGTH_SHORT).show();
storeUserData.setBoolean(AppConstants.KEY_IS_LOGIN, false);
updateUI(false);
}
}
还添加以进行身份验证。通过这种方法,您将获得完整的个人资料信息:
private void getProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
googleFirstName = currentPerson.getDisplayName();
googleImage = currentPerson.getImage().getUrl();
// String personGooglePlusProfile = currentPerson.getUrl();
googleEmailId = Plus.AccountApi
.getAccountName(mGoogleApiClient);
googleId = currentPerson.getId();
Log.i("googleId", googleId);
JSONObject fullname = new JSONObject(currentPerson.getName()
+ "");
googleFirstName = fullname.getString("familyName");
googleLastName = fullname.getString("givenName");
if (currentPerson.getGender() == 0)
googleGender = "female";
else if (currentPerson.getGender() == 1)
googleGender = "male";
else
googleGender = "other";
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
googleImage = googleImage
.substring(0, googleImage.length() - 2)
+ PROFILE_PIC_SIZE;
new GetGoogleAuthTask().execute();
// new LoadProfileImg(null).execute(personPhotoUrl);
} else {
// Toast.makeText(getApplicationContext(),
// "Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
SharedPreferences SharedPreference;
Editor editor;
private class GetGoogleAuthTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(RegisterActivity.this,
Plus.AccountApi.getAccountName(mGoogleApiClient),
"oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME);
// Change the permissions as per your need.
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
// Intent recover = e.getIntent();
// startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
if (token != null) {
googleToken = token;
// Log.i(TAG, "Access token retrieved:" + token);
SharedPreference = getApplicationContext()
.getSharedPreferences("TokenPreference", 0);
editor = SharedPreference.edit();
editor.putString("access_token", token);
editor.commit();
}
Log.i("GooGle", "called");
loginWithGoogleData();
storeUserData.setBoolean(AppConstants.KEY_IS_LOGIN, true);
}
}
最后是onactivity结果
@Override
public void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
uiHelper.onActivityResult(requestCode, responseCode, data);
/******** GOOGLE CODE START **************/
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
/******** GOOGLE CODE END **************/
}
不要忘记在清单中添加权限...
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>