2014-01-22 4 views
-1

У меня есть приложение для Android, которое имеет функцию входа в систему. Для процесса входа в системе я интегрировал Google Plus интеграции логинаGoogle plus интеграция

мне удалось получить доступ входа в системе, используя следующую ссылку login with google plus

Но теперь я хочу, чтобы мой Google плюс списка друзей, кто использует это приложение, я уже попытался это fetch people

мне удалось это сделать в facebook API, это Чет способ сделать это в Google Plus

ответ

0
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnCancelListener; 
import android.content.Intent; 
import android.content.IntentSender; 
import android.graphics.drawable.ColorDrawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.plus.PlusClient; 
import com.google.android.gms.plus.PlusShare; 


public class SignInActivity extends Activity implements 
     PlusClient.ConnectionCallbacks, PlusClient.OnConnectionFailedListener, 
     PlusClient.OnAccessRevokedListener { 

    private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1; 

    private static final int REQUEST_CODE_SIGN_IN = 1; 

    // private TextView mSignInStatus; 
    private PlusClient mPlusClient; 
    private Dialog GoolgePlus; 
    private ConnectionResult mConnectionResult; 
    private boolean mSharing; 
    String UserName; 

    // Share 
    private static final String STATE_SHARING = "state_sharing"; 
    private static final int REQUEST_CODE_INTERACTIVE_POST = 2; 
    private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 3; 
    private static final String LABEL_VIEW_ITEM = "VIEW_ITEM"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fbsample); 

     mPlusClient = new PlusClient.Builder(this, this, this) 
       .setVisibleActivities("http://schemas.google.com/AddActivity", 
         "http://schemas.google.com/BuyActivity").build(); 
     mSharing = savedInstanceState != null 
       && savedInstanceState.getBoolean(STATE_SHARING, false); 
     GooglePlus(); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putBoolean(STATE_SHARING, mSharing); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     mPlusClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     mPlusClient.disconnect(); 
     super.onStop(); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     if (id != DIALOG_GET_GOOGLE_PLAY_SERVICES) { 
      return super.onCreateDialog(id); 
     } 

     int available = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(this); 
     if (available == ConnectionResult.SUCCESS) { 
      return null; 
     } 
     if (GooglePlayServicesUtil.isUserRecoverableError(available)) { 
      return GooglePlayServicesUtil.getErrorDialog(available, this, 
        REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES); 
     } 
     return new AlertDialog.Builder(this).setMessage("Error") 
       .setCancelable(true).create(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE_SIGN_IN 
       || requestCode == REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES) { 
      if (resultCode == RESULT_OK && !mPlusClient.isConnected() 
        && !mPlusClient.isConnecting()) { 
       mPlusClient.connect(); 
      } 
     } 
     switch (requestCode) { 
     case REQUEST_CODE_SIGN_IN: 
     case REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES: 
      handleResult(resultCode); 
      break; 

     case REQUEST_CODE_INTERACTIVE_POST: 
      mSharing = false; 
      if (resultCode != RESULT_OK) { 
       Log.e("Sharing", "Failed to create interactive post"); 
      } 
      break; 
     } 
    } 

    @Override 
    public void onAccessRevoked(ConnectionResult status) { 
     if (status.isSuccess()) { 
     } else { 
      mPlusClient.disconnect(); 
     } 
     mPlusClient.connect(); 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     String currentPersonName = mPlusClient.getCurrentPerson() != null ? mPlusClient 
       .getCurrentPerson().getDisplayName() 
       : getString(R.string.unknown_person); 
     UserName = getString(R.string.signed_in_status, currentPersonName); 
     updateButtons(true); 

     if (!mSharing) { 
      // The share button hasn't been clicked yet. 
      return; 
     } 

     mSharing = false; 
     startActivityForResult(getInteractivePostIntent(), 
       REQUEST_CODE_INTERACTIVE_POST); 
    } 

    @Override 
    public void onDisconnected() { 
     mPlusClient.connect(); 
     updateButtons(false /* isSignedIn */); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     mConnectionResult = result; 
     updateButtons(false /* isSignedIn */); 
    } 

    private void updateButtons(boolean isSignedIn) { 
     if (isSignedIn) { 

      GoolgePlus.show(); 
      // mSignInButton.setVisibility(View.INVISIBLE); 
      // GooglePlus(); 
      // mSignOutButton.setEnabled(true); 
     } else { 
      if (mConnectionResult == null) { 
       // mSignInButton.setVisibility(View.INVISIBLE); 
       // GoolgePlus.show(); 
       // mSignInStatus.setText(getString(R.string.loading_status)); 
      } else { 
       // mSignInButton.setVisibility(View.VISIBLE); 
       int available = GooglePlayServicesUtil 
         .isGooglePlayServicesAvailable(this); 
       if (available != ConnectionResult.SUCCESS) { 
        showDialog(DIALOG_GET_GOOGLE_PLAY_SERVICES); 
        return; 
       } 

       try { 

        mConnectionResult.startResolutionForResult(this, 
          REQUEST_CODE_SIGN_IN); 
       } catch (IntentSender.SendIntentException e) { 
        // Fetch a new result to start. 
        mPlusClient.connect(); 
       } 

      } 
     } 
    } 

    public void GooglePlus() { 
     GoolgePlus = new Dialog(SignInActivity.this); 
     GoolgePlus.setContentView(R.layout.dialogeerror); 
     GoolgePlus.getWindow().setBackgroundDrawable(
       new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
     GoolgePlus.getWindow().getAttributes().windowAnimations = R.style.saveanim; 
     Button signout = (Button) GoolgePlus.findViewById(R.id.ok); 
     Button share = (Button) GoolgePlus.findViewById(R.id.cancle); 
     TextView textViewtitle = (TextView) GoolgePlus 
       .findViewById(R.id.textViewtitle); 
     textViewtitle.setText("NotionNote"); 
     TextView text = (TextView) GoolgePlus.findViewById(R.id.txt_texts); 
     signout.setText("Sign Out"); 
     share.setText("Share"); 
     text.setText("Are you sure want to share this post ?"); 
     share.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       GoolgePlus.dismiss(); 
       finish(); 
       if (!mPlusClient.isConnected()) { 
        mSharing = true; 
        if (!mPlusClient.isConnecting()) { 
         mPlusClient.connect(); 
        } 
       } else { 
        startActivityForResult(getInteractivePostIntent(), 
          REQUEST_CODE_INTERACTIVE_POST); 
       } 
      } 
     }); 
     GoolgePlus.setOnCancelListener(new OnCancelListener() { 

      @Override 
      public void onCancel(DialogInterface dialog) { 
       // TODO Auto-generated method stub 
       GoolgePlus.dismiss(); 

       Handler h = new Handler(); 
       h.postDelayed(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         finish(); 
        } 
       }, 500); 

      } 
     }); 
     signout.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       GoolgePlus.dismiss(); 
       if (mPlusClient.isConnected()) { 
        mPlusClient.revokeAccessAndDisconnect(SignInActivity.this); 
        finish(); 
       } 

      } 
     }); 
     GoolgePlus.getWindow().setBackgroundDrawable(
       new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    } 

    private void handleResult(int resultCode) { 
     if (resultCode == RESULT_OK) { 
      if (!mPlusClient.isConnecting() && !mPlusClient.isConnected()) { 
       mPlusClient.connect(); 
      } 
     } else { 
      Log.e("Sharing", "Unable to sign the user in."); 
      finish(); 
     } 
    } 

    private Intent getInteractivePostIntent() { 

     String action = "/?view=true"; 
     Uri callToActionUrl = Uri 
       .parse(getString(R.string.plus_example_deep_link_url) + action); 
     String callToActionDeepLinkId = getString(R.string.plus_example_deep_link_id) 
       + action; 

     PlusShare.Builder builder = new PlusShare.Builder(this, mPlusClient); 

     builder.addCallToAction(LABEL_VIEW_ITEM, callToActionUrl, 
       callToActionDeepLinkId); 

     builder.setContentUrl(Uri.parse(AddNote.PPTPath)); 

     builder.setContentDeepLinkId(
       getString(R.string.plus_example_deep_link_id), null, null, null); 
     builder.setText(" Android Application").setType("image/jpeg"); 

     return builder.getIntent(); 
    } 

} 



Intent intent = new Intent(getApplicationContext(), 
         SignInActivity.class); 
       startActivity(intent); 
+0

спасибо за ответ, и может послать мне строковые vaues, который дает мне ошибки компиляции –

Смежные вопросы