How to Maintain Session in Android

How to maintain session in android?

Make one class for your SharedPreferences

public class Session {

private SharedPreferences prefs;

public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}

public void setusename(String usename) {
prefs.edit().putString("usename", usename).commit();
}

public String getusename() {
String usename = prefs.getString("usename","");
return usename;
}
}

Now after making this class when you want to use it, use like this: make object of this class like

private Session session;//global variable 
session = new Session(cntx); //in oncreate
//and now we set sharedpreference then use this like

session.setusename("USERNAME");

now whenever you want to get the username then same work is to be done for session object and call this

session.getusename();

Do same for password

Android save user session

Yes, it is. You just have to store the session using SharedPreferences.

The idea is, after you login, I guess you return some kind of session id or whatever. You just need to store it. Then, in subsequents executions, you just fetch that session before doing any request.

SharedPreferences preferences = 
getSharedPreferences("com.blabla.yourapp", Context.MODE_PRIVATE);

//Save it
preferences.edit().putString("session", <yoursessiontoken>).commit();

//Fetch it
// The second parameter is the default value.
preferences.getString("session", "");

Anyway is better that you read the documentation to really understand what you are doing.

Maintaining session in android ( application stay authenticated on the server side)

Finally I solved the issue of session handling in Android.
Android cant handle the session itself(which a simple browser can) so we have to handle it explicitly.
I changed the code for http connection a bit.
Created an instance of DefaultHttpClient in the first Activity when connection established.

public static DefaultHttpClient httpClient;

For the first time connection,I did the following:

URL url=new URL(urlToHit);
LoginScreen.httpClient = new DefaultHttpClient(); //LoginScreen is the name of the current Activity

HttpPost httppost = new HttpPost(url.toString());
HttpResponse response = LoginScreen.httpClient.execute(httppost);

xr.parse(new InputSource(url.openStream())); //SAX parsing

Now for all further connections I used the same httpClient
For example in the next activity:

URL url=new URL(urlToHit);

HttpPost httppost = new HttpPost(url.toString());
HttpResponse response = LoginScreen.httpClient.execute(httppost);

// Log.v("response code",""+response.getStatusLine().getStatusCode());

// Get hold of the response entity
HttpEntity entity = response.getEntity();

InputStream instream = null;

if (entity != null) {
instream = entity.getContent();
}
xr.parse(new InputSource(instream)); //SAX parsing

Hope this will help you all too to solve session issue in Android.

How should I maintain session in android and what should I store(hashed passwords or something else) to maintain logged in state

you can create a shared preference class to store your small data like user info and you can create a login session like this :-

your preference class :-

public class AppPrefrences {

private static SharedPreferences mPrefs;
private static SharedPreferences.Editor mPrefsEditor;

public static boolean isUserLoggedOut(Context ctx) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
return mPrefs.getBoolean("id_logged_in", true);
}

public static void setUserLoggedOut(Context ctx, Boolean value) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putBoolean("id_logged_in", value);
mPrefsEditor.commit();
}
}

and when you logged in your app you can set the session of your logged in session like this where your login method seccess:-

AppPreference.setUserLoggedOut(CompleteProfileActivity.this, false);

and second time when you open the app you can set a check condition on your splash screen like this:-

if (isUserLoggedOut(StartActivity.this)) {
startActivity(new Intent(StartActivity.this, LoginActivity.class));
finish();
} else {
startActivity(new Intent(StartActivity.this, MainActivity.class));
finish();
}

how to maintain session using Shared Preferences

You are passing the wrong key to shared Pref because of that you are getting null.

Following is the updated code of yours which will give you the stored email address:

      SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE);

e = sharedpreferences.getString(main.email,"");
String url = "url?COMP_REQ_ID=" + title + "&StuEmail=" + e;

Happy coding !!!

Maintaining user session in android using Shared prference

check shared pref exist or not , in your splash screen or in your launcher screen.

  if (sharedpreferences.contains("yourkey")) {
//get yourkey value and load screen
}

With the help of this perform action to show home screen or login page.

       public class Splash_Screen extends FragmentActivity{
private static int SPLASH_TIME_OUT = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash_screen);
SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE);

//change here
if (sharedpreferences.contains(main.email)) {
Intent i = new Intent(Splash_Screen.this, MainActivity.class);
startActivity(i);
}
else{

remove this code from else

  //  Intent i = new Intent(Splash_Screen.this,Splash_Screen.class);
// startActivity(i);
//get yourkey value and load screen
}
new Handler().postDelayed(new Runnable() {

/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/

@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(Splash_Screen.this, main.class);
startActivity(i);


Related Topics



Leave a reply



Submit