Advertisement
kzwhkzw

login session

Aug 29th, 2017
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. package com.example.lbstempatles;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.app.ProgressDialog;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.os.AsyncTask;
  11. import android.os.Bundle;
  12. import android.preference.PreferenceManager;
  13. import android.util.Log;
  14. import android.view.KeyEvent;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.TextView;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. import org.apache.http.NameValuePair;
  24. import org.apache.http.message.BasicNameValuePair;
  25. import org.json.JSONArray;
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29.  
  30. public class Login extends Activity {
  31.    
  32.    
  33.      EditText txtusername,txtpassword;
  34.      String ip="";
  35.      int sukses;
  36.      private ProgressDialog pDialog;
  37.         JSONParser jsonParser = new JSONParser();
  38.  
  39.         private static final String TAG_SUKSES = "sukses";
  40.         private static final String TAG_record = "record";
  41.        
  42.         String kode_admin="",nama_admin,email;
  43.        
  44.     protected void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.login);
  47.         ip=jsonParser.getIP();
  48.        
  49.        
  50.         txtusername=(EditText)findViewById(R.id.txtusername);
  51.         txtpassword=(EditText)findViewById(R.id.txtpassword);
  52.  
  53.  
  54.         Button btnLogin= (Button) findViewById(R.id.btnlogin);
  55.         btnLogin.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View v) {
  58.                 String user=txtusername.getText().toString();
  59.                 String pass=txtpassword.getText().toString();
  60.                 if(user.length()<1){lengkapi("Username");}
  61.                 else if(pass.length()<1){lengkapi("Password");}
  62.                 else{
  63.                     new ceklogin().execute();
  64.                          }
  65.                        
  66.                   }
  67.  
  68.         });
  69.  
  70.  
  71.         TextView txtBuat=(TextView)findViewById(R.id.btnbantuan);
  72.         txtBuat.setOnClickListener(new View.OnClickListener() {
  73.         public void onClick(View arg0) {
  74.             Intent i = new Intent(Login.this,Bantuan.class);
  75.             startActivity(i);
  76.         }});
  77.        
  78.        
  79.  
  80.         TextView txtLupa=(TextView)findViewById(R.id.btnkeluar);
  81.         txtLupa.setOnClickListener(new View.OnClickListener() {
  82.         public void onClick(View arg0) {
  83.                 finish();
  84.         }});
  85.  
  86.     }
  87.     public void gagal(){
  88.         new AlertDialog.Builder(this)
  89.                 .setTitle("Gagal Login")
  90.                 .setMessage("Silakan Cek Account Anda Kembali")
  91.                 .setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
  92.                     public void onClick(DialogInterface dlg, int sumthin) {
  93.  
  94.                     }})
  95.                 .show();
  96.     }
  97.  
  98.     public void sukses(String item,String ex){
  99.         new AlertDialog.Builder(this)
  100.                 .setTitle("Sukses "+ex)
  101.                 .setMessage(ex+" data "+item+" Berhasil")
  102.                 .setNeutralButton("OK", new DialogInterface.OnClickListener() {
  103.                     public void onClick(DialogInterface dlg, int sumthin) {
  104. //                        Intent i = new Intent(login.this,MainActivity.class);
  105. //                        i.putExtra("xmlbio", xmlbio);
  106. //                        startActivity(i);
  107.                         // finish();
  108.                     }})
  109.                 .show();
  110.     }
  111.  
  112.  
  113.  
  114.     class ceklogin extends AsyncTask<String, String, String> {
  115.         @Override
  116.         protected void onPreExecute() {
  117.             super.onPreExecute();
  118.             pDialog = new ProgressDialog(Login.this);
  119.             pDialog.setMessage("Proses Login...");
  120.             pDialog.setIndeterminate(false);
  121.             pDialog.setCancelable(true);
  122.             pDialog.show();
  123.         }
  124.         protected String doInBackground(String... params) {
  125.          
  126.             try {
  127.                 String  username=txtusername.getText().toString().trim();
  128.                 String  password=txtpassword.getText().toString().trim();
  129.                
  130.                 List<NameValuePair> myparams = new ArrayList<NameValuePair>();
  131.                 myparams.add(new BasicNameValuePair("username", username));
  132.                 myparams.add(new BasicNameValuePair("password", password));
  133.  
  134.                 String url=ip+"admin/admin_login.php";
  135.                 Log.v("detail",url);
  136.                 JSONObject json = jsonParser.makeHttpRequest(url, "GET", myparams);
  137.                 Log.d("detail", json.toString());
  138.                 sukses = json.getInt(TAG_SUKSES);
  139.                 if (sukses == 1) {
  140.                     JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array
  141.                     final JSONObject myJSON = myObj.getJSONObject(0);
  142.                     runOnUiThread(new Runnable() {
  143.                         public void run() {
  144.                             try {
  145.  
  146.                                 kode_admin=myJSON.getString("kode_admin");
  147.                                 nama_admin=myJSON.getString("nama_admin");
  148.                                 email=myJSON.getString("email");
  149.                                     }
  150.                             catch (JSONException e) {e.printStackTrace();}
  151.                         }});
  152.                 }
  153.              
  154.             } catch (JSONException e) {
  155.                 e.printStackTrace();
  156.             }
  157.             return null;
  158.         }
  159.         @SuppressLint("NewApi")
  160.         protected void onPostExecute(String file_url) {
  161.            
  162.             pDialog.dismiss();
  163.             Log.v("SUKSES",kode_admin);
  164.  
  165.             if(sukses==1){
  166.                 final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Login.this);
  167.                 SharedPreferences.Editor editor = sharedPref.edit();
  168.                 editor.putBoolean("Registered", true);
  169.                 editor.putString("kode_admin", kode_admin);
  170.                 editor.putString("nama_admin", nama_admin);
  171.                 editor.putString("email", email);
  172.                 editor.apply();
  173.                 Intent i = new Intent(getApplicationContext(),Menu_admin.class);
  174.                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  175.                 startActivity(i);
  176.                 finish();
  177.             }
  178.             else{
  179.                 gagal("Login");
  180.             }
  181.         }
  182.     }
  183.  
  184.    
  185.  
  186.         public void lengkapi(String item){
  187.             new AlertDialog.Builder(this)
  188.             .setTitle("Lengkapi Data")
  189.             .setMessage("Silakan lengkapi data "+item)
  190.             .setNeutralButton("OK", new DialogInterface.OnClickListener() {
  191.                 public void onClick(DialogInterface dlg, int sumthin) {
  192.                 }})
  193.             .show();
  194.         }
  195.  
  196.    
  197.  
  198.       public void gagal(String item){
  199.             new AlertDialog.Builder(this)
  200.             .setTitle("Gagal Login")
  201.             .setMessage("Login "+item+" ,, Gagal")
  202.             .setNeutralButton("OK", new DialogInterface.OnClickListener() {
  203.                 public void onClick(DialogInterface dlg, int sumthin) {
  204.                 }})
  205.             .show();
  206.         }
  207.  
  208.  
  209.       public boolean onKeyDown(int keyCode, KeyEvent event) {
  210.             if (keyCode == KeyEvent.KEYCODE_BACK) {
  211.                 finish();
  212.                     return true;
  213.             }
  214.         return super.onKeyDown(keyCode, event);
  215.     }  
  216.        
  217.     }  
  218.  
  219. +++++++++++++++++++++++++
  220. Ambil session :
  221.  
  222.   SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(customer.this);
  223.    Boolean Registered = sharedPref.getBoolean("Registered", false);
  224.          if (!Registered){
  225.              finish();
  226.          }else {
  227.           kode_admin= sharedPref.getString("kode_admin", "");
  228.           nama_admin = sharedPref.getString("nama_admin", "");
  229.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement