在本地存储ID的最佳方法是什么?
问题描述:
我正在编写一个Android应用程序.我需要将用户的ID存储在本地,以便他不必在每次使用该应用程序时都重写其名称,姓氏和密码. 做这个的最好方式是什么?
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?
答
您可以使用共享首选项"存储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"); */