短信备份步骤:
1.创建序列化编辑器
XmlSerializer serializer = Xml.newSerializer();
2.创建短信写入的位置
File file = new File(Environment.getExternalStorageDirectory(),
"info.xml");
// getExternalStorageDirectory(), sd卡路径
3.创建输出流
FileOutputStream fos = new FileOutputStream(file);
4.初始化xml序列化器,指定xml写入哪个文件中,并且制定文件格式
serializer.setOutput(fos, "utf-8");
5.写入头部标签,及xml头部标签(<?xml version="1.0" encoding="utf-8"?>)
serializer.startDocument("utf-8", true); 表示用utf-8格式写入,该文件是否独立
6.循环写入短信内容
for (MessageInfo mess : messageInfo) {
serializer.startTag(null, "info");
serializer.attribute(null, "id", mess.getId() + "");
serializer.startTag(null, "data");
serializer.text(mess.getData() + "");
serializer.endTag(null, "data");
serializer.startTag(null, "type");
serializer.text(mess.getType() + "");
serializer.endTag(null, "type");
serializer.startTag(null, "body");
serializer.text(mess.getBody());
serializer.endTag(null, "body");
serializer.startTag(null, "address");
serializer.text(mess.getAddress());
serializer.endTag(null, "address");
serializer.endTag(null, "info");
}
// Message 为类,messageInfo为list<
Message>集合
// serializer.startTag(null, "info"); <info></info>
7.短信备份成功