Android简易数据存储之SharedPreferences

浅谈Android中SharedPreferences的使用
本文介绍Android中SharedPreferences的基本概念、使用方法及其注意事项。包括数据的添加、修改和读取,以及文件创建和读取的方式。同时,讨论了与SharedPreferences相关的安全性和存储位置,并对比了它与SQLite的区别。

  Andorid提供了多种数据存储的方式,例如前面说到的“Android数据存储之SQLite的操作”是用于较复杂的数据存储。然而,如果有些简单的数据存储如果采用SQLite的方式的话会显得比较笨重。例如:记录用户是否访问过APP的欢迎页面之类的数据,如果采用SQLite的话会显得没必要而且费时费力。因此Andorid提供了另一种存储简单数据的方式SharedPreferences。SharedPreferences是一个轻量级的数据存储方式,其仅支持boolean、int、long、float、String和Set<String>这几种数据类型。

  

  此外,SharedPreferences不能直接添加和修改数据,添加和修改数据需要通过SharedPreferences的Editor来完成。具体的实现可参考官方文档:http://developer.android.com/reference/android/content/SharedPreferences.html。下面看看SharedPreferences是如何进行添加、修改和读取数据的。

  新建一个工程,名字为DataOperate,然后再MainActivity.java中添加如下代码

        SharedPreferences sharedPreferences = getSharedPreferences("appSetting", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("IsFirstView", false);
        editor.putString("AppName", "SharePreferences Demo");
        editor.commit();//同步提交到磁盘文件,因而会出现阻塞等的现象,如果要确保提交成功,尽量使用commit
        editor.apply();//先提交到内存,然后异步提交到磁盘,效率更高,但没有返回消息。

        Boolean isFirstView = sharedPreferences.getBoolean("IsFirstView", false);
        System.out.println("IsFirstView : " + isFirstView);
        String appName = sharedPreferences.getString("AppName", "");
        System.out.println("AppName : " + appName);
        String author = sharedPreferences.getString("author", "author null");
        System.out.println("Author : " + author);

  将APP运行到模拟器中,可以看到如下的输出结果:

09-12 10:34:50.627    2669-2669/com.example.ibm.dataoperate I/System.out﹕ IsFirstView : false
09-12 10:34:50.627    2669-2669/com.example.ibm.dataoperate I/System.out﹕ AppName : SharePreferences Demo
09-12 10:34:50.651    2669-2669/com.example.ibm.dataoperate I/System.out﹕ Author : author null

  这就是SharedPreferences的简单应用。然而,这里有几个地方需要注意的:

  1、文件创建或读取的方式有三种:MODE_PRIVATEMODE_WORLD_READABLEMODE_WORLD_WRITEABLE

    然而官方推荐的使用方式为MODE_PRIVATE的使用方式,也是默认的方式。至于原因,官方文档是这么说的:

This constant was deprecated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as ContentProvider, BroadcastReceiver, and Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore. File creation mode: allow all other applications to have read access to the created file.

  MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE都是very dangerous的,如果采用了这两种方式都会导致应用存在安全漏洞。因此官方推荐使用的MODE_PRIVATE的方式。

  2、Editor提交数据的方式有两种。一种是通过commit的方式提交,另一种是通过apply的方式来提交。

  官方文档是这么说的:

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call apply wins.

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.

As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with apply() if you were already ignoring the return value.

You don't need to worry about Android component lifecycles and their interaction with apply() writing to disk. The framework makes sure in-flight disk writes from apply() complete before switching states.

The SharedPreferences.Editor interface isn't expected to be implemented directly. However, if you previously did implement it and are now getting errors about missing apply(), you can simply call commit() from apply().

  这两种提交方式的不同点在于(英文不好,有错请指出):

    a、apply是先将数据提交到内存,然后异步写入文件,效率较高;而commit是同步提交到内存并写入文件,因此效率相对较低,如果数据大的话会存在阻塞的情况。

    b、由于apply是使用异步提交的方式,因此没有返回值,即使写入失败也不会返回消息。而commit有返回值,能确保数据正常写入磁盘文件。因此如果需要确保数据写入的完整性,最好采用commit的方式。

  3、数据存储方式和位置

    成功运行APP后,可以在app安装目录下的shared_prefs(完整路径为/data/data/项目包/shared_prefs/名称.xml)中看到“appSetting.xml”(文件名为自定义的名称),打开xml文件后可以看到其实就是采用了标准xml文件键值对的方式进行存储。

  存储位置:

  文件内容:

  4、PreferenceActivity创建配置首选项界面

    此外Android还提供了PreferenceActivity快速创建首选项页面。和SharedPreferences不同的是,SharedPreferences是纯操作,要另外创建设置页面;而PreferenceActivity则可以让你快速地创建设置界面和存储数据。在此先不深入说明,改天有空再另写一篇文章加以描述。感兴趣的可以在查看官方文档:http://developer.android.com/reference/android/preference/PreferenceActivity.html。墙哦!俗话说。。。

转载于:https://www.cnblogs.com/rwxwsblog/p/4802708.html

在车辆工程中,悬架系统的性能评估和优化一直是研究的热点。悬架不仅关乎车辆的乘坐舒适性,还直接影响到车辆的操控性和稳定性。为了深入理解悬架的动态行为,研究人员经常使用“二自由度悬架模型”来简化分析,并运用“传递函数”这一数学工具来描述悬架系统的动态特性。 二自由度悬架模型将复杂的车辆系统简化为两个独立的部分:车轮和车身。这种简化模型能够较准确地模拟出车辆在垂直方向上的运动行为,同时忽略了侧向和纵向的动态影响,这使得工程师能够更加专注于分析与优化与垂直动态相关的性能指标。 传递函数作为控制系统理论中的一种工具,能够描述系统输入和输出之间的关系。在悬架系统中,传递函数特别重要,因为它能够反映出路面不平度如何被悬架系统转化为车内乘员感受到的振动。通过传递函数,我们可以得到一个频率域上的表达式,从中分析出悬架系统的关键动态特性,如系统的振幅衰减特性和共振频率等。 在实际应用中,工程师通过使用MATLAB这类数学软件,建立双质量悬架的数学模型。模型中的参数包括车轮质量、车身质量、弹簧刚度以及阻尼系数等。通过编程求解,工程师可以得到悬架系统的传递函数,并据此绘制出传递函数曲线。这为评估悬架性能提供了一个直观的工具,使工程师能够了解悬架在不同频率激励下的响应情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值