我正在尝试将 google plus 登录集成到我的 android 应用程序中。所有 API 调用以及代码都工作正常。但我收到了一个意外的对话框,尽管我的布局 xml 中没有任何内容。
此外,我已经从 Google 开发者网站完成了所有先决条件集成过程。
下面是我的代码:
public class GooglePlusActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "GooglePlusMainActivity";
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mPlusClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_plus);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mPlusClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
signIn();
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mPlusClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
finish();
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount currentPerson = result.getSignInAccount();
if (currentPerson != null) {
Intent intent = new Intent(GooglePlusActivity.this, RegisterActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("socialuserID", currentPerson.getId());
intent.putExtra("userEmail", currentPerson.getEmail());
intent.putExtra("userFname", currentPerson.getDisplayName());
startActivity(intent);
}
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
最佳答案
仔细遵循 Google 集成指南:
https://developers.google.com/identity/sign-in/android/start-integrating
您需要:
生成 google-services.json 文件并将其添加到/app 目录
在项目的 gradle 中添加依赖项:
dependencies { classpath 'com.google.gms:google-services:2.0.0-alpha6' }
在应用程序 gradle 中添加插件:
apply plugin: 'com.google.gms.google-services'
和依赖项:
dependencies { compile 'com.google.android.gms:play-services-auth:8.3.0' }
在Activity的onCreate中创建GoogleApiClient:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build();
您的 Activity 需要实现 GoogleApiClient.OnConnectionFailedListener(Android Studio 会通知您)
在布局中添加登录按钮:
<com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
将 onClickListener 附加到按钮:
findViewById(R.id.sign_in_button).setOnClickListener(this);
这会将 View.OnClickListener 的实现添加到 Activity 中
创建 onClick 方法:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: signIn(); break; } }
创建登录方法:
private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); }
添加捕获事件的方法
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } }
最后处理 SignIn
private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handleSignInResult:" + result.isSuccess()); if (result.isSuccess()) { // Signed in successfully, show authenticated UI. GoogleSignInAccount acct = result.getSignInAccount(); Log.d(TAG, "authenticated " + acct.getDisplayName() + " " + acct.getEmail()); } else { // Signed out, show unauthenticated UI. Log.d(TAG, "unAuthenticated"); } }
上面的示例可以在没有白色对话框的情况下运行。
您可以引用 Google 示例:https://github.com/googlesamples/google-services/blob/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin/SignInActivity.java如果您有任何疑问
关于java - 在 Android 中集成 Google Plus SignIn 显示意外对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36497666/