2014-10-29 28 views
7

Tôi đang thực hiện Google+ đăng nhập thông qua tài liệu hướng dẫn phát triển. Các phương thức onConnectionFailed của tôi đang được gọi sau khi tôi chọn một tài khoản để đăng nhập bằng lỗi RESOLUTION_REQUIRED (mã lỗi 6). Thao tác này sẽ khởi chạy hộp thoại 'Chọn tài khoản' khác sau đó hoạt động (đưa tôi đến quyền) nếu tôi chọn cùng một tài khoản. Tôi không chắc tại sao nó nhắc một hộp thoại khác. Tôi bắt đầu với resolveSignInError Bất kỳ thông tin chi tiết nào?Google Plus đăng nhập 'Chọn một tài khoản' hộp thoại xuất hiện hai lần

Ngoài ra, việc chọn tài khoản từ 'Chọn tài khoản' sẽ hiển thị quyền, nếu tôi nhấn hủy tại thời điểm đó và chọn một tài khoản khác từ mặt số, nó sẽ hiển thị hình ảnh sai cho quyền hoặc đôi khi không có ảnh nào cả. Tôi cũng đã nhận được An internal error has occurred bánh mì nướng một lần.

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    if (!mIntentInProgress) { 
     // Store the ConnectionResult so that we can use it later when the user clicks 
     // 'sign-in'. 
     mConnectionResult = connectionResult; 
     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(); 
     } 
    } 
} 

private void resolveSignInError() { 
    if (mConnectionResult != null && mConnectionResult.hasResolution()) { 
     try { 
      mIntentInProgress = true; 
      startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(), 
        RC_SIGN_IN, null, 0, 0, 0); 

     } catch (IntentSender.SendIntentException e) { 
      // The intent was canceled before it was sent. Return to the default 
      // state and attempt to connect to get an updated ConnectionResult. 
      mIntentInProgress = false; 
      mGoogleApiClient.connect(); 
     } 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RC_SIGN_IN) { 
     if (resultCode != RESULT_OK) { 
      mSignInClicked = false; 
     } 
     mIntentInProgress = false; 
     if (!mGoogleApiClient.isConnecting()) { 
      mGoogleApiClient.connect(); 
     } 
    } 
} 

Trả lời

1

Mã sau đây đang hoạt động tốt cho tôi, Vui lòng cập nhật mã của bạn và kiểm tra.

private void resolveSignInError() { 
     if (mConnectionResult.hasResolution()) { 
      try { 
       mIntentInProgress = true; 
       mConnectionResult.startResolutionForResult(this, RC_SIGN_IN); 
      } catch (SendIntentException e) { 
       mIntentInProgress = false; 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     if (!result.hasResolution()) { 
      GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show(); 
      return; 
     } 

     if (!mIntentInProgress) { 

      mConnectionResult = result; 

      if (mSignInClicked) { 

       resolveSignInError(); 
      } 
     } 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
     if (requestCode == RC_SIGN_IN) { 
      if (responseCode != RESULT_OK) { 
       mSignInClicked = false; 
      } 

      mIntentInProgress = false; 

      if (!mGoogleApiClient.isConnecting()) { 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 
2

Trong "onActivityForResult", bạn nên xóa dòng đầu tiên "super.onActivityResult (requestCode, resultCode, data);"

Ngoài ra, chỉ để chắc chắn, bạn tạo GoogleApiClient của mình trong onCreate, kết nối nó trong onStart() và ngắt kết nối trong onStop()?

Bạn có gọi resolveSignInError() từ bất kỳ nơi nào khác trong mã của bạn không?

+0

Cảm ơn. Tôi đã có một super.onActivityForResult() và loại bỏ nó (resp. Chỉ gọi nó khi tôi không tự xử lý kết quả) đã giải quyết được vấn đề. – Ridcully

0

Nhập Function signout trong getprofileinformation(). Giống như vậy.Hy vọng, mã này có thể giúp bạn

private void getProfileInformation() { 
     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); 

       forget_login_txt.setText(personName+" "+email); 
       Log.e(TAG, "Name: " + personName + ", plusProfile: " 
         + personGooglePlusProfile + ", email: " + email 
         + ", Image: " + personPhotoUrl); 


       personPhotoUrl = personPhotoUrl.substring(0, 
         personPhotoUrl.length() - 2) 
         + PROFILE_PIC_SIZE; 


       signOutFromGplus(); 



      } else { 
       Toast.makeText(getApplicationContext(), 
         "Person information is null", Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
Các vấn đề liên quan