List<SMSInfo> smsInfos = service.getSMSInof(); try { File file = new File("/sdcard/smsbackup.xml"); XmlSerializer serializer = Xml.newSerializer(); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "smss"); serializer.startTag(null, "count"); serializer.text(smsInfos.size()+""); serializer.endTag(null, "count"); for (SMSInfo smsInfo : smsInfos) { serializer.startTag(null, "sms"); serializer.startTag(null, "id"); serializer.text(smsInfo.getId()); serializer.endTag(null, "id"); serializer.startTag(null, "address"); serializer.text(smsInfo.getAddress()); serializer.endTag(null, "address"); serializer.startTag(null, "date"); serializer.text(smsInfo.getDate()); serializer.endTag(null, "date"); serializer.startTag(null, "type"); serializer.text(smsInfo.getType()+""); serializer.endTag(null, "type"); serializer.startTag(null, "body"); serializer.text(smsInfo.getBody()); serializer.endTag(null, "body"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); fos.flush(); fos.close(); Looper.prepare(); Toast.makeText(getApplicationContext(), "备份完成", 1).show(); Looper.loop(); } catch (Exception e) { e.printStackTrace(); Looper.prepare(); Toast.makeText(getApplicationContext(), "备份失败", 1).show(); Looper.loop(); }
说明:1.new一个文件,将数据存放到文件中。
2.用XmlSerializer将数据写到xml文件中。
应用:1.短信备份
2.通讯录本地备份
3.等等备份