layout页面:
main.xml
只加了一个button
1.MainActivity.java
package com.zufe.smsback;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1 = (Button)findViewById(R.id.button);
bt1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri,new String[]{"address","date","type","body"},null,null,null);
List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();
while(cursor.moveToNext()){
String address = cursor.getString(0);
long date = cursor.getLong(1);
int type = cursor.getInt(2);
String body = cursor.getString(3);
SmsInfo smsInfo = new SmsInfo();
smsInfo.setDate(date);
smsInfo.setAddress(address);
smsInfo.setBody(body);
smsInfo.setType(type);
}
cursor.close();
SmsUtils.backSms(smsInfos,MainActivity.this);
}
});
}
}
2.SmsInfo.java对象类
package com.zufe.smsback;
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;
}
}
3.SmsUtils工具类
package com.zufe.smsback;
import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast;
import org.xmlpull.v1.XmlSerializer;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
public class SmsUtils {
public static void backSms(List<SmsInfo> smsInfos, Context context){
try{
XmlSerializer serializer = Xml.newSerializer();
File file = new File(Environment.getExternalStorageDirectory(),"sys.xml");
FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos,"utf-8");
serializer.startDocument("utf-8",true);
//构建根节点
serializer.startTag(null,"smss");
for (SmsInfo info:smsInfos){
serializer.startTag(null,"sms");
serializer.attribute(null,"id",info.getId()+"");
//构建子节点body
serializer.startTag(null,"body");
serializer.text(info.getBody());
serializer.endTag(null,"body");
//构建子节点address
serializer.startTag(null,"address");
serializer.text(info.getAddress());
serializer.endTag(null,"address");
//构建type子节点
serializer.startTag(null,"type");
serializer.text(info.getType()+"");
serializer.endTag(null,"type");
//构建date子节点
serializer.startTag(null,"date");
serializer.text(info.getDate()+"");
serializer.endTag(null,"date");
serializer.endTag(null,"sms");
}
serializer.endTag(null,"smss");
serializer.endDocument();
fos.close();
Toast.makeText(context,"备份成功",Toast.LENGTH_SHORT).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(context,"备份失败",Toast.LENGTH_SHORT).show();
}
}
}
4.Manifest.xml设置权限
在activity前面加上三句话
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>