gpt4 book ai didi

java - 如何使用 Firebase Auth 注销您的 Android Studio 帐户?

转载 作者:行者123 更新时间:2023-12-02 10:07:12 25 4
gpt4 key购买 nike

有一个带有 Firebase Auth 的应用程序,注销功能位于 MainActivity 中,但输出必须从 MenuActivity 实现,Intent 不起作用,因为执行了输入并初始化了用户值。需要在其他 Activity 中调用函数sign Out,我不知道该怎么做,请解释一下或告诉在哪里阅读。

主要 Activity

package com.example.ecohelp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.app.ProgressDialog;


import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends Activity implements
View.OnClickListener {
private static final String TAG = "EmailPassword";

private EditText mEmailField;
private EditText mPasswordField;
public ProgressDialog pd;

protected FirebaseAuth mAuth;


@Override
public void onCreate(Bundle savedInstanceState) {
FirebaseApp.initializeApp(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);








mEmailField = findViewById(R.id.fieldEmail);
mPasswordField = findViewById(R.id.fieldPassword);


findViewById(R.id.emailSignInButton).setOnClickListener(this);
findViewById(R.id.emailCreateAccountButton).setOnClickListener(this);



mAuth = FirebaseAuth.getInstance();
}


@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}

private void createAccount(String email, String password) {
Log.d(TAG, "Создание аккаунта" + email);
if (validateForm()) {
return;
}
pd = new ProgressDialog(this);

pd.show();
pd.setMessage("Регистрация");


mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
pd.hide();
Log.d(TAG, "Аккаунт успешно создан");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
pd.hide();
Log.w(TAG, "Ошибка создания аккаунта", task.getException());
Toast.makeText(MainActivity.this, "Ошибка создания аккаунта",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

});
}

private void signIn(String email, String password) {
Log.d(TAG, "Вход" + email);
if (validateForm()) {
return;
}
pd = new ProgressDialog(this);
pd.show();
pd.setMessage("Вход");

mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
pd.hide();
Log.d(TAG, "Вход через почту успешен");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
pd.hide();
Log.w(TAG, "Вход не вошелся", task.getException());
Toast.makeText(MainActivity.this, "Ошибка входа",
Toast.LENGTH_SHORT).show();
updateUI(null);
}


});
}


private boolean validateForm() {
boolean valid = true;

String email = mEmailField.getText().toString();
if (TextUtils.isEmpty(email)) {
mEmailField.setError("Пусто");
valid = false;
} else {
mEmailField.setError(null);
}

String password = mPasswordField.getText().toString();
if (TextUtils.isEmpty(password)) {
mPasswordField.setError("Пусто");
valid = false;
} else {
mPasswordField.setError(null);
}

return !valid;
}

protected void signOut() {
mAuth.signOut();
updateUI(null);
}

private void updateUI(FirebaseUser user) {
pd = new ProgressDialog(this);
pd.hide();
if (user != null) {
Intent intent = new Intent(MainActivity.this, Menu.class);
startActivity(intent);

}

}

@Override
public void onClick(View v) {

int i = v.getId();
if (i == R.id.emailCreateAccountButton) {
createAccount(mEmailField.getText().toString(), mPasswordField.getText().toString());
} else if (i == R.id.emailSignInButton) {
signIn(mEmailField.getText().toString(), mPasswordField.getText().toString());
}

}

菜单 Activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;



public class Menu extends Activity implements View.OnClickListener {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_menu);

}

public void onClick(View v) {
int i = v.getId();
if (i == R.id.map) {
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
} else if (i == R.id.signOut) {





}

}
}

MenuActivity.xml

<Button
android:id="@+id/map"
android:layout_width="164dp"
android:layout_height="61dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="@color/maps"
android:text="@string/map"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.451" />

<Button
android:id="@+id/signOut"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="@string/signOut"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

ActivityMain.xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TableRow
android:layout_width="422dp"
android:layout_height="359dp">

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aaaaaaaaaaa" />
</TableRow>

<LinearLayout
android:layout_width="434dp"
android:layout_height="225dp"
android:orientation="vertical">

<EditText
android:id="@+id/fieldEmail"
android:layout_width="match_parent"
android:layout_height="87dp"
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress" />

<EditText
android:id="@+id/fieldPassword"
android:layout_width="match_parent"
android:layout_height="84dp"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="69dp"
android:orientation="horizontal">

<Button
android:id="@+id/emailCreateAccountButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/createAccount" />

<Button
android:id="@+id/emailSignInButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/signIn" />
</LinearLayout>

</LinearLayout>

最佳答案

您可以从当前 Activity 注销,然后使用 Intent 导航到另一个 Activity 。

public void onClick(View v) {
int i = v.getId();
if (i == R.id.map) {
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
} else if (i == R.id.signOut) {

FirebaseAuth.getInstance.signOut();
Intent navigateIntent=new Intent(CurrentActivity.this,SecondActivity.class);
startActivity(navigateIntent);
//so that when you click the back button, you won't have to able to go back the previous activity
finish();
}

}
}

关于java - 如何使用 Firebase Auth 注销您的 Android Studio 帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com