import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class login extends Activity {
EditText txtpassword,txtusername;
String ip="";
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
String kode_customer,nama_customer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ip=jsonParser.getIP();
txtusername=(EditText)findViewById(R.id.username);
txtpassword=(EditText)findViewById(R.id.password);
Button btnLogin= (Button) findViewById(R.id.btnlogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user=txtusername.getText().toString();
String pass=txtpassword.getText().toString();
if(user.length()<1){lengkapi("Username");}
else if(pass.length()<1){lengkapi("Password");}
else{
Intent put= new Intent(getBaseContext(), Menuutama.class);
startActivity(put);
finish();
}
}
});
TextView txtBuat=(TextView)findViewById(R.id.buat);
txtBuat.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(login.this,Registrasi.class);
startActivity(i);
}});
}
public void gagal(){
new AlertDialog.Builder(this)
.setTitle("Gagal Login")
.setMessage("Silakan Cek Account Anda Kembali")
.setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void sukses(String item,String ex){
new AlertDialog.Builder(this)
.setTitle("Sukses "+ex)
.setMessage(ex+" data "+item+" Berhasil")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
// Intent i = new Intent(login.this,MainActivity.class);
// i.putExtra("xmlbio", xmlbio);
// startActivity(i);
// finish();
}})
.show();
}
class ceklogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(login.this);
pDialog.setMessage("Proses Login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
int sukses;
try {
String username=txtusername.getText().toString().trim();
String password=txtpassword.getText().toString().trim();
List<NameValuePair> myparams = new ArrayList<NameValuePair>();
myparams.add(new BasicNameValuePair("username", username));
myparams.add(new BasicNameValuePair("password", password));
String url=ip+"customer/customer_login.php";
Log.v("detail",url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", myparams);
Log.d("detail", json.toString());
sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array
final JSONObject myJSON = myObj.getJSONObject(0);
runOnUiThread(new Runnable() {
public void run() {
try {
kode_customer=myJSON.getString("kode_customer");
nama_customer=myJSON.getString("nama_customer");
}
catch (JSONException e) {e.printStackTrace();}
}});
}
else{
// jika id tidak ditemukan
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@SuppressLint("NewApi")
protected void onPostExecute(String file_url) {pDialog.dismiss();
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(login.this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Registered", true);
editor.putString("kode_customer", kode_customer);
editor.putString("nama_customer", nama_customer);
editor.apply();
Intent i = new Intent(getApplicationContext(),Menuutama.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
public void lengkapi(String item){
new AlertDialog.Builder(this)
.setTitle("Lengkapi Data")
.setMessage("Silakan lengkapi data "+item)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void gagal(String item){
new AlertDialog.Builder(this)
.setTitle("Gagal Login")
.setMessage("Login "+item+" ,, Gagal")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
+++++++++++++++++++++++++
Ambil session :
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(customer.this);
Boolean Registered = sharedPref.getBoolean("Registered", false);
if (!Registered){
finish();
}else {
kode_customer = sharedPref.getString("kode_customer", "");
nama_customer = sharedPref.getString("nama_customer", "");
}
Tidak ada komentar:
Posting Komentar