[{"data":1,"prerenderedAt":84},["ShallowReactive",2],{"page-配置类-1":3,"page-count-配置类":83},[4],{"id":5,"title":6,"body":7,"date":59,"description":13,"extension":60,"meta":61,"navigation":64,"path":76,"seo":77,"stem":78,"tags":79,"__hash__":82},"blogs\u002F_legacy\u002F2012\u002F2012-12-16-android%e9%85%8d%e7%bd%ae%e7%b1%bb%e7%9a%84%e4%bd%bf%e7%94%a8.md","Android配置类的使用",{"type":8,"value":9,"toc":55},"minimark",[10,14,17,28,31,37,40,46,49],[11,12,13],"p",{},"Android本身提供了应用保存配置的机制，虽然有时我们会希望自己来控制配置的过程，但是利用系统自带的配置也是一个不错的选择。",[11,15,16],{},"最重要的是，由于是系统自带的功能，所以调用起来比较简单。而不用只是为了一两个变量就自己去写配置文件的读取和写入这些东西。现在写东西比较偷懒，基本每次都是复制下面的代码然后改改就用了。",[18,19,24],"pre",{"className":20,"code":22,"language":23},[21],"language-text","\u002F\u002F保存配置信息\nSharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);\nSharedPreferences.Editor editor = settings.edit();\neditor.putString(\"preferences\", mPreferences);\neditor.commit();\n\n\u002F\u002F读取配置信息        \nSharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);\nString mPreferences = settings.getString(\"preferences\", DEFULT_PRES);\n","text",[25,26,22],"code",{"__ignoreMap":27},"",[11,29,30],{},"关于getSharedPreferences()，第一个参数是配置文件本身的标识，只要提供字符串就可以了。第二参数则是标识配置文件的读写权限的，一般情况下用0就好了，文档对这个参数的描述是这样的：",[18,32,35],{"className":33,"code":34,"language":23},[21],"Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. The bit MODE_MULTI_PROCESS can also be used if multiple processes are mutating the same SharedPreferences file. MODE_MULTI_PROCESS is always on in apps targetting Gingerbread (Android 2.3) and below, and off by default in later versions.\n",[25,36,34],{"__ignoreMap":27},[11,38,39],{},"操作模式是一个bit flag，具体的值对应关系为",[18,41,44],{"className":42,"code":43,"language":23},[21],"MODE_PRIVATE           ---  0000\nMODE_WORLD_READABLE    ---  0001\nMODE_WORLD_WRITEABLE   ---  0010\nMODE_MULTI_PROCESS     ---  0100\n",[25,45,43],{"__ignoreMap":27},[11,47,48],{},"仔细看一下的话就会发现，正如文档所描述的，只有MODE_MULTI_PROCESS这个标识是API Level11之后才有的。这个设定是为了打开配置文件的多进程同时访问功能，大概是随着android版本和对应设备性能的提升才会出现的设计变更。不过大体上程序都不会异步去访问配置，也很少会有多个程序读取相同的配置文件的情况，所以使用的机会比较少。但是既然是bit flag的话，平时就一直设成4就好了。如果想保险起见的话那就先判定一下就好了。",[18,50,53],{"className":51,"code":52,"language":23},[21],"int __sdkLevel = Build.VERSION.SDK_INT;\nSharedPreferences __sp = $context.getSharedPreferences(SETTING_NAME, (__sdkLevel > Build.VERSION_CODES.FROYO) ? 4 : 0);\n",[25,54,52],{"__ignoreMap":27},{"title":27,"searchDepth":56,"depth":57,"links":58},2,3,[],"2012-12-16","md",{"layout":62,"status":63,"published":64,"author":65,"author_login":66,"author_email":67,"author_url":68,"wordpress_id":69,"wordpress_url":70,"date_gmt":71,"excerpt":72},"post","publish",true,{"display_name":66,"login":66,"email":67,"url":68},"chaoshikari","chaoshikari@gmail.com","\u002F",528,"\u002F\u002F?p=528","2012-12-16 04:45:44 +0000",{"type":8,"value":73},[74],[11,75,13],{},"\u002F2012-12-16-android配置类的使用",{"title":6,"description":13},"_legacy\u002F2012\u002F2012-12-16-android%e9%85%8d%e7%bd%ae%e7%b1%bb%e7%9a%84%e4%bd%bf%e7%94%a8",[80,81],"Android","配置类","QU3_1jY3y2TlcZQGJKdq4u7QyopDtP2t8EQ5KaLpKsI",1,1788763182926]