[Several ways to develop data persistence in android]

1. Introduction

The Android system provides us with several ways of persistent data storage to meet different needs.

1. 【Shared Preferences】

  1. Preferences is analyzed from the structure of its saved data, which is a relatively lightweight method of storing data. Similar to our commonly used ini file to save software initialization settings, it is also commonly used to store simpler parameter settings on the Android platform.

    For example, it can be used to save the modification or custom parameter setting made by the user last time, and the original setting will still be maintained when the program is started again. Use the Context.getSharedPreferences() method to read and write values. This method enables other modules in the same program to share data by setting the name.

     如果 不需要与其它模块共享数据,可以使用 Activity.getPreferences()方法保持数据私有。需要着重强调一点,无法直接在多个程序间共享 Preferences 数据(不包括使用 Content Providers)。
    
  2. 【File】

    This is the second method, which can create a file for saving data in the device's own storage device or an external storage device. Also by default, files cannot be shared between different programs.

     【1)写文件:调用Context.openFileOutput()方法 根据指定的路径和文件名来创建文件,这个方法会返回一个 FileOutputStream 对象。】 【因此创建文件输出流时,如果指定路径下 没有指定名称的文件,则会新建一个文件 】
    
     2)读取文件:调用 Context.openFileInput()方法通过制定的路径和文件名来返回一个标准的 Java FileInputStream 对象。
    
    • [Divided into internal memory and external storage by location]
      • [Internal Storage memory]
        store the data persistently in the internal storage space of the mobile phone. It is mainly used for private data storage.

      • [External Storage]
        Store data persistently in the external SD card of the mobile phone. It is mainly used for non-stealthy data storage.

  3. 【SQLite Databases】

    Databases include interfaces for using SQLite databases in the Android API.

    [The database created by each program is private, in other words, programs cannot access each other's database].

    Create an SQLiteDatabase object in the program, which contains most of the methods for interacting with the database, such as reading data or managing current data. New databases can be created using the create() method of SQLiteDatabase and its subClassSQLiteOpenHelper.

  4. 【Network Connection (storage)】

    Network acquires and saves data resources through the network. This method requires the device to maintain a network connection, so there are some limitations. Two classes for related operations are listed below: java.net.* android.net.*

Guess you like

Origin blog.csdn.net/UserFrank/article/details/129205805