【问题标题】:What's the best way to store an id in local?在本地存储 id 的最佳方式是什么?
【发布时间】:2017-05-05 10:59:35
【问题描述】:

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

【问题讨论】:

  • 共享偏好
  • 使用SharedPreferences
  • 谢谢,我会搜索一些关于这个的文档

标签: android file save


【解决方案1】:

您可以使用共享偏好;

查看this教程。

【讨论】:

    【解决方案2】:

    您可以使用 Shared Preferences 来存储 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"); */
    

    希望对你有所帮助

    【讨论】:

    • 谢谢@sukhbir,它帮助了我。
    【解决方案3】:

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

    1.Shared Preferences - 最适合您的用例

    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

    【讨论】:

    • 请在对任何帖子投反对票之前给出正当理由。人们正试图在这里提供帮助。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多