android SharedPreferences 详解与开发实例

本文详细介绍了Android中的SharedPreferences的使用,包括如何存储和读取数据,创建与销毁机制,权限设置,以及跨进程访问数据的方法。讲解了MODE_PRIVATE、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE等权限模式,并强调了在不同Android版本中权限的差异。同时,分享了跨进程访问SharedPreferences的步骤和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


如果转载,请注明博主名字与文章出处:

杨光atany

http://blog.youkuaiyun.com/yang8456211/article/details/9051851


让我们先来看看SDK里面怎么写吐舌头

       Interface for accessing and modifying preference data returned bygetSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through anSharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the variousget methods must be treated as immutable by the application.


意思是说:想要使用sharedpreferences可以通过一个getSharedPreferences(String,int)方法获得接口,修改preferences中的数据则需要使用到SharedPreferences.Editor对象,去保证数据提交的不变性和持久性。


看完介绍,首先说下本文内容。

1、SharedPreferences的使用

2、SharedPreferences创建与销毁

3、SharedPreferences的权限

4、跨进程访问SharedPreferences数据


一、SharedPreferences的使用

				SharedPreferences myShare = getSharedPreferences("Test",MODE_WORLD_WRITEABLE + MODE_MULTI_PROCESS+ MODE_WORLD_READABLE);//所有权限
				SharedPreferences.Editor editor = myShare.edit();
				editor.putString("myvalue", "Hello Atany");
				editor.commit();


1、储存Preferences

getSharedPreferences()- 接受两个参数,第一个是文件名字,第二个是权限,用于设置不同程序对该文件操作

getPreferences() - 只有文件权限参数,与上一个不同在于只创建一个文件,默认使用当前类名作为文件的名字


1)可以看到,SharedPreferences 实例是通过getSharedPreferences()方法得到的,第一个参数的值为String类型,是SharedPreferences存储xml文件的名字,第二个值是该文件的权限,现在设为MODE_PRIVATE,为私有,关于权限看下文。

                                SharedPreferences myShare = getSharedPreferences("Test", MODE_PRIVATE);//私有权限

2)存储数据是通过创建一个editor实现。editor.putString()第一个参数是key,第二个是value,在xml文件中,数据是以键值对<key,value>的方式储存

SharedPreferences.Editor editor = myShare.edit();
editor.putString("myvalue", "Hello Atany"); 

3)如果不提交的话,数据是不会保存的。

editor.commit();

2、读取Preferences

SharedPreferences share = getSharedPreferences("Test",MODE_PRIVATE);
String str = share.getString("myvalue", null);
注:通过getString方法获得数据,第一个参数是key,第二个参数是DefValue 所谓的默认值(对于不同数据有getBoolean等等)


二、SharedPreferences创建与销毁


1、什么时候创建?


1)当读取的xml不存在的时候,和File不同,程序是不会报错的,取而代之是会返回一个defvalue(默认值),这个默认值在getString的第二个参数设置。

2)SharedPreferences创建的时机是editor.commit();在editor提交数据之后,在DDMS的data/data/包名/shared_prefs下会创建这个文件。


2、怎么销毁?

1)editor.clear();能够清空SharedPreferences里面的数据。


三、SharedPreferences的权限

SharedPreferences的权限分为四种


MODE_PRIVATE

MODE_WORLD_READABLE

MODE_WORLD_WRITEABLE

MODE_MULTI_PROCESS


注:在第一次创建SharedPreferences时写死权限,getSharedPreferences("Test", MODE_PRIVATE);当以后修改时,例如getSharedPreferences("Test",MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE+MODE_MULTI_PROCESS);也不会对其权限进行修改。


1、MODE_PRIVATE(0)

File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID)

默认模式,表示preference是私有的,只有当前appcation能够对其进行读写操作,当然,如果ShareUserID的话,与创建该应用的有相同UserID的应用也可以对其进行读写。


2、MODE_WORLD_READABLE(1)

File creation mode: allow all other applications to have read access to the created file.

此种模式产生的preferences对所有应用程序都是可读的,这个模式是不推荐的,如果需要不同数据之间的共享的话,建议用到ContentProvider。


3、MODE_WORLD_WRITEABLE(2)

File creation mode: allow all other applications to have write access to the created file.

此种模式产生的preferences能够被所有应用程序修改。同样不推荐使用,在4.0之后即使设置了此种模式,也不能被其他程序修改了。


4、MODE_MULTI_PROCESS(4)

主要用于多任务,2.3版本之前是默认的,2.3之后当多个进程共同访问的时候,必须指定这个标签。

注:括号中的数为权限对应的整形


四、跨进程访问SharedPreferences数据

访问其他程序的Sharedpreferences分为三步:

1、创建SharedPreferences对象

创建对象时要根据需求设置权限为,MODE_WORLD_READABLE或者MODE_WORLD_WRITEABLE

SharedPreferences myShare = getSharedPreferences("Test",MODE_WORLD_WRITEABLE + MODE_WORLD_READABLE);
SharedPreferences.Editor editor = myShare.edit();
editor.putString("myvalue", "Hello Atany");
editor.commit();

注:如果没有设置权限,在其他程序读取时Log会显示could't rename file的异常。 在4.0之后即使设置了MODE_WORLD_WRITEABLE,也不能被其他程序修改了。如果需要修改可以尝试ContentProvider 或者BroadCast


2、创建需要访问对象的上下文,"com.example.testsharedpreferences"创建preferences包的包名

Context otherContext = createPackageContext("com.example.testsharedpreferences", CONTEXT_IGNORE_SECURITY);

3、读取SharePreferences中的信息

SharedPreferences myShare = otherContext.getSharedPreferences("Test", MODE_WORLD_READABLE);
String str = myShare.getString("myValue", "");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值