其中涉及的部分类可以自行查询,如:AtomicFile.java   FastXmlSerializer.java

    private static final String APPCONFIG_FILENAME = "appPowerSaveConfig.xml";private static final String XML_TAG_FILE = "app_powersave_config";private static final String XML_TAG_PKG = "package";private static final String XML_ATTRIBUTE_PKG_NAME = "name";private static final String XML_ATTRIBUTE_PKG_OPTIMIZE  = "optimize";private static final String XML_ATTRIBUTE_PKG_ALARM  = "alarm";private static final String XML_ATTRIBUTE_PKG_WAKELOCK = "wakelock";private static final String XML_ATTRIBUTE_PKG_NETWORK  = "network";private static final String XML_ATTRIBUTE_PKG_AUTOLAUNCH  = "autolaunch";private static final String XML_ATTRIBUTE_PKG_SECONDARYLAUNCH  = "secondarylaunch";private static final String XML_ATTRIBUTE_PKG_LOCKSCREENCLEANUP  = "lockscreencleanup";private static final String XML_ATTRIBUTE_PKG_POWERCONSUMERTYPE = "consumertype";//将一下MAP数据写入XML文件中
public static boolean writeConfig(Map<String, AppPowerSaveConfig> appConfigMap) {AtomicFile aFile = new AtomicFile(new File(new File(Environment.getDataDirectory(),"system"), APPCONFIG_FILENAME));FileOutputStream stream;try {stream = aFile.startWrite();} catch (IOException e) {Slog.e(TAG, "Failed to write state: " + e);return false;}try {XmlSerializer serializer = new FastXmlSerializer();serializer.setOutput(stream, "utf-8");serializer.startDocument(null, true);serializer.startTag(null, XML_TAG_FILE);if (appConfigMap != null) {for (Map.Entry<String, AppPowerSaveConfig> cur : appConfigMap.entrySet()) {final String appName = cur.getKey();final AppPowerSaveConfig config = cur.getValue();if (config.isReset()) continue;serializer.startTag(null, XML_TAG_PKG);serializer.attribute(null, XML_ATTRIBUTE_PKG_NAME, appName);serializer.attribute(null, XML_ATTRIBUTE_PKG_OPTIMIZE,String.valueOf(config.optimize));serializer.attribute(null, XML_ATTRIBUTE_PKG_ALARM,String.valueOf(config.alarm));serializer.attribute(null, XML_ATTRIBUTE_PKG_WAKELOCK,String.valueOf(config.wakelock));serializer.attribute(null, XML_ATTRIBUTE_PKG_NETWORK,String.valueOf(config.network));serializer.attribute(null, XML_ATTRIBUTE_PKG_AUTOLAUNCH,String.valueOf(config.autoLaunch));serializer.attribute(null, XML_ATTRIBUTE_PKG_SECONDARYLAUNCH,String.valueOf(config.secondaryLaunch));serializer.attribute(null, XML_ATTRIBUTE_PKG_LOCKSCREENCLEANUP,String.valueOf(config.lockscreenCleanup));serializer.attribute(null, XML_ATTRIBUTE_PKG_POWERCONSUMERTYPE,String.valueOf(config.powerConsumerType));serializer.endTag(null, XML_TAG_PKG);}}serializer.endTag(null, XML_TAG_FILE);serializer.endDocument();aFile.finishWrite(stream);} catch (IOException e) {Slog.e(TAG, "Failed to write state, restoring backup." + "exp:" + "\n" + e);aFile.failWrite(stream);return false;}return true;}//将XML文件写入一下MAP中/*** static API used to read the config from /data/system/appPowerSaveConfig.xml* and save them in appConfigMap* @param appConfigMap The configs read from config file will save to it* @return Returns true for sucess.Return false for fail*/public static boolean readConfig(Map<String, AppPowerSaveConfig> appConfigMap) {AtomicFile aFile = new AtomicFile(new File(new File(Environment.getDataDirectory(),"system"), APPCONFIG_FILENAME));InputStream stream = null;try {stream = aFile.openRead();} catch (FileNotFoundException exp) {Slog.e(TAG, ">>>file not found," + exp);}if (null == stream) {aFile = new AtomicFile(new File(new File(Environment.getRootDirectory(), "etc"),APPCONFIG_FILENAME));try {stream = aFile.openRead();} catch (FileNotFoundException exp) {Slog.e(TAG, ">>>default file not found," + exp);return false;}}try {String appName = null;AppPowerSaveConfig appConfig = null;XmlPullParser pullParser = Xml.newPullParser();pullParser.setInput(stream, "UTF-8");int event = pullParser.getEventType();while (event != XmlPullParser.END_DOCUMENT) {switch (event) {case XmlPullParser.START_DOCUMENT://retList = new ArrayList<PowerGuruAlarmInfo>();break;case XmlPullParser.START_TAG:if (XML_TAG_PKG.equals(pullParser.getName())) {appConfig = new AppPowerSaveConfig();appName = pullParser.getAttributeValue(null, XML_ATTRIBUTE_PKG_NAME);appConfig.optimize = Integer.parseInt(pullParser.getAttributeValue(null,XML_ATTRIBUTE_PKG_OPTIMIZE));appConfig.alarm = Integer.parseInt(pullParser.getAttributeValue(null,XML_ATTRIBUTE_PKG_ALARM));appConfig.wakelock = Integer.parseInt(pullParser.getAttributeValue(null,XML_ATTRIBUTE_PKG_WAKELOCK));appConfig.network = Integer.parseInt(pullParser.getAttributeValue(null,XML_ATTRIBUTE_PKG_NETWORK));appConfig.autoLaunch = Integer.parseInt(pullParser.getAttributeValue(null, XML_ATTRIBUTE_PKG_AUTOLAUNCH));appConfig.secondaryLaunch =Integer.parseInt(pullParser.getAttributeValue(null, XML_ATTRIBUTE_PKG_SECONDARYLAUNCH));appConfig.lockscreenCleanup =Integer.parseInt(pullParser.getAttributeValue(null, XML_ATTRIBUTE_PKG_LOCKSCREENCLEANUP));appConfig.powerConsumerType =Integer.parseInt(pullParser.getAttributeValue(null, XML_ATTRIBUTE_PKG_POWERCONSUMERTYPE));}break;case XmlPullParser.END_TAG:if (XML_TAG_PKG.equals(pullParser.getName())) {appConfigMap.put(appName, appConfig);appConfig = null;}break;}event = pullParser.next();}} catch (IllegalStateException e) {Slog.e(TAG, "Failed parsing " + e);return false;} catch (NullPointerException e) {Slog.e(TAG, "Failed parsing " + e);return false;} catch (NumberFormatException e) {Slog.e(TAG, "Failed parsing " + e);return false;} catch (XmlPullParserException e) {Slog.e(TAG, "Failed parsing " + e);return false;} catch (IOException e) {Slog.e(TAG, "Failed parsing " + e);return false;} catch (IndexOutOfBoundsException e) {Slog.e(TAG, "Failed parsing " + e);return false;} finally {try {stream.close();} catch (IOException e) {Slog.e(TAG, "Fail to close stream " + e);return false;} catch (Exception e) {Slog.e(TAG, "exception at last,e: " + e);return false;}}return true;}

XML文件格式:

xxxx:/data/system # cat appPowerSaveConfig.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<app_powersave_config>
<package name="com.ss.android.ugc.aweme" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="0" />
<package name="com.android.camera2" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="0" consumertype="4" />
<package name="com.fadisu.cpurun" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="0" />
<package name="com.pp.assistant" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="0" consumertype="0" />
<package name="com.sprd.sleepwakeuptest" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="sprdtest.message" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.xinlian.bitz" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="1" />
<package name="com.spreadtrum.itestapp" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.google.android.gms" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="0" consumertype="4" />
<package name="com.sprd.bmte.coulomb" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="0" secondarylaunch="0" lockscreencleanup="2" consumertype="0" />
<package name="com.comcat.activity" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.jio.emiddleware" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.tencent.qqlive" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="1" />
<package name="com.greenpoint.android.mc10086.activity" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.spreadst.validdate" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="0" />
</app_powersave_config>

Android 将MAP格式数据写入XML 将XMP文件读MAP数据格式中相关推荐

  1. abap中读取excel中不同的sheet数据_Python 如何将数据写入Excel的不同或同一个工作簿中...

    在进行日常的数据分析统计时,很多时候我们需要将不同数据写入到同一个Excel文件的不同工作簿或同一个工作簿中.本文主要介绍 Python 如何将多个 DataFrame 写入同一个 Excel 不同或 ...

  2. python 向已有excel写数据_将数据写入已有的excel文件并制作汇总页签

    我必须将一些数据写入现有的xls文件中.(我应该说,我在unix上工作,不能使用windows) 我更喜欢使用python,并尝试过xlwt.openpyxl.xlutils等库.在 它不工作,因为在 ...

  3. Opencv——写入或读取数据到XML或YAML文件

    什么是XML.YAML文件 XML(eXtensible Markup Language)是一种元标记语言.所谓"原标记",就是开发者可以根据自身需要定义的标记,任何满足XML命名 ...

  4. Android 读取csv格式数据文件

    前言 什么是csv文件呢?百度百科上说 CSV是逗号分隔值文件格式,也有说是电子表格的,既然是电子表格,那么就可以用Excel打开,那为什么要在Android中来读取这个.csv格式的文件呢?因为现在 ...

  5. 将excel中的数据写入xml

    import xlwt import json import xlrd from lxml import etree class RedeExcel: def init(self,filename,s ...

  6. python将数据写入xml文件_python读取/创建XML文件

    Python中定义了很多处理XML的函数,如xml.dom,它会在处理文件之前,将根据xml文件构建的树状数据存在内存.还有xml.sax,它实现了SAX API,这个模块牺牲了便捷性,换取了速度和减 ...

  7. java中用jdom 如何创建xml文档/将数据写入XML中

    JDomOutput.java代码如下 -------------------------------------------------------------------------------- ...

  8. java 监听map的数据_使用监听器:定时清除map缓存的key value .

    使用监听器:定时清除map缓存的key value . 配置web.xml:注意位置 com.my.common.listener.TimerListener 监听类: public class Ti ...

  9. python 将json字符串格式数据格式化保存到本地文件

    我们在调用一些api的时候,经常对得到返回结果为json字符串格式的数据,但是在输出的时候,内容全是在一行 而我们如果只是使用json.dumps()方法的时候,类似于下面 最后的结果就会是这样 可以 ...

最新文章

  1. 面试准备--7.31
  2. 大神程序员几行代码增粉 100W 最后入狱,附源码。
  3. 转- prototype
  4. 将Node.js升级到最新版本
  5. stm32 Boot0,Boot1引脚设置
  6. java好还是python好-Python和Java哪个薪资更高、远景更好?
  7. python学好了可以做什么菜_你是怎么学好Python的?
  8. Linux中的高级文本处理命令,cut命令,sed命令,awk命令
  9. 以持续集成工具实现DevOps之禅
  10. 唏嘘!这家双屏智能手机厂商宣布破产:曾被称为“俄罗斯iPhone”
  11. SURF源码分析之fasthessian.h和fasthessian.cpp
  12. Atitit 重大问题解决法---记不住问题的解决 目录 1. 记不住的原因 1 2. 大脑存储内容分布 2 2.1. Jvm的存储机制 2 2.2. 人的存储机制 2 2.2.1. 图片区视觉区
  13. java 抓取网页_Java抓取网页数据
  14. 在python中month函数的用法_python @classmethod 的使用场合
  15. JavaAwt子部件定位设置大小,setBounds(x, y, w, h);setLocation(x, y); setSize(w, h); 一开始不起作用,加个延时起作用了
  16. 软件测试—十二章测试层次
  17. lego_loam 代码阅读与总结
  18. 中英文停止词表(stopword)
  19. 海康SDK的NET_DVR_GET_FTPCFG_V40
  20. 阿里云ECS添加公网IPV6

热门文章

  1. 调度算法(时间片轮转,优先级调度,多级反馈队列)【操作系统学习笔记】
  2. Ryan Dahl的Node.js遗憾地导致了Deno
  3. 安卓rom制作,你知道的有多少
  4. 学习太极创客 — ESP8226 (四)Stream
  5. DT时代,大数据如何服务产业经济?
  6. android 浏览器夜间模式
  7. 高防CDN、高防服务器以及高防IP怎么选择
  8. 查找算法之元二分搜索|单边二分搜索
  9. 51单片机入门基础介绍
  10. 推荐两款java版开源的MES制造执行系统源码,免费分享