How to maintain session in android?

后端 未结 10 2007
轻奢々
轻奢々 2020-11-29 19:35

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 unin

10条回答
  •  鱼传尺愫
    2020-11-29 19:54

    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 callback = new AccountManagerCallback() {
    
            @Override
            public void run(AccountManagerFuture 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;
        }
    }
    

提交回复
热议问题