我是一个初学者程序员,但是有一个问题。如何在“活动”菜单中使用Google注销功能?我在这个问题上坐了很长时间,以某种方式我无法解决它。活动菜单用于显示用户信息。
主要活动
public class MainActivity extends AppCompatActivity {
SignInButton signInButton;
public GoogleSignInClient mGoogleSignInClient;
private final static int RC_SIGN_IN = 123;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
signInButton=(SignInButton)findViewById(R.id.sign_in_button);
createRequest();
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser user = mAuth.getCurrentUser();
if(user!=null){
Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
startActivity(intent);
}
}
private void createRequest() {
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
// ...
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}
菜单活动
logoutBtn是一个按钮,单击后将用于从Google注销用户并将其咀嚼到MainActivity,以便他可以再次选择要登录到我的应用程序的帐户
Button logoutBtn;
TextView userName;
ImageView profileImage;
ImageButton play_button, multi_button, statistic_button, option_button;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
logoutBtn = (Button) findViewById(R.id.logoutBtn);
userName = (TextView) findViewById(R.id.name);
profileImage = (ImageView) findViewById(R.id.profileImage);
GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(signInAccount != null) {
userName.setText(signInAccount.getDisplayName());
//mail.setText(signInAccount.getEmail());
}
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
});
}'
问题来源:Stack Overflow
您可以方便地使用它进行注销(详细信息在这里https://firebase.google.com/docs/auth/android/firebaseui):
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener {
// ...
}
对于片段,“ this”必须替换为requireContext()。 回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。