Android Google plus 登录按钮不起作用

Car*_*lla 5 authentication android button google-plus

我正在尝试在我的应用程序的第一个活动中实现使用 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();
            }
        }
    }
}

}
Run Code Online (Sandbox Code Playgroud)

编辑:我发现在 onConncectionFailed() 方法中,如果我删除第一个“if()”来检查 processDialog 是否显示,那么当应用程序启动时,无需单击任何内容,就会出现一个 google+ 对话框,要求我登录。这很奇怪

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

Lee*_*Lee 0

您是否已在 Google 开发者控制台上注册了您的应用程序?您需要确保您的 Android 应用程序具有启用了 Google+ API 的关联客户端 ID。

https://developers.google.com/+/mobile/android/getting-started#step_1_enable_the_google_api