java - 在 Android 中集成 Google Plus SignIn 显示意外对话框

标签 java android android-studio google-plus google-play-services

我正在尝试将 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) {

}

}

enter image description here

最佳答案

仔细遵循 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/

相关文章:

Java regex - 如何获取空格和字符?

php - 从 php 数据库中获取一组 JSON 数据

java - Android 应用程序可能使用 Dalvik 以外的其他 VM

java - android:TimePickerDialog 阻止用户选择过去的时间,并可以选择具有新日期的 future 时间

android - 从 4 升级到 4.1 后启动 android studio 时出错 (MacOS 10.15)

java线程哪个更好?

java - 如何从 java servlet 中引发 "back button'?

java - 如何获取 Groovy 类的所有属性名称?

android - 如何防止滚动事件被 PopupWindow 拦截

android-studio - Android Studio正在设备上安装旧的APK