问题
I'm coding an Android application. I need to store the id of a user in local in order that he doesn't have to rewrite its name, surname and password every time he uses the application. What is the best way to do this?
回答1:
You can use Shared Preferences to store id
Code to save value
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("id", 1);
/*editor.putString("Key", value); */
editor.commit();
Code to get value
String id = sharedpreferences.getString("id", "");
/*sharedpreferences.getString("key", "defaultValue"); */
Hope this will help you
回答2:
You can Use Shared Preference;
Check this tutorial.
回答3:
There are 4 ways Some of the below methods are useful if you want to save more complex data.
1.Shared Preferences-best for your use case
Find the tutorial here
2.SQLite-a database used for android
Check this tutorial to get through SQLite
3.Content providers, if you want to share the same local data to other applications of your choice
Find the tutorial here
4.ORM mapping libraries
a.GreenDao
my favorite-Documentation and HowToDo
b.DBFlow
little complex but fast-Documentation and HowToDo
c.ORMLite
Very good documentation and support over the internet-Documentation and How to do
d.Realm
New but heavily used by tech giants-Documentation and how to do
来源:https://stackoverflow.com/questions/43803247/whats-the-best-way-to-store-an-id-in-local