Android Studio 谷歌登录弹出
创始人
2024-08-16 18:00:43
0

要实现Android Studio中的谷歌登录弹出功能,你可以按照以下步骤操作:

  1. 在你的Android项目中,打开build.gradle文件,并确保已添加以下依赖项:
implementation 'com.google.android.gms:play-services-auth:19.2.0'
  1. 在你的布局文件中,添加一个按钮,用于触发谷歌登录弹出:
  1. 在你的Activity中,添加以下代码:
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.common.SignInButton;

public class MainActivity extends AppCompatActivity {

    private GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN = 1;

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

        // 配置Google登录选项
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        // 构建GoogleSignInClient对象
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        // 设置Google登录按钮样式
        SignInButton signInButton = findViewById(R.id.google_login_button);
        signInButton.setSize(SignInButton.SIZE_STANDARD);

        // 设置Google登录按钮点击事件
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 启动Google登录流程
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 处理Google登录结果
        if (requestCode == RC_SIGN_IN) {
            Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // 成功登录,获取Google登录用户信息
                GoogleSignInAccount account = task.getResult(ApiException.class);
                String displayName = account.getDisplayName();
                String email = account.getEmail();
                // TODO: 在此处处理登录成功后的逻辑
            } catch (ApiException e) {
                // Google登录失败
                Log.w(TAG, "Google sign in failed", e);
            }
        }
    }
}

这样,当用户点击“Google Login”按钮时,将会弹出谷歌登录界面。用户成功登录后,你可以在onActivityResult()方法中处理登录成功后的逻辑。

相关内容

热门资讯

2026版辅助挂!牵手游戏ap... 2026版辅助挂!牵手游戏app辅助器,心悦踢辅助软件-其实真的是有辅助神器(哔哩哔哩)1、完成牵手...
反观!金州水鱼脚本,永盛联盟辅... 反观!金州水鱼脚本,永盛联盟辅助脚本-其实真的是有辅助插件(哔哩哔哩)1、下载好永盛联盟辅助脚本脚本...
方法辅助挂!新漫游免费辅助器,... 方法辅助挂!新漫游免费辅助器,新海贝之城脚本-竟然存在有辅助脚本(哔哩哔哩)在进入新漫游免费辅助器软...
代打辅助挂!微信老铁13水辅助... 代打辅助挂!微信老铁13水辅助,仙神互娱辅助-果然确实有辅助器(哔哩哔哩)1、任何微信老铁13水辅助...
黑科技辅助挂!乐酷副厅外卖辅助... 黑科技辅助挂!乐酷副厅外卖辅助,掌中乐游戏辅助工具-好像真的是有辅助工具(哔哩哔哩)1、下载好掌中乐...
今日!桃乐甘肃麻将辅助器,天天... 今日!桃乐甘肃麻将辅助器,天天福建十三兵修改器-一贯存在有辅助挂(哔哩哔哩)1、任何天天福建十三兵修...
透视免费!新九天作必弊系统,赣... 透视免费!新九天作必弊系统,赣湘互娱辅助-切实是真的有辅助神器(哔哩哔哩)1、玩家可以在新九天作必弊...
截至目前!金虎爷辅助器,新51... 截至目前!金虎爷辅助器,新518互游插件下载-真是是真的有辅助神器(哔哩哔哩)1、下载好新518互游...
截至发稿!蜀山四川辅助脚本,福... 截至发稿!蜀山四川辅助脚本,福建天天开心辅助工具下载-真是真的是有辅助app(哔哩哔哩)1、上手简单...
随着!杭州都莱大菠萝买了挂有用... 随着!杭州都莱大菠萝买了挂有用吗,小唐家乐园辅助-真是是有辅助软件(哔哩哔哩)1、游戏颠覆性的策略玩...