SessionManagement.java
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SessionManagement {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "SharedPreferences_Name";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_UserName = "UserName";
public static final String KEY_Password = "Password";
@SuppressLint("CommitPrefEdits")
public SessionManagement(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
*
Create login session
*
*/
public void createLoginSession(String UserName,
String Password) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_UserName, UserName);
editor.putString(KEY_Password, Password);
editor.commit();
}
public void checkLogin() {
if (!this.isLoggedIn()) {
Intent i = new Intent(_context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user
= new HashMap<String, String>();
user.put(KEY_UserName, pref.getString(KEY_UserName, null));
user.put(KEY_Password, pref.getString(KEY_Password, null));
return user;
}
public void logoutUser() {
editor.clear();
editor.commit();
Intent intent;
intent = new Intent(_context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(intent);
}
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
MainActivity.java
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtMsg;
private EditText edtName, edtPassword;
private Button BtnLogin, BtnLogout;
public SessionManagement session;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
session = new
SessionManagement(getApplicationContext());
txtMsg = (TextView) findViewById(R.id.txtMsg);
edtName = (EditText) findViewById(R.id.edtName);
edtPassword = (EditText) findViewById(R.id.edtPassword);
BtnLogin = (Button) findViewById(R.id.BtnLogin);
BtnLogout = (Button) findViewById(R.id.BtnLogout);
BtnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
session.createLoginSession(edtName.getText().toString(),
edtName.getText().toString());
BtnLogout.setVisibility(View.VISIBLE);
BtnLogin.setVisibility(View.GONE);
edtName.setVisibility(View.GONE);
edtPassword.setVisibility(View.GONE);
session.checkLogin();
// get user data from session
HashMap<String,
String> user = session.getUserDetails();
txtMsg.setText(user.get(SessionManagement.KEY_UserName));
}
});
BtnLogout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
session.logoutUser();
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1.5dp"
>
<TextView
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="3dp"
android:text="@string/Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1.5dp"
android:ems="10"
android:hint="@string/Name"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1.5dp"
android:ems="10"
android:hint="@string/Password"
android:inputType="textPassword"
/>
<Button
android:id="@+id/BtnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1.5dp"
android:text="@string/Login"
/>
<Button
android:id="@+id/BtnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1.5dp"
android:text="@string/Logout"
android:visibility="gone"
/>
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<string name="app_name">SharedPreferences</string>
<string name="Logout">Logout</string>
<string name="Login">Login</string>
<string name="Name">Name</string>
<string name="Hello">Hello</string>
<string name="Password">Password</string>
</resources>
No comments:
Post a Comment