安卓:登录使用谷歌帐户? [英] Android: Login using google account?

查看:499
本文介绍了安卓:登录使用谷歌帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到如何使用谷歌帐户来执行我的应用程序登录一个很好的证明例子。

I am trying to find a good documented example of how to perform a login in my app using a google account.

也许我找错了地方,但我找不到在Android SDK文档任何东西。从我的理解谷歌服务的一部分,但仍然有问题找的例子。

Maybe I am looking in the wrong place, but i can't find anything in the android sdk docs. From what i understand its part of Google Services but still having problems find examples.

我还需要支持,如果用户具有设备的配置超过1谷歌帐户弹出,并要求使用哪个帐户。

I also need to support if the user has more than 1 google account configured on the device to popup and ask which account to use.

然后在我的应用程序加载的未来我会自动登录。

Then on future loading of my app I would automatically login.

任何人都可以点我在正确的方向,还是有没有例子可用?

Can anyone point me in the right direction, or are there no examples available?

感谢

推荐答案

您可能需要使用本指南:

You probably want to use this guide:

https://developers.google.com/+/mobile/android/签到

从指南:
您必须创建一个谷歌的API控制台项目并初始化PlusClient对象

添加Google+登录按钮,您的应用

添加SignInButton在应用程序的布局:

Add the SignInButton in your application's layout:

<com.google.android.gms.common.SignInButton
    android:id="@+id/sign_in_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

初​​始化mPlusClient在你Activity.onCreate处理程序所要求的可见的活动。

Initialize mPlusClient with the requested visible activities in your Activity.onCreate handler.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPlusClient = new PlusClient.Builder(this, this, this)
            .setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
            .build();
}

在Android活动中,注册按钮的OnClickListener在用户点击注册时:

In the Android activity, register your button's OnClickListener to sign in the user when clicked:

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

在用户点击登录按钮后,你应该开始解决mConnectionResult持有的任何连接错误。可能的连接错误包括提示用户选择一个帐户,并授予您的应用程序的访问。

After the user has clicked the sign-in button, you should start to resolve any connection errors held in mConnectionResult. Possible connection errors include prompting the user to select an account, and granting access to your app.

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

当用户成功登录,您的onConnected处理器将被调用。在这一点上,你可以检索用户的帐户名或进行身份验证的请求。

When the user has successfully signed in, your onConnected handler will be called. At this point, you are able to retrieve the user’s account name or make authenticated requests.

@Override
public void onConnected(Bundle connectionHint) {
    mConnectionProgressDialog.dismiss();
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}

这篇关于安卓:登录使用谷歌帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆