Login.java
SessionManager session;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
session = new SessionManager(getApplicationContext());
if(sukses==1){
session.createLoginSession(id_alumni,nama_alumni);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Login.this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Registered", true);
editor.putString("id_alumni", id_alumni);
editor.putString("nama_alumni", nama_alumni);
editor.putString("alamat", alamat);
editor.putString("status", status);
editor.apply();
Intent i = new Intent(getApplicationContext(),Menu_utama.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
Toast.makeText(Login.this, "Login Berhasil....", Toast.LENGTH_SHORT).show();
}
else{gagal("Login"); }
new AlertDialog.Builder(this)
.setTitle("Invalid")
.setMessage("Please complete "+item)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}})
.show();
}
public void gagal(String item){
new AlertDialog.Builder(this)
.setTitle("Login Failed")
.setMessage("Please Check your username/password")
.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);
}
========================
SessionManager.java
package com.example.apps_jasasepatu;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.util.HashMap;
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_KODE = "kode";
// Email address (make variable public to access from outside)
public static final String KEY_NAMA= "nama";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String kode, String nama){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_KODE, kode);
// Storing email in pref
editor.putString(KEY_NAMA, nama);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_KODE, pref.getString(KEY_KODE, null));
// user email id
user.put(KEY_NAMA, pref.getString(KEY_NAMA, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logout(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
===============
MenuUtama.java
SessionManager session;
session = new SessionManager(getApplicationContext());
session.checkLogin();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(MenuUtama.this);
Boolean Registered = sharedPref.getBoolean("Registered", false);
if (!Registered) {
finish();
} else {
cid = sharedPref.getString("cid", "");
cnama = sharedPref.getString("cnama", "");
cket = sharedPref.getString("cket", "");
}
Sistem akan terus merecord Session
Sehigga cukup 1x login saja ...sistem akan ingat terus, kecuali menekan tombol Logout....
Tidak ada komentar:
Posting Komentar