一:界面设计
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt"
android:onClick="backSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短信备份" />
<Button
android:layout_below="@id/bt"
android:onClick="backSms2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二种方式短信备份" />
</RelativeLayout>
二:虚拟一个短信信息类SmsInfo.java
package com.cloud.sms;
public class SmsInfo {
private long date;
private int type;
private String body;
private String address;
private int id;
public SmsInfo() {
}
public SmsInfo(long date, int type, String body, String address,int id) {
this.date = date;
this.type = type;
this.body = body;
this.address = address;
this.id=id;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
三:编写主程序代码
package com.example.xml1;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.xmlpull.v1.XmlSerializer;
import com.cloud.sms.SmsInfo;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private List<SmsInfo> smsInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//模拟一些短信信息,用于备份
smsInfo=new ArrayList<SmsInfo>();
Random random=new Random();
long number=1350000000;
for(int i=0;i<10;i++){
smsInfo.add(new SmsInfo(System.currentTimeMillis(),random.nextInt(2)+1,
"短信内容"+i,Long.toString(number+i),i));
}
}
/**
* 备份手机的短信
* @param v
*/
public void backSms(View v){
//xml格式进行备份文件
StringBuilder sb=new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sb.append("<smss>");
for(SmsInfo info:smsInfo){
sb.append("<sms>");
sb.append("<address>");
sb.append(info.getAddress());
sb.append("</address>");
sb.append("<type>");
sb.append(info.getType());
sb.append("</type>");
sb.append("<body>");
sb.append(info.getBody());
sb.append("</body>");
sb.append("<date>");
sb.append(info.getDate());
sb.append("</date>");
sb.append("</sms>");
}
sb.append("</smss>");
try {
File file=new File(Environment.getExternalStorageDirectory(),"backup.xml");
FileOutputStream fos=new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(this, "备份成功", 0).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "备份失败", 0).show();
}
}
public void backSms2(View v){
try {
XmlSerializer serializer= Xml.newSerializer();
File file=new File(Environment.getExternalStorageDirectory(),"backup2.xml");
FileOutputStream os=new FileOutputStream(file);
serializer.setOutput(os, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss");
for(SmsInfo info:smsInfo){
serializer.startTag(null, "sms");
serializer.attribute(null, "id", info.getId()+"");
serializer.startTag(null, "body");
serializer.text(info.getBody());
serializer.endTag(null, "body");
serializer.startTag(null, "address");
serializer.text(info.getAddress());
serializer.endTag(null, "address");
serializer.startTag(null, "type");
serializer.text(info.getType()+"");
serializer.endTag(null, "type");
serializer.startTag(null, "date");
serializer.text(info.getDate()+"");
serializer.endTag(null, "date");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smss");
os.close();
Toast.makeText(this, "备份成功", 0).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "备份失败", 0).show();
}
}
}
四:该程序使用的权限
android.permission.WRITE_EXTERNAL_STORAGE
五:程序运行结果
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21