首先需要加入必用的权限:
android.permission.WRITE_SMS
android.permission.READ_SMS
android.permission.WRITE_EXTERNAL_STORAGE
布局文件:
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="备份所有的短信" />
</RelativeLayout>
备用短信的方法类:
package com.example.duduanxing.domain;
public class dxxx {
private long date;
private int type;
private String body;
private String address;
private int id;
public dxxx() {
}
public dxxx(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 dxxx(long date, int type, String body, String address) {
this.date = date;
this.type = type;
this.body = body;
this.address = address;
}
public long getDate() {
return date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
调用备份短信的方法类:
package com.example.duduanxing;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast;
import com.example.duduanxing.domain.dxxx;
public class SmsUtils {
/**
* @param dxxx短信的集合
* @param context上下文
*/
public static void backUpSms(List<dxxx> dxxx, Context context) {
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 (dxxx info : dxxx) {
serializer.startTag(null, "smss");
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, "smss");
}
serializer.endTag(null, "smss");
serializer.endDocument();
os.close();
Toast.makeText(context, "备份成功", 0).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "澶囦唤澶辫触", 0).show();
}
}
}
主类:
package com.example.duduanxing;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import com.example.duduanxing.domain.dxxx;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
// 获取全部的路径
Uri uri = Uri.parse("content://sms/");
// 得到系统的中间人
ContentResolver resolver = getContentResolver();
// 路径、获取哪些列的信息、选择条件、参数、是否排序
Cursor cursor = resolver.query(uri, new String[] { "address", "date",
"type", "body" }, null, null, null);
List<dxxx> dxxxs = new ArrayList<dxxx>();
while (cursor.moveToNext()) {
String address = cursor.getString(0);
long date = cursor.getLong(1);
int type = cursor.getInt(2);
String body = cursor.getString(3);
dxxx dxxx = new dxxx(date, type, body, address);
dxxxs.add(dxxx);
/*
* System.out.println("address" + address);
* System.out.println("date" + date); System.out.println("type" +
* type); System.out.println("body" + body);
* System.out.println("--------------------");
*/
}
cursor.close();
//备份的集合、上下文
SmsUtils.backUpSms(dxxxs, this);
}
}