日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 将MAP格式数据写入XML 将XMP文件读MAP数据格式中

發(fā)布時間:2023/12/20 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 将MAP格式数据写入XML 将XMP文件读MAP数据格式中 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?其中涉及的部分類可以自行查詢,如: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數(shù)據(jù)寫入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>

?

總結(jié)

以上是生活随笔為你收集整理的Android 将MAP格式数据写入XML 将XMP文件读MAP数据格式中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。