xml 备份短信 序列化器 XmlSerializer

MsgInfo.java  
//短信数据内容属性结构
<pre name="code" class="java">package com.example.MsgDomain;

/**
 * 短信数据内容
 * int type  代表接收或发送的短信状态
 * @author Administrator
 *
 */
public class MsgInfo {
	long data;
	int type,id;
	String body;
	String address;
	
	public MsgInfo(long data, int type, String address, String body, int id) {
		super();
		this.data = data;
		this.type = type;
		this.id = id;
		this.body = body;
		this.address = address;
	}
	public MsgInfo() {
		
	}
	public MsgInfo(long data, int type, String body, String address) {
		super();
		this.data = data;
		this.type = type;
		this.body = body;
		this.address = address;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public long getData() {
		return data;
	}
	public void setData(long data) {
		this.data = data;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

}

 
</pre><p></p><span style="color:#cc0000"><br /></span><p><span style="color:#cc0000">MsgMainActivity.java  MainActivity</span></p><p><span style="color:#cc0000">//第二·中序列化方式  XmlSerializer 推荐使用</span></p><p></p><pre name="code" class="java">public class MsgMainActivity extends Activity {
	//短信集合
	List<MsgInfo> msgInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_msg_main);
		
		msgInfo = new ArrayList<MsgInfo>();
		Random random = new Random();//随机数
		long number = 1380000000;
		for(int i = 0;i<10;i++){
			msgInfo.add(new MsgInfo(System.currentTimeMillis(),random.nextInt(2),"短信内容:"+i,Long.toString(number+i),i));
		}
	}	
	/**
	 * 备份短信内容到xml文件-构建xml格式
	 * 假设已经获取到短信信息
	 * @param v
	 */
	public void ButListener(View v){
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<smss>");
		for(MsgInfo info:msgInfo){
			sb.append("<sms>");
			
				sb.append("<type>");
				sb.append(info.getType());
				sb.append("</type>");
			
				sb.append("<address>");
				sb.append(info.getAddress());
				sb.append("</address>");
				
				sb.append("<body>");
				sb.append(info.getBody());
				sb.append("</body>");
				
				sb.append("<data>");
				sb.append(info.getData());
				sb.append("</data>");
				
			sb.append("</sms>");
		}
		sb.append("</smss>");
		//写文件之前,判断sd是否存在,空间是否足够
		File file = new File(Environment.getExternalStorageDirectory(),"SmsBackup.xml");
		try {
			FileOutputStream fos;
			fos = new FileOutputStream(file);
			fos.write(sb.toString().getBytes());
			fos.close();
			Toast.makeText(this, "backup succeed", 1).show();
		} catch (IOException e) {
			e.printStackTrace();
			Toast.makeText(this, "backup Failure", 1).show();
		}
		
	}
	/**第二种生成xml方式
	 * 序列化方式 XmlSerializer
	 * 初始化序列化器,指定xml数据写入到哪个文件,并指定编码格式
	 * @param 
	 */
	
	public void ButListeneTow(View v){
		try {
			XmlSerializer  serializer = Xml.newSerializer();
			//创建文件
			File file = new File(Environment.getExternalStorageDirectory(),"SmsBackupTow.xml");
			//文件输出流
			FileOutputStream os = new FileOutputStream(file);
			//要写入文件与编码格式
			serializer.setOutput(os, "UTF-8");
			//true 是否以独立文件成立
			serializer.startDocument("UTF-8",true);
			//开始节点,null:命名空间
			serializer.startTag(null, "smss");
			for (MsgInfo info : msgInfo) {
				// <sms id = "1">
				serializer.startTag(null, "sms");
				serializer.attribute(null, "id", info.getId()+"");
				//body
				serializer.startTag(null, "body");
				serializer.text(info.getBody());
				serializer.endTag(null, "body");
				//address
				serializer.startTag(null, "address");
				serializer.text(info.getAddress());
				serializer.endTag(null, "address");
				//data
				serializer.startTag(null, "data");
				serializer.text(info.getData()+"");
				serializer.endTag(null, "data");
				//type
				serializer.startTag(null, "type");
				serializer.text(info.getType()+"");
				serializer.endTag(null, "type");
				//</sms>
				serializer.endTag(null, "sms");
			}
			//</smss>
			serializer.endTag(null, "smss");
			serializer.endDocument();
			os.close();//closs stream
			Toast.makeText(this, "backup succeed", 1).show();
		} catch (IOException e) {
			Toast.makeText(this, "backup Failure", 1).show();
			e.printStackTrace();
		}	
	}
}	

 
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值