有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在登录后在不同的活动中重定向2个不同的用户?

我正在创建一个有2个不同用户的应用程序,我想在他们登录后在不同的活动中重定向他们。每次您在应用程序中注册或注册时。它会问你是学生还是老师。注册后。您填写的所有信息都存储在firebase数据库中,包括姓名、电话、帐户类型和用户Id。这是我的代码。我希望这里有人能帮助我,因为这是我们的论文项目

这是我的后勤工作。java代码

package com.example.vincedamsel.firebase;

import 安卓.app.Dialog;
import 安卓.app.ProgressDialog;
import 安卓.content.Context;
import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.content.SharedPreferences;
import 安卓.graphics.Color;
import 安卓.graphics.Typeface;
import 安卓.support.annotation.NonNull;
import 安卓.support.design.widget.TextInputLayout;
import 安卓.support.v7.app.AlertDialog;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Gravity;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.Button;
import 安卓.widget.Spinner;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import com.google.安卓.gms.tasks.OnCompleteListener;
import com.google.安卓.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

@SuppressWarnings("ConstantConditions")
    public class LoginActivity extends AppCompatActivity {

    private TextInputLayout emailField;
    private TextInputLayout passwordField;
    private View btnLogin,btnRegister,btnForgotPassword;
    private ProgressDialog progressDialog;
    private FirebaseAuth auth;
    private Spinner spinAccountType;
    private DatabaseReference ref;
    private FirebaseAuth.AuthStateListener mAuthListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        emailField = (TextInputLayout) findViewById(R.id.email_field);
        passwordField = (TextInputLayout) findViewById(R.id.password_field);
        btnLogin = findViewById(R.id.login);
        btnRegister = findViewById(R.id.register);
        btnForgotPassword = findViewById(R.id.forgotpassword);

        //Get Firebase auth instance
        auth = FirebaseAuth.getInstance();

        emailField.requestFocus();

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder2=new AlertDialog.Builder(LoginActivity.this);
                builder2.setTitle("Register");
                builder2.setMessage("Sign up for Attendance");
                builder2.setPositiveButton("       Professor       ",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final String type2="Professor";
                        Intent intent = new Intent(LoginActivity.this, SignupActivity.class);
                        intent.putExtra("Type",type2);
                        startActivity(intent);
                    }
                });

                builder2.setNegativeButton("        Student        ", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final String type1="Student";
                        Intent intent = new Intent(LoginActivity.this, SignupActivity.class);
                        intent.putExtra("Type",type1);
                        startActivity(intent);
                    }
                });
                AlertDialog dialog = builder2.show();
                TextView messageView = (TextView)dialog.findViewById(安卓.R.id.message);
                messageView.setGravity(Gravity.CENTER);
                messageView.setTextSize(17);
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Utils.hasText(emailField)) {
                    Utils.showToast(LoginActivity.this, "Please input your email");
                } else if (!Utils.hasText(passwordField)) {
                    Utils.showToast(LoginActivity.this, "Please input your password");
                } else {
                    //requesting Firebase server
                    showProcessDialog();
                    authenticateUser(Utils.getText(emailField), Utils.getText(passwordField));

                }
            }
        });

        btnForgotPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ResetPasswordActivity.class);
                startActivity(intent);
            }
        });
    }

    private void authenticateUser(String email, String password) {
        auth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        // When login failed
                        if (!task.isSuccessful()) {
                            progressDialog.dismiss();
                            Utils.showToast(LoginActivity.this, "Login error!");
                        } else {
                            //When login successful, redirect user to main activity
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            progressDialog.dismiss();
                            finish();
                        }
                    }
                });

    }

    private void showProcessDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Login");
        progressDialog.setMessage("Logging in Firebase server...");
        progressDialog.show();
    }
}

this is my Database image

this is my Rules image


共 (3) 个答案

  1. # 1 楼答案

    如果要重定向到不同的活动,需要用户类型,当用户登录成功时,需要检索用户类型并重定向到特定活动,例如:

     String USER_TYPE_1 = "student";
     String USER_TYPE_2 = "profesor";
    
     /**After login success you add logic:**/
    
     Intent intent = null;
     if(user.equals(USER_TYPE_1)){
        intent = new Intent(this, Activity1.class);
     }else if(user.equals(USER_TYPE_2)){
        intent = new Intent(this, Activity2.class);
     }
    
     if(intent!=null){
        startActivity(intent);
     }
    

    请记住,用户类型需要检索数据库或存储器,无论您想要什么

  2. # 2 楼答案

    您可以使用if语句检查帐户类型,并使用两个意图将用户引导到活动

  3. # 3 楼答案

    您需要将登录类型的状态保存到共享首选项中(例如:如果学生已登录,则必须将状态保存为学生..如果是教授,则必须将其保存为教授)。。在注册时。。再次登录时,您必须从共享首选项获取状态,以便调用活动