Android数据存储专题之Shared Preferences

Data Storage(数据存储)

Store application data in databases, files, or preferences, in internal or removeable storage. You can also add a data backup service to let users store and recover application and system data.(Android应用中的数据以数据库,文件,preferences等形式存储在手机内部存储器或者可移动存储器。也可以添加一个数据备份服务让使用者可以备份和恢复应用和系统的数据。

Storage Options(存储选项)

Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.(Android系统为开发者提供了几种用于存储持久的应用程序数据,开发者根据开发的具体需求选择不同的方式去存储数据,比如,根据需要存储的数据对这个应用是私有的呢还是可以与其他应用(和其他使用者)共享以及这些数据的大小。
Your data storage options are the following:(可供选择的方式如下:)

Shared Preferences(共享参数)
Store private primitive data in key-value pairs.(以键值对的形式存储私人的原始数据)


Internal Storage(内部存储)
Store private data on the device memory.(存储私有数据在设备内部存储器)


External Storage(外部存储)
Store public data on the shared external storage.(存储公共类型的数据在外部存储)


SQLite Databases(sqlite 数据库)
Store structured data in a private database.(存储机构化的数据在私有数据库)


Network Connection(网络连接)
Store data on the web with your own network server.(依靠自己的网络服务存储数据在网络上)

Android provides a way for you to expose even your private data to other applications — with a content provider. A content provider is an optional component that exposes read/write access to your application data, subject to whatever restrictions you want to impose. For more information about using content providers, see the Content Providers documentation.(Android提供了一种可以让应用自身私有的数据暴露给其他应用的方法---内容提供者。内容提供者是一个根据你提供的限制条件使你应用数据暴露可读写的可选择组件。更多有关使用内容提供者的信息可以查看内容提供者文档)

Using Shared Preferences(内容提供者)

The Shared Preferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use Shared Preferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
(Android系统源码中内容提供者类提供了一个允许你保存和检索基本类型的 持久 键值对值的大致框架。你可以使用内容提供者去保存任何基本类型数据:布尔类型,浮点类型,整型,长整型,字符串。这些数据将被保存,即使应用程序被结束了)

To get a SharedPreferences object for your application, use one of two methods:
使用以下两种方法之一去获取一个内容提供者对象

getSharedPreferences() 

- Use this if you need multiple preferences files identified by name, which you specify with the first parameter.(如果你需要获取多个preferences文件通过名称(第一个参数)标识的指定,使用getSharedpreferences()方法。)

getPreferences() 

-Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.(如果你只需为你的activity获取一个的preferences文件使用getPreferences()方法。因为这个文件是你的activity的唯一一个preferences文件)

To write values(写入值):


Call edit() to get a SharedPreferences.Editor.内容提供者对象调用edit()方法获取SharedPreferences.Editor对象
Add values with methods such as putBoolean() and putString().使用这些方法存储不同基础类型的数据
Commit the new values with commit()最后提交数据

To read values(读取值):

use SharedPreferences methods such as getBoolean() and getString().使用这些对应方法读取不同类型的存储数值。

Here is an example that saves a preference for silent keypress mode in a calculator(这里是一个使用SharedPreferences来保存一个计算器选择按键为静音模式的例子):

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";


    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .


       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }


    @Override
    protected void onStop(){
       super.onStop();


      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);


      // Commit the edits!
      editor.commit();
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android数据存储是一种将数据存储在设备上的技术,以便应用程序可以使用它们,无论它们是否在运行时。这使得应用程序可以使用一些简单且安全的存储方法,而无需直接与设备存储硬件进行交互。Android提供了几种不同的数据存储选项,包括: 1. **Shared Preferences**:这是最基本的数据存储方式,它允许应用程序存储和检索键值对数据。这对于小型数据存储需求非常有用,因为它们是本地存储并且对其他应用程序不可见。 2. **Files**:应用程序可以使用文件系统来存储和检索数据。这可以包括文本文件、图像、音频和视频文件等。文件系统存储数据可以在应用程序关闭后保持持久性。 3. **SQLite**:SQLite是一个轻量级的关系型数据库,它可以在设备上作为嵌入式数据库使用。SQLite提供了对数据的结构化查询支持,因此更适合存储大量数据。 4. **Content Providers**:Content Providers允许应用程序之间共享数据。它们使得跨应用程序访问和共享数据成为可能,这对于构建大型应用程序的跨应用程序功能非常有用。 5. **外部存储(如SD卡)**:Android允许应用程序访问设备的外部存储(如SD卡)。这可以用于存储大文件或大型数据集,但需要注意的是,访问外部存储可能需要额外的权限。 在选择适当的存储选项时,需要考虑数据的类型、大小、持久性和安全性。例如,如果数据只需要在应用程序运行时可用,Shared Preferences可能是一个合适的选择。然而,如果需要存储大量结构化数据或大型文件,那么SQLite或外部存储可能更适合。 此外,对于敏感数据(如用户密码或个人信息),建议使用加密来保护这些数据。在Android中,可以使用AES或其他加密算法来实现这一点。 最后,记住在处理存储数据时遵循最佳实践,例如使用适当的错误处理和日志记录,以确保数据的安全性和完整性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值