Page 379 - MDP2022-2
P. 379
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class LoginActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private DatabaseReference mDatabaseRef;
private EditText mEtEmail, mEtPwd;
private int touch_state = 0;
String email, pass;
String autoEmail, autoPass;
SharedPreferences auto;
SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//타이틀바 없애기
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
mFirebaseAuth = FirebaseAuth.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("prototype");
mEtEmail = findViewById(R.id.email);
mEtPwd = findViewById(R.id.password);
CheckBox autoLoginCheck = findViewById(R.id.autoLogin);
auto = getSharedPreferences("autoLogin", Activity.MODE_PRIVATE);
autoEmail = auto.getString("inputEmail",null);
autoPass = auto.getString("inputPass",null);
Button btn_login = findViewById(R.id.login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(autoEmail == null && autoPass == null) {
String strEmail = mEtEmail.getText().toString();
String strPwd = mEtPwd.getText().toString();
if (strEmail.equals("") && strPwd.equals("")) {
Toast.makeText(LoginActivity.this, "이메일과 비밀번호를 입력해주세요.", Toast.LENGTH_SHORT).show();
} else if (strEmail.equals("") == false && strPwd.equals("")) {
Toast.makeText(LoginActivity.this, "비밀번호를 입력해주세요.", Toast.LENGTH_SHORT).show();
} else if (strEmail.equals("") && strPwd.equals("") == false) {
Toast.makeText(LoginActivity.this, "이메일을 입력해주세요.", Toast.LENGTH_SHORT).show();
}
else {
email = mEtEmail.getText().toString();
pass = mEtPwd.getText().toString();
mFirebaseAuth.signInWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(LoginActivity.this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
if(autoLoginCheck.isChecked() == true) {
SharedPreferences.Editor autoLogin = auto.edit();
autoLogin.putString("inputEmail", email);
autoLogin.putString("inputPass", pass);
autoLogin.apply();
}
Toast.makeText(LoginActivity.this, "로그인에 성공했습니다.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, ComActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
} else {