==========login page
LoginPreference loginpreference = new LoginPreference(getApplicationContext());
loginpreference.set_login_value(user_id, user_name,P_referralcode);
===========create class
package com.example.mactech.spinevent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import java.util.HashMap;
/** * Created by mactech on 5/17/2018. */public class LoginPreference {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
// All Shared Preferences Keys private static final String IS_USER_LOGIN = "IsUserLoggedIn";
private static final String PREFER_NAME = "LoginPreference";
public static String user_id = "u_id";
public static String user_name = "u_name";
public static String P_referralcode = "user_referal_generate";
public LoginPreference(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void clear() {
editor.clear();
editor.commit();
}
public void set_login_value(String u_id ,String u_name,String user_referal_generate ) {
// Storing login value as TRUE
editor.putBoolean(IS_USER_LOGIN, true);
editor.putString(user_id, u_id);
editor.putString(user_name, u_name);
editor.putString(P_referralcode, user_referal_generate);
editor.commit();
}
/** * Get stored session data * */ public HashMap<String, String> getUserDetails(){
//Use hashmap to store user credentials HashMap<String, String> user = new HashMap<String, String>();
// user name user.put(user_id, pref.getString(user_id, null));
user.put(user_name, pref.getString(user_name, null));
user.put(P_referralcode, pref.getString(P_referralcode, null));
// return user return user;
}
// Check for login public boolean isUserLoggedIn(){
return pref.getBoolean(IS_USER_LOGIN, false);
}
public boolean checkLogin() {
// Check login status if(!this.isUserLoggedIn()){
// user is not logged in redirect him to Login Activity Intent i = new Intent(_context, Login.class);
// Closing all the Activities from stack 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);
return true;
}
return false;
}
public void logoutUser() {
// Clearing all user data from Shared Preferences editor.clear();
editor.commit();
// After logout redirect user to Login Activity Intent i = new Intent(_context, Login.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Staring Login Activity _context.startActivity(i);
}
}
================get data
session = new LoginPreference(getApplicationContext());
if (session.checkLogin())
finish();
HashMap<String, String> user = session.getUserDetails();
id = user.get(LoginPreference.user_id);
username = user.get(LoginPreference.user_name);
P_referralcode = user.get(LoginPreference.P_referralcode);
ActionBar mActionbar = getSupportActionBar();
mActionbar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar_activity_dashbord)));
Comments
Post a Comment