public
class
MainActivity
extends
Activity
implements
OnClickListener {
private
Button
bt_create
;
private
List<SmsBean>
smsList
;
SmsBean
sms
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
bt_create
=(Button) findViewById(R.id.
bt_create
);
bt_create
.setOnClickListener(
this
);
//模拟短信生成
Random random=
new
Random();
smsList
=
new
ArrayList<SmsBean>();
for
(
int
i=0;i<10;i++){
smsList
.add(
sms
);
}
}
/**
*/
@Override
public
void
onClick(View v) {
try
{
//创建xml序列化器
XmlSerializer serializer = Xml.newSerializer();
//初始化序列化器参数
File file=
new
File(Environment.getExternalStorageDirectory(),
"/sms.xml"
);
FileOutputStream fos=
new
FileOutputStream(file);
serializer.setOutput(fos,
"UTF-8"
);
//开始写xml文件
for
(SmsBean sms :
smsList
) {
serializer.startTag(
null
,
"sms"
);
serializer.startTag(
null
,
"address"
);
serializer.text(sms.getAddress());
serializer.endTag(
null
,
"address"
);
serializer.startTag(
null
,
"content"
);
serializer.text(sms.getContent());
serializer.endTag(
null
,
"content"
);
serializer.startTag(
null
,
"tate"
);
serializer.text(String.valueOf(sms.getDate()));
serializer.endTag(
null
,
"tate"
);
serializer.endTag(
null
,
"sms"
);
}
serializer.endTag(
null
,
"Sms"
);
serializer.endDocument();
//关闭流资源
fos.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
}