使用Google帐户登录后获取FirstName,LastName,电子邮件和个人资料照片

时间:2017-07-11 12:45:24

标签: android google-plus

我有一个 SignIn 活动,用户可以使用Google帐户登录,还有另一项活动雇主,然后会获取用户的基本信息并显示它。< / p>

我的登录活动:

public class SignIn extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {

//Signin employer_button
private SignInButton signInButton;

//Signing Options
private GoogleSignInOptions gso;

//google api client
private GoogleApiClient mGoogleApiClient;

//Signin constant to check the activity result
private int RC_SIGN_IN = 100;

private ProgressDialog mProgressDialog;
private TextView mStatusTextView;

private CoordinatorLayout coordinatorLayout;

// private TextView textViewName;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signin);

    //   textViewName = (TextView) findViewById(R.id.textViewName);

    coordinatorLayout = (CoordinatorLayout) findViewById(R.id
            .coordinatorLayout);

    //Initializing google signin option
    gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestProfile()
            .requestEmail()
            .build();

    //Initializing signinbutton
    signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    signInButton.setScopes(gso.getScopeArray());

    //Initializing google api client
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();


    //Setting onclick listener to signing employer_button
    signInButton.setOnClickListener(this);


}

private void signIn() {
    //Creating an intent
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);

    //Starting intent for result
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //If signin
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
       GoogleSignInAccount acct = result.getSignInAccount();
        String personName = acct.getDisplayName();
        String personGivenName = acct.getGivenName();
        String personFamilyName = acct.getFamilyName();
        String personEmail = acct.getEmail();
        String personId = acct.getId();
        Uri personPhoto = acct.getPhotoUrl();
        //Calling a new function to handle signin
        handleSignInResult(result);
    }
}


//After the signing we are calling this function
private void handleSignInResult(GoogleSignInResult result) {
    //If the login succeed
    if (result.isSuccess()) {
        //Getting google account
        GoogleSignInAccount acct = result.getSignInAccount();

        Intent intent = new Intent(this, Selection.class);
        startActivity(intent);
        finish();

        //Displaying name and email
        //  textViewName.setText(acct.getDisplayName());


    } else {
        //If login fails
        // Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
        Snackbar snackbar = Snackbar
                .make(coordinatorLayout, "Login Failed!", Snackbar.LENGTH_LONG).setActionTextColor(Color.RED);
        snackbar.show();
    }
}

@Override
public void onClick(View v) {
    if (v == signInButton) {
        //Calling signin
        signIn();
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {


}
}

我的雇主活动:

    public class EmployerActivity extends AppCompatActivity {
    TextView username;
    private GoogleApiClient mGoogleApiClient;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_employeractivity);

    username = (TextView) findViewById(R.id.username);

 }
}

更新

我的选择活动

public class Selection extends AppCompatActivity {

Button employerButton, employeeButton;
private GoogleApiClient mGoogleApiClient;


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

    employerButton = (Button) findViewById(R.id.employerButton);
    employeeButton = (Button) findViewById(R.id.employeeButton);

    onSelection();
}

private void onSelection() {
            employerButton.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v) {
                    Intent i = new Intent(Selection.this, Employer.class);
                    startActivity(i);
                    finish();
                }
            });

            employeeButton.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v) {
                    Intent i = new Intent(Selection.this, Employee.class);
                    startActivity(i);
                    finish();
                }
            });
        }

public boolean onCreateOptionsMenu (Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.items, menu);

    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionItemSelected(MenuItem item){
   switch (item.getItemId()) {
       case R.id.item_logout:
           loggedOut();
       return true;
       default:
           return super.onOptionsItemSelected(item);
   }
}

private void loggedOut() {

    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status){
                  Intent intent = new Intent(Selection.this, SignIn.class);
                    startActivity(intent);
                    finish();
                }
            });
}

}

2 个答案:

答案 0 :(得分:0)

获取显示名称&amp;转到下一个活动

if (result.isSuccess()) {
    GoogleSignInAccount acct = result.getSignInAccount();

    Intent intent = new Intent(this, Selection.class);
    intent.putExtra("displayName", acct.getDisplayName());
    startActivity(intent);
    finish();
}

在您的章节活动中阅读&amp;像这样使用

name = getIntent().getExtras().getString("displayName");
username .setText(name);

修改

如果您需要更新其他活动,请将该字段声明为public&amp;像这样从这里访问..

TargetClass class = new TargetClass();
class.name = ((TextView) findViewById(R.id.name));
class.name.setText("hello");

希望这可以帮到你..

答案 1 :(得分:0)

您可以使用Activity登录 Intent启动选择活动,并将数据与其一起传递。使用Intent方法将数据放入putExtra()。然后,在选择 getIntent().getExtras()的{​​{1}}方法中使用onCreate

详细解释 - Start Another Activity

代码 -

登录 Activity -

Activity

此数据会发送到选择 public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //If signin if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); //Calling a new function to handle signin handleSignInResult(result); } } //After the signing we are calling this function private void handleSignInResult(GoogleSignInResult result) { //If the login succeed if (result.isSuccess()) { //Getting google account GoogleSignInAccount acct = result.getSignInAccount(); Intent intent = new Intent(this, Selection.class); intent.putExtra("name", acct.getDisplayName()); intent.putExtra("email", acct.getEmail()); intent.putExtra("profile_pic", acct.getPhotoUrl().toString()); startActivity(intent); finish(); } else { //If login fails // Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show(); Snackbar snackbar = Snackbar .make(coordinatorLayout, "Login Failed!", Snackbar.LENGTH_LONG).setActionTextColor(Color.RED); snackbar.show(); } } ,可以存储,直到雇主 Activity启动。启动雇主Activity时,您可以使用Activity将存储的数据存储到Intent,并获取雇主 putExtra()中的数据。

选择 Activity -

Activity

雇主 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_selection); employerButton = (Button) findViewById(R.id.employerButton); employeeButton = (Button) findViewById(R.id.employeeButton); onSelection(getIntent().getExtras()); } private void onSelection(Bundle extras) { String name=extras.getString("name"); String email=extras.getString("email"); String profile_pic=extras.getString("profile_pic"); employerButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { Intent i = new Intent(Selection.this, Employer.class); i.putExtra("name", name); i.putExtra("email", email); i.putExtra("profile_pic", profile_pic); startActivity(i); finish(); } }); employeeButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { Intent i = new Intent(Selection.this, Employee.class); startActivity(i); finish(); } }); } -

Activity