首页 » 谷歌 » 登录文档官方文档中有个配置项目,注销功能就这样

登录文档官方文档中有个配置项目,注销功能就这样

 

谷歌锁区号/谷歌邮箱老号-购买商城
谷歌play地区代改
Google Voice号码支持自助购买
谷歌锁区号购买商城]
美区VISA卡代开-可以用于aws,azure,FB,谷歌,亚马逊,速卖通,eBay,独立站,paypal等支付
如果您还有其他问题可以加我电报交流。
电报号:telegram:@tianmeiapp

打开,登录,看到如下界面

打开登录文档官方文档

allprojects {
    repositories {
        google()
        // If you're using a version of Gradle lower than 4.1, you must instead use:
        // maven {
        //     url 'https://maven.google.com'
        // }
    }
}

implementation 'com.google.android.gms:play-services-auth:19.0.0'

文档中有一个配置项,点击按照步骤进行配置(如果没有项目申请注册谷歌账号,就新建一个,有的话直接选择即可)

​这里可以根据需要选择,这里我会选择

​ 这里填写包名和SHA-1

ps:除了他建议的方法,还有另外一种获取SHA-1的方法。直接在 AS 中查看

填写后会提示两个字符串。我忘了它是什么没有截图。你可以复制它。还有一个按钮可以下载,然后就可以在里面查看了

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    private GoogleSignInClient mGoogleSignInClient;
    private static final String TAG = "MainActivity";
    
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == 1) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }
    
    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
            
            // Signed in successfully, show authenticated UI.
            Log.d(TAG, "handleSignInResult: " + account.getEmail());
            Log.d(TAG, "handleSignInResult: " + account.getId());
            Log.d(TAG, "handleSignInResult: " + account.getPhotoUrl());
            Log.d(TAG, "handleSignInResult: " + account.getIdToken());
            Log.d(TAG, "handleSignInResult: " + account.getGrantedScopes());

            Log.d(TAG, "handleSignInResult: " + account.getServerAuthCode());
        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
//            updateUI(null);
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //配置Google登录和GoogleSignInClient对象
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                                          .requestEmail()
                                          .requestId()
                                          .requestIdToken(this.getString(R.string.google_server_client_id))
                                          .requestProfile()
                                          .build();
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        //Google登录按钮
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        //自定义退出登录按钮
        findViewById(R.id.btn_1).setOnClickListener(this);
        
    }
    
    @Override

    protected void onStart() {
        super.onStart();
        //检查现有的登录用户
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        Log.d(TAG, "onStart: " + (account == null ? true : false));
        //检索登录用户的个人资料信息
        if (account != null) {
            String personName = account.getDisplayName();
            String personGivenName = account.getGivenName();
            String personFamilyName = account.getFamilyName();
            String personEmail = account.getEmail();
            String personId = account.getId();
            Uri personPhoto = account.getPhotoUrl();
            Log.d(TAG, "onStart: ---- personName is : " + personName +
                               "n personGivenName is : " + personGivenName +
                               "n personFamilyName is : " + personFamilyName +
                               "n personEmail is : " + personEmail +
                               "n personId is : " + personId +
                               "n personPhoto is : " + personPhoto);
            signOut();
        }
       
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();

                break;
            case R.id.btn_1:
                signOut();
                break;
        }
    }
    
    //登录
    private void signOut() {
        mGoogleSignInClient.signOut()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Log.d(TAG, "signOut: -----------------------------");
                        // ...
                    }
                });
    }
    
    //注销
    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, 1);
    }
    
    //断开帐户
    //强烈建议您为使用Google登录的用户提供断开其Google帐户与您的应用的连接的功能。
    // 如果用户删除其帐户,则必须删除您的应用程序从Google API获取的信息。
    private void revokeAccess() {

        mGoogleSignInClient.revokeAccess()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        // ...
                    }
                });
    }
}

ps:如果在这里登录账号,那么登录界面会闪一下。

这里使用的_id就是这个,我试过了,好像不行,不知道为什么

.(this.(R.._id)) 如果不加this,有些信息是无法获取的,比如.()申请注册谷歌账号,具体原因不明,实际运行得到的结果

xml文件

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

最后记得添加网络权限

<uses-permission android:name="android.permission.INTERNET"/>

一个简单的登录、注销功能就是这样,其他详细内容在官方文档中,如果想了解更多请移步官方文档

谷歌锁区号/谷歌邮箱老号-购买商城
谷歌play地区代改
Google Voice号码支持自助购买
谷歌锁区号购买商城]
美区VISA卡代开-可以用于aws,azure,FB,谷歌,亚马逊,速卖通,eBay,独立站,paypal等支付
如果您还有其他问题可以加我电报交流。
电报号:telegram:@tianmeiapp

原文链接:登录文档官方文档中有个配置项目,注销功能就这样,转载请注明来源!

0