我正在通过Fragment
实施Google plus登录。
我在对话框中获得了Google标志的无限循环。
代码: 活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("TEST", "requestCode: " + requestCode + ", resultCode: " + resultCode);
if (FragmentLogIn.RC_SIGN_IN == requestCode){
FragmentLogIn fragment = (FragmentLogIn) getSupportFragmentManager().findFragmentById(mainContent.getId());
fragment.onActivityResult(requestCode, resultCode, data);
} else{
super.onActivityResult(requestCode, resultCode, data);
}
}
片段:
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onActivityResult(int requestCode, int responseCode, Intent intent)
{
Log.e("GOOGLE+", "requestCode: " + requestCode + "responseCode = " + responseCode);
if (requestCode == RC_SIGN_IN) {
if (responseCode != Activity.RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle bundle)
{
Log.e("GOOGLE TEST", "onConnected");
//get user info
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 email = Plus.AccountApi.getAccountName(mGoogleApiClient);
new RetrieveTokenTask().execute(email);
} else {
Log.e("GOOGLE+", "info = null");
}
} catch (Exception e) {
e.printStackTrace();
}
mSignInClicked = false;
}
@Override
public void onConnectionSuspended(int i)
{
Log.e("GOOGLE TEST", "onConnectionSuspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), getActivity(),
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 onClick(View view)
{
if (view.getId() == R.id.google_sign_in_button) {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
} else if (view.getId() == R.id.google_sign_out_button) {
revokeGplusAccess();
}
}
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String accountName = params[0];
Log.e("accountName", accountName);
String scopes = "oauth2:profile "+Scopes.PLUS_LOGIN;;
String token = null;
try {
if (getActivity() != null)
token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), accountName, scopes);
} catch (IOException e) {
Log.e("GOOGLE+", e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), 1);
} catch (GoogleAuthException e) {
Log.e("GOOGLE+", e.getMessage());
}
return token;
}
@Override
protected void onPostExecute(String s) {
Log.d("TOKEN", "token: " + s);
}
}
private void revokeGplusAccess() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
Log.e("GOOGLE+", "User access revoked!");
mGoogleApiClient.connect();
}
});
}
}
此代码在Activity
中写入时有效,但当我将其切换为Fragment
时,我得到无限循环。
我做错了什么?是否有任何机构使用访问令牌使用Google加上登录的不同示例?
谢谢, 伊兰
答案 0 :(得分:4)
当您断开googleApiClient
中的onStop()
时,会出现无限循环问题。您应该只在onDestroy()
。
希望它有所帮助。
答案 1 :(得分:0)
删除行
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();
}
来自onConnectionFailed(ConnectionResult result)
方法,无限循环不再发生。