Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain session in android?

Tags:

Can anybody tell me how to maintain session for a user login. For example when the user sign- in to an application they have to be signed in unless the user logouts or uninstall the application similar to gmail in android.

like image 595
june Avatar asked Dec 19 '13 10:12

june


People also ask

How is session maintained?

Sessions are maintained automatically by a session cookie that is sent to the client when the session is first created. The session cookie contains the session ID, which identifies the client to the browser on each successive interaction.

How do I keep logged in on Android?

getUserName(MainActivity. this). length() == 0) { // call Login Activity } else { // Stay at the current activity. } In Login activity if user login successful then set UserName using setUserName() function.

What is meant by session in Android?

The definition of session A session is a period of time wherein a user interacts with an app. Usually triggered by the opening of an app, a session records the length and frequency of app use to show developers, marketers and product managers how much time users spend within an app.


2 Answers

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

like image 137
Bhanu Sharma Avatar answered Oct 13 '22 13:10

Bhanu Sharma


You can achieve this by using AccountManager.

Code Sample

// method to add account.. private void addAccount(String username, String password) {     AccountManager accnt_manager = AccountManager             .get(getApplicationContext());      Account[] accounts = accnt_manager             .getAccountsByType(getString(R.string.account_type)); // account name identifier.      if (accounts.length > 0) {         return;     }      final Account account = new Account(username,             getString(R.string.account_type));      accnt_manager.addAccountExplicitly(account, password, null);      final Intent intent = new Intent();     intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);     intent.putExtra(AccountManager.KEY_PASSWORD, password);     intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,             getString(R.string.account_type));     // intent.putExtra(AccountManager.KEY_AUTH_TOKEN_LABEL,     // PARAM_AUTHTOKEN_TYPE);     intent.putExtra(AccountManager.KEY_AUTHTOKEN, "token");     this.setAccountAuthenticatorResult(intent.getExtras());     this.setResult(RESULT_OK, intent);     this.finish(); }  // method to retrieve account. private boolean validateAccount() {     AccountManagerCallback<Bundle> callback = new AccountManagerCallback<Bundle>() {          @Override         public void run(AccountManagerFuture<Bundle> arg0) {             Log.e("calback", "msg");              try {                 Bundle b = arg0.getResult();                 if (b.getBoolean(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE)) {                     //User account exists!!..                 }                 } catch (OperationCanceledException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (AuthenticatorException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }     };      AccountManager accnt_manager = AccountManager             .get(getApplicationContext());      Account[] accounts = accnt_manager             .getAccountsByType(getString(R.string.account_type));      if (accounts.length <= 0) {         return false;     } else {         loginNameVal = accounts[0].name;         loginPswdVal = accnt_manager.getPassword(accounts[0]);         return true;     } } 
like image 43
nikvs Avatar answered Oct 13 '22 11:10

nikvs