package com.example.com.pro;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
public class Login extends AppCompatActivity {
CheckBox autoLogin;
//Boolean loginChecked;
SharedPreferences pref;
SharedPreferences.Editor editor;
String id;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText idInput = (EditText) findViewById(R.id.emailInput);
final EditText passwordInput = (EditText) findViewById(R.id.passwordInput);
autoLogin = (CheckBox) findViewById(R.id.checkbox);
final Button LoginButton = (Button) findViewById(R.id.loginButton);
final Button registerButton = (Button) findViewById(R.id.signupButton);
pref = getSharedPreferences("login", 0);
editor = pref.edit();
LoginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final String UserID = idInput.getText().toString();
final String UserPassword = passwordInput.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String UserID = jsonResponse.getString("UserID");
String UserPassword = jsonResponse.getString("UserPassword");
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setMessage("로그인에 성공하였습니다.").setPositiveButton("확인", null).create().show();
Intent intent = new Intent(Login.this, MapsActivity.class);
Login.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setMessage("로그인에 실패하였습니다").setNegativeButton("다시 시도",null).create().show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(UserID, UserPassword, responseListener);
RequestQueue queue = Volley.newRequestQueue(Login.this);
queue.add(loginRequest);
}
});
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Login.this, Signup.class);
startActivity(i);
}
});
// if autoLogin checked, get input
if (pref.getBoolean("autoLogin", false)) {
idInput.setText(pref.getString("UserID", ""));
passwordInput.setText(pref.getString("UserPassword", ""));
autoLogin.setChecked(true);
// goto mainActivity
}
// set checkBoxListener
autoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
id = idInput.getText().toString();
password = passwordInput.getText().toString();
editor.putString("UserID", id);
editor.putString("UserPassword", password);
editor.putBoolean("autoLogin", true);
editor.commit();
}
else
{
// if unChecked, removeAll
editor.remove("id");
editor.remove("pw");
editor.remove("autoLogin");
editor.clear();
editor.commit();
}
}
});
}
소스코드입니다...