模拟备份短信功能,首先要知道存短信得分为四个部分,短信体,时间,传送方式:接收/发送,还有就是电话号码。
然后我们定义一个javabean对他进行封装。然后通过stringbuffer拼接好,传入到本地sd卡中,就完成了;
短信封装类
package com.example.xmlsave; public class Mess { private String body; private String date; private String address; private String type; public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Mess(String body, String date, String address, String type) { super(); this.body = body; this.date = date; this.address = address; this.type = type; } }
activity代码
package com.example.xmlsave; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn; private List<Mess> list ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<Mess>(); //模拟存入短信到list里 for (int i = 0; i < 10; i++) { //短信封装对象 Mess mess = new Mess("吉明哲真帅"+i, new Date()+"", "138"+i, "1"); list.add(mess); } btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //用stringbuffer进行拼接 StringBuffer sb = new StringBuffer(); sb.append("<?xml version=‘1.0‘ encoding=‘utf-8‘?>"); sb.append("<Message>"); for (int i = 0; i < list.size(); i++) { sb.append("<msm>"); sb.append("<body>"); sb.append(list.get(i).getBody()); sb.append("</body>"); sb.append("<type>"); sb.append(list.get(i).getType()); sb.append("</type>"); sb.append("<date>"); sb.append(list.get(i).getDate()); sb.append("</date>"); sb.append("<address>"); sb.append(list.get(i).getAddress()); sb.append("</address>"); sb.append("</msm>"); } sb.append("</Message>"); //获得sd卡路径 File file = new File(Environment.getExternalStorageDirectory()+File.separator+"mes.xml"); try { FileOutputStream fos = new FileOutputStream(file); fos.write(sb.toString().getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }
通过xml系列化器生成xml文件
package com.example.xmlsave; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.xmlpull.v1.XmlSerializer; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Xml; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn; private List<Mess> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<Mess>(); // 模拟存入短信到list里 for (int i = 0; i < 10; i++) { // 短信封装对象 Mess mess = new Mess("吉明哲真帅" + i, new Date() + "", "138" + i, "1"); list.add(mess); } btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // 判断sd卡是否挂载 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File file = new File(Environment .getExternalStorageDirectory() + File.separator + "sms2.xml"); try { FileOutputStream fos = new FileOutputStream(file); // 声明xml序列化对象 XmlSerializer xs = Xml.newSerializer(); // 初始化 参数为文件输出流和生成xml文件的编码集 xs.setOutput(fos, "utf-8"); // 开始生成xml文件 参数为头文件的编码集 xml文件是否独立 xs.startDocument("utf-8", true); // 生成开始节点 命名空间 节点的名字不用加尖括号 xs.startTag(null, "message"); for (int i = 0; i < list.size(); i++) { xs.startTag(null, "msm"); xs.startTag(null, "body"); // 内容 xs.text(list.get(i).getBody()); xs.endTag(null, "body"); xs.startTag(null, "type"); xs.text(list.get(i).getType()); xs.endTag(null, "type"); xs.startTag(null, "date"); xs.text(list.get(i).getDate()); xs.endTag(null, "date"); xs.startTag(null, "address"); xs.text(list.get(i).getAddress()); xs.endTag(null, "address"); xs.endTag(null, "msm"); } xs.endTag(null, "message"); // 告诉序列化器生成完毕 xs.endDocument(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } }
时间: 2024-10-13 11:28:03