Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.achal089.pestcontrol;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import java.util.HashMap;
- /**
- * Created by as on 5/7/2016.
- */
- public class UserSession {
- SharedPreferences pref;
- // Editor reference for Shared preferences
- SharedPreferences.Editor editor;
- // Context
- Context _context;
- // Shared preferences mode
- int PRIVATE_MODE = 0;
- // Shared preferences file name
- public static final String PREFER_NAME = "Register";
- // All Shared Preferences Keys
- public static final String IS_USER_LOGIN = "IsUserLoggedIn";
- // User name (make variable public to access from outside)
- public static final String Email = "Email";
- // Email address (make variable public to access from outside)
- public static final String Password = "Password";
- // Constructor
- public UserSession(Context context){
- this._context = context;
- pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
- editor = pref.edit();
- }
- //Create login session
- public void createUserLoginSession(String uName, String uPassword){
- // Storing login value as TRUE
- editor.putBoolean(IS_USER_LOGIN, true);
- // Storing name in preferences
- editor.putString(Email, uName);
- // Storing email in preferences
- editor.putString(Password, uPassword);
- // commit changes
- editor.commit();
- }
- /**
- * Check login method will check user login status
- * If false it will redirect user to login page
- * Else do anything
- * */
- 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;
- }
- /**
- * 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(Email, pref.getString(Email, null));
- // user email id
- user.put(Password, pref.getString(Password, null));
- // return user
- return user;
- }
- /**
- * Clear session details
- * */
- public void logoutUser(){
- // Clearing all user data from Shared Preferences
- editor.clear();
- editor.commit();
- // After logout redirect user to MainActivity
- Intent i = new Intent(_context, MainActivity.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);
- }
- // Check for login
- public boolean isUserLoggedIn(){
- return pref.getBoolean(IS_USER_LOGIN, false);
- }
- }
- package com.achal089.pestcontrol;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import java.util.HashMap;
- /**
- * Created by as on 5/7/2016.
- */
- public class UserSession {
- SharedPreferences pref;
- // Editor reference for Shared preferences
- SharedPreferences.Editor editor;
- // Context
- Context _context;
- // Shared preferences mode
- int PRIVATE_MODE = 0;
- // Shared preferences file name
- public static final String PREFER_NAME = "Register";
- // All Shared Preferences Keys
- public static final String IS_USER_LOGIN = "IsUserLoggedIn";
- // User name (make variable public to access from outside)
- public static final String Email = "Email";
- // Email address (make variable public to access from outside)
- public static final String Password = "Password";
- // Constructor
- public UserSession(Context context){
- this._context = context;
- pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
- editor = pref.edit();
- }
- //Create login session
- public void createUserLoginSession(String uName, String uPassword){
- // Storing login value as TRUE
- editor.putBoolean(IS_USER_LOGIN, true);
- // Storing name in preferences
- editor.putString(Email, uName);
- // Storing email in preferences
- editor.putString(Password, uPassword);
- // commit changes
- editor.commit();
- }
- /**
- * Check login method will check user login status
- * If false it will redirect user to login page
- * Else do anything
- * */
- 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;
- }
- /**
- * 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(Email, pref.getString(Email, null));
- // user email id
- user.put(Password, pref.getString(Password, null));
- // return user
- return user;
- }
- /**
- * Clear session details
- * */
- public void logoutUser(){
- // Clearing all user data from Shared Preferences
- editor.clear();
- editor.commit();
- // After logout redirect user to MainActivity
- Intent i = new Intent(_context, MainActivity.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);
- }
- // Check for login
- public boolean isUserLoggedIn(){
- return pref.getBoolean(IS_USER_LOGIN, false);
- }
- }
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class Register extends Activity {
- UserSession session;
- SharedPreferences sharedpreferences;
- TextView name;
- TextView address;
- TextView email;
- TextView phone;
- TextView occupation;
- TextView password;
- public static final String mypreference = "mypref";
- public static final String Name = "nameKey";
- public static final String Email = "emailKey";
- public static final String Phone = "Phone";
- public static final String Address = "Address";
- public static final String Occupation = "Occupation";
- public static final String Password = "Password";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- name = (TextView) findViewById(R.id.editText6);
- address = (TextView) findViewById(R.id.editText7);
- email = (TextView) findViewById(R.id.editText9);
- phone = (TextView) findViewById(R.id.editText8);
- password = (TextView)findViewById(R.id.editText11);
- occupation = (TextView)findViewById(R.id.editText10);
- sharedpreferences = getSharedPreferences(mypreference,
- Context.MODE_PRIVATE);
- if (sharedpreferences.contains(Name)) {
- name.setText(sharedpreferences.getString(Name, ""));
- }
- if (sharedpreferences.contains(Email)) {
- email.setText(sharedpreferences.getString(Email, ""));
- }
- if (sharedpreferences.contains(Occupation)) {
- occupation.setText(sharedpreferences.getString(Occupation, ""));
- }
- if (sharedpreferences.contains(Password)) {
- password.setText(sharedpreferences.getString(Password, ""));
- }
- if (sharedpreferences.contains(Address)) {
- address.setText(sharedpreferences.getString(Address, ""));
- }
- if (sharedpreferences.contains(Phone)) {
- phone.setText(sharedpreferences.getString(Phone, ""));
- }
- }
- public void Save(View view) {
- String n = name.getText().toString();
- String e = email.getText().toString();
- String w = phone.getText().toString();
- String m = address.getText().toString();
- String p = password.getText().toString();
- String v = occupation.getText().toString();
- SharedPreferences.Editor editor = sharedpreferences.edit();
- editor.putString(Name, n);
- editor.putString(Email, e);
- editor.putString(Phone, w);
- editor.putString(Address,m);
- editor.putString(Occupation,v);
- editor.putString(Password,p);
- editor.commit();
- Intent a = new Intent(Register.this, Login.class);
- startActivity(a);
- }
- public void Login (View view)
- {
- Intent a = new Intent(Register.this, Login.class);
- startActivity(a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement