80,358
社区成员
发帖
与我相关
我的任务
分享
/**
* 备份短信
*/
public void backupSms() {
new Thread()
{
public void run()
{
//SystemSms是自定义的用于保存系统格式短信的类
//getAllSystemSms用于获取系统所有的短信
List<SystemSms> infos =getAllSystemSms();
System.out.println(infos);
File dir = new File(Environment.getExternalStorageDirectory(), "/phoneGuard/backup");
if(!dir.exists())
{
dir.mkdirs();
}
File file = new File(Environment.getExternalStorageDirectory() + "/phoneGuard/backup/smsbackup.xml");
//创建一个xml的序列化器
XmlSerializer xmlSerializer = Xml.newSerializer();
try
{
FileOutputStream fos = new FileOutputStream(file);
//设置写xml的编码
xmlSerializer.setOutput(fos, "utf-8");
xmlSerializer.startDocument("utf-8", true);
xmlSerializer.startTag(null, "smss");
for(SystemSms info : infos)
{
xmlSerializer.startTag(null, "sms");
xmlSerializer.startTag(null, "id");
xmlSerializer.text(info.getId());
xmlSerializer.endTag(null, "id");
xmlSerializer.startTag(null, "address");
xmlSerializer.text(info.getAddress());
xmlSerializer.endTag(null, "address");
xmlSerializer.startTag(null, "date");
xmlSerializer.text(info.getDate());
xmlSerializer.endTag(null, "date");
xmlSerializer.startTag(null, "type");
xmlSerializer.text(info.getType() + "");
xmlSerializer.endTag(null, "type");
xmlSerializer.startTag(null, "body");
xmlSerializer.text(info.getBody());
xmlSerializer.endTag(null, "body");
xmlSerializer.endTag(null, "sms");
}
xmlSerializer.endTag(null, "smss");
xmlSerializer.endDocument();
fos.flush();
fos.close();
//在子线程里面是不能弹出一个Toast的,因为子线程里面没有Looper,
//但我们通过下面几个步骤就可以在子线程里面弹出Toast了
// Looper.prepare();//创建一个Looper
//Toast.makeText(context, "备份成功", Toast.LENGTH_SHORT).show();
// Looper.loop();//轮循一次Loope
System.out.println("备份成功");
}
catch (Exception e)
{
// Looper.prepare();//创建一个Looper
//Toast.makeText(context, "备份失败", Toast.LENGTH_SHORT).show();
//Looper.loop();//轮循一次Looper
e.printStackTrace();
System.out.println("备份失败");
}
}
}.start();
}