安卓谷歌加登录按钮无法使用

5

我正在尝试在我的应用程序的第一个活动中实现使用Google+登录。我按照Google开发教程进行了操作,但是当我单击“签到”按钮时没有任何反应。我认为我犯了一些错误,以下是代码:

public class MainActivity extends FragmentActivity implements OnClickListener,
    ConnectionCallbacks, OnConnectionFailedListener {

public static final String mAPP_ID = "xxxx";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private static final String TAG = "MainActivity";

private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;

private ImageButton googleSignOutButton;

AssetsExtracter mTask;

private MainFragment mainFragment;

static {
    IMetaioSDKAndroid.loadNativeLibs();
}

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

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, mainFragment).commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
                .findFragmentById(android.R.id.content);
    }

    mPlusClient = new PlusClient.Builder(this, this, this)
            .setActions("http://schemas.google.com/AddActivity")
            .setScopes(Scopes.PLUS_LOGIN) 
            .build();
    // Progress bar to be displayed if the connection failure is not
    // resolved.
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");

    mTask = new AssetsExtracter();
    mTask.execute(0);

    findViewById(R.id.sign_in_button).setOnClickListener(this);
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mConnectionProgressDialog.isShowing()) {
        // The user clicked the sign-in button already. Start to resolve
        // connection errors. Wait until onConnected() to dismiss the
        // connection dialog.
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this,
                        REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                mPlusClient.connect();
            }
        }
    }

    // Save the intent so that we can start an activity when the user clicks
    // the sign-in button.
    mConnectionResult = result;

}

@Override
public void onConnected(Bundle connectionHint) {
    // We've resolved any connection errors.
    mConnectionProgressDialog.dismiss();
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

}

@Override
public void onDisconnected() {
    Log.d(TAG, "disconnected");

}

@Override
protected void onActivityResult(int requestCode, int responseCode,
        Intent intent) {
    super.onActivityResult(requestCode, responseCode, intent);
    if (requestCode == REQUEST_CODE_RESOLVE_ERR
            && responseCode == RESULT_OK) {
        mConnectionResult = null;
        mPlusClient.connect();
    }
}

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

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

@Override
public void onClick(View v) {
    if (v.getId() == R.id.sign_in_button
            && !mPlusClient.isConnected()) {
        if (mConnectionResult == null) {
            mConnectionProgressDialog.show();
        } else {
            try {
                mConnectionResult.startResolutionForResult(
                        getParent(), REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                // Try connecting again.
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }
    }
}

}

编辑: 我发现在onConncectionFailed()方法中,如果我删除第一个“if()”检查processDialog是否显示的语句,当应用程序启动时,不点击任何内容,谷歌+对话框将出现要求我登录。这很奇怪。

编辑: 我使用普通按钮并在其上实现onClick来解决了我的问题,按照Google Dev的教程进行操作。

2个回答

0

是的,我按照教程创建了客户端ID,并使用keytool生成了SHA1摘要。我需要将客户端ID作为字符串放在string.xml文件中吗? - Cardella
不需要在任何地方添加客户端ID。客户端ID与您的应用程序相关联,因为它使用您的密钥进行签名。创建客户端ID时,您应该输入应用程序证书的指纹。 - Lee
这是我使用SHA1指纹所做的。我不知道为什么登录无法工作,而Logcat上也没有关于错误或异常的信息。 - Cardella
在完全按照指示操作后,我发现我的应用在运行Android 5.1(BLU)的手机上无法正常工作,但是在运行Android 4.2(Huawaii)和Android 4.3(Samsung)的其他手机上可以正常工作。有什么想法吗? - Chad Mx

0

经过一番调查,我意识到仅在控制台提供发布 SHA-1 只能让我在生成已签名 APK 并手动安装 APK 而非通过 Android Studio 安装时才能运行我的项目并拥有一个可用的登录按钮。
我想改变 SHA-1 为调试版本是解决方案,但直到现在我才能够更改它。

要将您的 SHA-1 从发布版本更改为调试版本,以便您可以通过 Android Studio 安装一个可工作/可测试的 apk:

打开 Google Cloud 控制台 > 打开 API 管理器 > 单击凭据。
在 OAuth2.0 客户端 ID 下 > 单击您应用的 Android 客户端并将 SHA-1 更改为您的调试版本(按照这些说明应该可以帮助您检索调试 SHA-1:https://developer.android.com/studio/publish/app-signing.html
完成后,在 Cloud 控制台中保存您的新凭据并返回屏幕以生成配置文件。您应该注意到您更改的 SHA-1。现在生成配置文件并将其粘贴到app/..项目目录中。清理、重建和运行您的项目,您的登录按钮现在应该正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接