使用XML文件备份短信
使用StringBuffer拼接字符串
-
把整个xml文件所有节点append到sb对象里
sb.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"); //添加smss的开始节点 sb.append("<smss>"); .......
-
把sb写到输出流中
fos.write(sb.toString().getBytes());
代码
JavaBean
public class Message {private String body; // 内容 date// 时间 address// 对方号码 type// 类型 1是收到 2是发送 getBody()return}void setBody(String)this.body = getDate setDatedate getAddress setAddressaddress getType setTypetype Message,super(); }
操作
// 存放虚拟短信信息
// 备份短信
import javaio.File;.FileNotFoundException.FileOutputStreamutil.ArrayList.List comitheimacreatexmldomain.Message androidos.Bundleapp.Activityview.Menu.View MainActivityextends Activity List<Message> smsList @Overrideprotected onCreate(Bundle savedInstanceStateonCreate(savedInstanceState); setContentViewRlayoutactivity_main//虚拟10条短信 smsList new ArrayList>();for(int i 0<10 i++){ sms ("Hello"+ SystemcurrentTimeMillis """138"i"1"addsms} click(View v){//在内存中把xml备份短信的格式拼接出来 StringBuffer sb StringBuffer sbappend"<?xml version='1.0' encoding='utf-8' standalone='yes' ?>""<messages>"for(Message:"<sms>""<body>"getBody());"</body>""<date>"getDate"</date>""<type>"getType"</type>""<address>"getAddress"</address>""</sms>""</messages>" // 将内容保存起来 File file File"sdcard/sms.xml"try FileOutputStream fos FileOutputStreamfile foswritesbtoString().getBytesclosecatch(Exception e// TODO Auto-generated catch blockprintStackTrace
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> |