最佳答案
请引用以下链接,了解 Google 与我们应用程序的集成。 https://developers.google.com/identity/sign-in/android/start-integrating
需要记住的几个步骤如下: 1) 单击 https://developers.google.com/identity/sign-in/android/start-integrating 中的生成配置按钮生成 google-service.json 文件
2) 复制google-service.json文件到应用的app文件夹
3) 仔细按照开发者链接的步骤
有代码截图引用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
/**
*
* Google plus
*
*/
private void signIn(){
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signIn]
// [START signOut]
private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
// [START_EXCLUDE]
// updateUI(false);
// [END_EXCLUDE]
}
});
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d("LOGIN", "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
Constant.socail_data = "Google+";
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
firstName = acct.getDisplayName().split(" ")[0];
if(acct.getDisplayName().split(" ")[1]!= null){
lastName = acct.getDisplayName().split(" ")[1];
}
googleEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
Toast.makeText(LoginActivity.this, personName, 30).show();
System.out.println(personName + ">>>" +googleEmail + ">>" + personId + ">>>" + personPhoto);
} else {
}
}
// [START revokeAccess]
private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
// [START_EXCLUDE]
System.out.println("LOGIN"+status.toString());
// [END_EXCLUDE]
}
});
}
@Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d("LOGIN", "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
关于android - Google plus 登录在 AuthLogin 上显示虚拟白色 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35099587/