这种方式应该是用起来最简单的Android读写外部数据的方法了。他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、 透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,那么这些设置最后就可以 通过Preferences来保存,而程序员不需要知道它到底以什么形式保存的,保存在了什么地方。当然,如果你愿意保存其他的东西,也没有什么限制。只是在性能上不知道会有什么问题。
在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME/shared_prefs 目录下。
数据读取
String PREFS_NAME = "Note.sample.roiding.com";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
String hello = settings.getString("hello", "Hi");
这段代码中:
- SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
通过名称,得到一个SharedPreferences,顾名思义,这个Preferences是共享的,共享的范围据现在同一个Package中,这里面说所的Package和Java里面的那个Package不同,貌似这里面的Package是指在AndroidManifest.xml文件中:<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.roiding.sample.note" android:versionCode="1" android:versionName="1.0.0">
这里面的package。根据我目前的实验结果看,是这样的,欢迎指正。后面的那个int是用来声明读写模式,先不管那么多了,暂时就知道设为0(android.content.Context.MODE_PRIVATE)就可以了。
- boolean silent = settings.getBoolean(”silentMode”, false);
获得一个boolean值,这里就会看到用Preferences的好处了:可以提供一个缺省值。也就是说如果Preference中不存在这个值的话,那么就用后面的值作为返回指,这样就省去了我们的if什么什么为空的判断。
数据写入
String PREFS_NAME = "Note.sample.roiding.com";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.putString("hello", "Hello~");
editor.commit();
有了上面数据读取的代码,这里面的就容易理解了,只是别忘了最后的commit();
March 18th, 2010 at 2:39
In fact,I can’t understand.
March 26th, 2010 at 4:08
Sign Early,choose wonder point nurse marry argue equally rule accident boy successful soil back card quarter available before old soon many age social report stage bag boat issue similar to second double bad total bed supply at consider cover instrument point wind key proper whose contrast completely observe sight upper date line program commercial old treatment change account fear teach easily appearance low railway care contrast impact per garden within hot damage sign college track individual belong painting can train visitor opportunity shout law always career from achieve neighbour
April 18th, 2010 at 7:16
wilful post
April 29th, 2010 at 7:40
Hello! cdgfdbg interesting cdgfdbg site!
April 29th, 2010 at 7:40
Very nice site! cheap viagra
April 29th, 2010 at 7:40
Very nice site! [url=http://ypxoiea.com/ovyrvov/2.html]cheap cialis[/url]
April 29th, 2010 at 7:40
Very nice site! cheap cialis http://ypxoiea.com/ovyrvov/4.html
April 29th, 2010 at 7:40
Very nice site!
May 3rd, 2010 at 3:09
Hello!
tramadol ,
May 3rd, 2010 at 3:10
Hello!
xanax ,
May 4th, 2010 at 2:33
Hello!
viagra ,
May 5th, 2010 at 1:31
Hello!
cialis ,
May 6th, 2010 at 6:19
Hello!
viagra ,
May 7th, 2010 at 7:05
Hello!
cialis ,
May 15th, 2010 at 6:35
Hi Homie
Ive been reading along for a while now.
Cu
May 19th, 2010 at 11:49
Yo Bro
you couldnt agree more, myself, but not everyone is as clever as you seem to be. Or as you seem to be! HA! :-p
Do you do blogroll exchanging?
Good Bye
May 20th, 2010 at 12:36
Hi Homie
A friend of mine just emailed me one of your articles from a while back. i read that one a few more.
May 20th, 2010 at 12:38
Added to my Google News Reader. Looking forward to reading more from you down da road!
Whats yer opinion on similar?
June 5th, 2010 at 7:45
I look forward to reading more from you. Have a really tight week.
Good Bye
June 6th, 2010 at 10:12
Hello You
Thanks i found just dha info i already searched everywhere and just couldnt find. What a good website. Like this website this site is one of my favs.
June 6th, 2010 at 10:12
meself am excited already as meself know you always have pretty cool stuff.
my homie will put your site on my blogroll.
June 6th, 2010 at 10:42
my homie must say this is a ubercool article my homie enjoyed reading it keep da good work
Ciao
June 14th, 2010 at 2:32
good decision
June 18th, 2010 at 1:22
You know, i have to tell you, i really enjoyed this site and teh insight from everyone who participates. i find it to be refreshing and very informative. i wish there were more blogs like it. Anyway, i felt it was about time i posted, I
Goodbye
June 18th, 2010 at 7:05
Hello
I couldnt agree more, myself, but not everyone is as clever as you seem to be. Or as I seem to be! HA! :-p
My bro have a website aswell.
June 25th, 2010 at 10:01
Yes, to learn what your very own signs are. It can be a fun experience.
June 27th, 2010 at 4:56
looks great! I hope to see more soon!
June 28th, 2010 at 11:17
http://www.roiding.com is very the most informative. I liked your blog a lot. Thank you.
June 30th, 2010 at 10:14
Love that.
June 30th, 2010 at 10:32
Greetings! May I export your material in Pdf formatting? I will need it in my training course.
July 3rd, 2010 at 1:07
Hi There
i want to thank the blogger very much not only for this post but also for his all previous efforts. I found your site to be very interesting. I will be coming back to site for more information.
Anyway, meself felt it was about time meself posted
Yours Sincerly
July 7th, 2010 at 7:34
Your website is simply posting glitches on my FF 3 web browser.
July 8th, 2010 at 1:22
I’m simply right here to help, you’ll need to check reviews right here. Intensely.
July 11th, 2010 at 1:35
http://www.roiding.com is great! If you havent heard about payday loans well this is a great opportunity to get to know them Payday loans are loans which you dont really get from banks but from other lending institutions
July 11th, 2010 at 7:47
Hi ! Do you enjoy some kind of gift box where I will likely pass monetary gift in PayPal?
July 13th, 2010 at 8:17
information? Might be this method deep under creative commons?
July 17th, 2010 at 4:51
another great news,thanks.Can’t wait to download!
July 20th, 2010 at 8:54
I agree with you.
July 20th, 2010 at 10:37
THANK YOU VERY MUCH FOR YOUR ENCOURAGING WORDS ,FULL OF INSPIRATION
July 22nd, 2010 at 8:33
Hows It Going
Ive been reading along for a while now.
July 23rd, 2010 at 10:48
Hello
meself must say this is a nice article meself enjoyed reading it keep da good work
meself will put your site on my blogroll.
Laters
July 24th, 2010 at 9:38
I was completely astonished after reading the article.
July 25th, 2010 at 4:34
Hey
Can you tell me who did your layout? Ive been looking for one kind of like yours. thank you very much.
If you want to exchange links let me know.
Laters
July 26th, 2010 at 10:59
UAdBui
July 28th, 2010 at 11:37
Excessively
July 29th, 2010 at 12:35
Hello ! Do you have a sort of gift box where I may transfer gift in PayPal?
July 29th, 2010 at 11:03
Hiya! I referred to your articles though I don’t get a hold of a trackback. Can certainly you describe to us what’s taking place?
July 29th, 2010 at 11:43
Hey! We are hunting for longer term writers, will you be intrigued? It will not allow you way too rich however there is an essential compensation and if you highly enjoy authoring then that opportunity is for you.