简体   繁体   English

如何使用Firebase Auth登出您的Android Studio帐户?

[英]How to log out of your android studio account using Firebase Auth?

There is an application with Firebase Auth, the sign out function lies in the MainActivity, but the output must be implemented from the MenuActivity, Intent does not work because the input is executed and the user value is initialized. 有一个具有Firebase Auth的应用程序,注销功能位于MainActivity中,但是必须从MenuActivity实现输出,Intent无法正常工作,因为已执行输入并初始化了用户值。 Need to call the function sign Out of other activities, I don't know how to do it, please explain or tell where to read about it. 需要在其他活动之外调用该函数的sign,我不知道该怎么做,请解释或告诉在哪里阅读。

MainActivity 主要活动

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());
    }

}

MenuActivity MenuActivity

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 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 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>

You can logout From current activity and then Use Intent to navigate into another 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();
    }

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Android中的谷歌帐户注销? - How to log out from google account in android ? Firebase 谷歌验证,退出再登录会用上次登录的账号登录 - Firebase Google auth, signing out and logging in again will log in with the last signed account Firebase auth错误android工作室 - Firebase auth error android studio Firebase 实时数据库注销 Google 帐户 - Firebase Realtime Database Log Out Google Account 在Android Studio / Firebase中创建帐户时出错 - Error in create account in Android studio / Firebase Android Studio / JAVA:如何使用“日志”跟踪 android studio 中的错误 - Android Studio / JAVA: How to trace error at android studio using "log" 如何使用Firebase监听Android Studio中的更改 - How to listen for changes in android studio using firebase 如何在Android中使用Firebase获取Nest帐户上设备的简单列表? - How to get simple list of devices on a Nest account using Firebase in Android? 如何在 Firebase 中退出我的 Google 帐户,这样当我再次登录时,我可以选择我想登录的帐户? - How do I log out of my Google Account in Firebase such when I log in again, I can choose the account I would like to sign in with? 在手机上使用Android Studio时,创建帐户无法与Firebase一起使用 - When using android studio on my phone, creating an account does not work with firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM