2017-01-09 33 views
0

我想集成google plus登錄到我的android應用程序我已經嘗試幾乎所有的源代碼,他們在網絡中,但仍然無法實現我沒有在我的代碼中有任何錯誤,但它仍然不會登錄通過gmail請任何一個給解決方案。Android:如何將Google加入登錄到Android應用程序?

mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build(); 

    } 


    protected void onStart() { 
      super.onStart(); 
      mGoogleApiClient.connect(); 
      } 


    protected void onStop() { 
      super.onStop(); 
      if (mGoogleApiClient.isConnected()) { 
       mGoogleApiClient.disconnect(); 
      } 
      } 


    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) { 
      // store mConnectionResult 
      mConnectionResult = result; 

      if (signedInUser) { 
      resolveSignInError(); 
      } 
     } 
     } 

     @Override 
     protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
     switch (requestCode) { 
     case RC_SIGN_IN: 
      if (responseCode == RESULT_OK) { 
      signedInUser = false; 

      } 
      mIntentInProgress = false; 
      if (!mGoogleApiClient.isConnecting()) { 
      mGoogleApiClient.connect(); 
      } 
      break; 
     } 
     } 

     @Override 
     public void onConnected(Bundle arg0) { 
     signedInUser = false; 
     Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show(); 
     getProfileInformation(); 
     } 

     private void updateProfile(boolean isSignedIn) { 
     if (isSignedIn) { 
      signinFrame.setVisibility(View.GONE); 
      profileFrame.setVisibility(View.VISIBLE); 

     } else { 
      signinFrame.setVisibility(View.VISIBLE); 
      profileFrame.setVisibility(View.GONE); 
     } 
     } 

     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 email = Plus.AccountApi.getAccountName(mGoogleApiClient); 

      username.setText(personName); 
      emailLabel.setText(email); 

      new LoadProfileImage(image).execute(personPhotoUrl); 

      // update profile frame with new info about Google Account 
      // profile 
      updateProfile(true); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     } 

     @Override 
     public void onConnectionSuspended(int cause) { 
     mGoogleApiClient.connect(); 
     updateProfile(false); 
     } 

     @Override 
     public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.signin: 
      googlePlusLogin(); 
      break; 
     } 
     } 

     public void signIn(View v) { 
     googlePlusLogin(); 
     } 

     public void logout(View v) { 
     googlePlusLogout(); 
     } 

     private void googlePlusLogin() { 
     if (!mGoogleApiClient.isConnecting()) { 
      signedInUser = true; 
      resolveSignInError(); 
     } 
     } 

     private void googlePlusLogout() { 
     if (mGoogleApiClient.isConnected()) { 
      Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); 
      mGoogleApiClient.disconnect(); 
      mGoogleApiClient.connect(); 
      updateProfile(false); 
     } 
     } 

     // download Google Account profile image, to complete profile 
     private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> { 
     ImageView downloadedImage; 

     public LoadProfileImage(ImageView image) { 
      this.downloadedImage = image; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String url = urls[0]; 
      Bitmap icon = null; 
      try { 
      InputStream in = new java.net.URL(url).openStream(); 
      icon = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
      } 
      return icon; 
     } 

     protected void onPostExecute(Bitmap result) { 
      downloadedImage.setImageBitmap(result); 
     } 


     } 
    /*@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    }*/ 

} 
+1

解釋你嘗試過什麼,在這裏把你的代碼,你試過 –

+0

發表您的code..then,我們將能夠幫助.. – rafsanahmad007

+0

嘗試「火力地堡認證」谷歌,Facebook,微博登錄。 https://www.youtube.com/watch?v=MFWZLYFD8yI –

回答