How to maintain session in android?

后端 未结 10 2008
轻奢々
轻奢々 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 20:02

    Use SharedPreferences. Code to save a value to sharedpreferences:

    SharedPreferences sp=getSharedPreferences("key", Context.MODE_PRIVATE);
    SharedPreferences.Editor ed=sp.edit();
    ed.putInt("value", your_value);
    ed.commit();
    

    Code to get value from sharedpreferences:

    SharedPreferences sp=getSharedPreferences("key", Context.MODE_PRIVATE);
    int value = sp.getInt("value", default_value);
    

    You can check login and logout by using this value.

提交回复
热议问题