什么是在本地存储ID的最佳方式?

时间:2017-05-05 10:59:36

标签: android file save

我正在编写Android应用程序。我需要将用户的id存储在本地,以便他每次使用应用程序时都不必重写其名称,姓氏和密码。 做这个的最好方式是什么?

3 个答案:

答案 0 :(得分:2)

您可以使用共享偏好;

检查this教程。

答案 1 :(得分:1)

您可以使用共享首选项来存储ID

  

保存价值的代码

 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();
  

获取价值的代码

 String id = sharedpreferences.getString("id", "");
/*sharedpreferences.getString("key", "defaultValue"); */
  

希望这会对你有所帮助

答案 2 :(得分:0)

有4种方法 如果您想保存更复杂的数据,以下某些方法非常有用。

1.Shared偏好 - 最适合您的用例

Find the tutorial here

2.SQLite-用于android的数据库

Check this tutorial to get through SQLite

3.内容提供者,如果您想将相同的本地数据共享给您选择的其他应用程序

Find the tutorial here

4.ORM映射库

a.GreenDao

我的最爱 - Documentation and HowToDo

b.DBFlow

有点复杂但很快 - Documentation and HowToDo

c.ORMLite

通过互联网提供非常好的文档和支持 - Documentation and How to do

d.Realm

新技术巨头大量使用 - Documentation and how to do

相关问题