packagecom.example.generatexml;importjava.io.File;importjava.io.FileOutputStream;importjava.util.ArrayList;importjava.util.List;importorg.xmlpull.v1.XmlSerializer;importandroid.os.Bundle;importandroid.os.Environment;importandroid.support.v7.app.ActionBarActivity;importandroid.util.Xml;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.Toast;importcom.example.generatexml.domain.SmsInfo;public class MainActivity extendsActionBarActivity {private Listlist;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= new ArrayList();long phone = 1880075555;for(int i=0;i<10;i++)
{
SmsInfo info= new SmsInfo(i, System.currentTimeMillis(),"
list.add(info);
}
}public voidbackupSms1(View v){try{
File file= new File(Environment.getExternalStorageDirectory(),"backup1.xml");
FileOutputStream fos= newFileOutputStream(file);
StringBuffer sb= newStringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("");for(SmsInfo s :list)
{
sb.append("");
sb.append("
"+s.getBody()+"");sb.append(""+s.getType()+"");
sb.append(""+s.getAddr()+"");
sb.append(""+s.getDate()+"");
sb.append("");
}
sb.append("");
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(this, "保存成功", 0).show();
}catch(Exception e) {
e.printStackTrace();
Toast.makeText(this, "保存失败", 0).show();
}
}public voidbackupSms2(View v)
{try{
XmlSerializer ser=Xml.newSerializer();
File file= new File(Environment.getExternalStorageDirectory(),"backup2.xml");
FileOutputStream os= newFileOutputStream(file);
ser.setOutput(os,"utf-8");
ser.startDocument("utf-8", true);
ser.startTag(null, "smss");for(SmsInfo s :list)
{
ser.startTag(null, "sms");
ser.attribute(null, "id", s.getId()+"");
ser.startTag(null, "body");
ser.text(s.getBody());
ser.endTag(null, "body");
ser.startTag(null, "type");
ser.text(s.getType()+"");
ser.endTag(null, "type");
ser.startTag(null, "addr");
ser.text(s.getAddr());
ser.endTag(null, "addr");
ser.startTag(null, "date");
ser.text(s.getDate()+"");
ser.endTag(null, "date");
ser.endTag(null, "sms");
}
ser.endTag(null, "smss");
ser.endDocument();
os.close();
Toast.makeText(this, "保存成功", 0).show();
}catch(Exception e) {
e.printStackTrace();
Toast.makeText(this, "保存失败", 0).show();
}
}
}