MainActivity
package com.example.zong.testcopy; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.support.v4.content.ContentResolverCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.net.URI; 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); } 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<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(date, type, body, address); smsInfos.add(smsInfo); } cursor.close(); SmsUtils.backUpSms(smsInfos, this); } }
SmsInfo
package com.example.zong.testcopy; /** * Created by Zong on 2017/6/22. */ public class SmsInfo { private long date; private int type; private String body; private String address; private int id; public int getId(){ return id; } public void setId(int id) { this.id = 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 SmsInfo(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 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; } }
SmsUtils
package com.example.zong.testcopy; 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.io.IOException; import java.util.List; /** * Created by Zong on 2017/6/22. */ public class SmsUtils { public static void backupSms(List<SmsInfo> smsInfos, Context context) throws IOException { try { XmlSerializer serialier = Xml.newSerializer(); File file = new File(Environment.getExternalStoragePublicDirectory(), "sms.xml"); FileOutputStream os = new FileOutputStream(file); serialier.setOutput(os, "utf-8"); serialier.startDocument("utf-8", true); serialier.startTag(null, "smss"); for (SmsInfo info : smsInfos) { serialier.startTag(null, "smss"); serialier.attribute(null, "id", info.getId() + ""); serialier.startTag(null, "body"); serialier.text(info.getBody()); serialier.endTag(null, "body"); serialier.startTag(null, "address"); serialier.text(info.getAddress()); serialier.endTag(null, "address"); serialier.startTag(null, "type"); serialier.text(info.getType() + ""); serialier.endTag(null, "type"); serialier.startTag(null, "date"); serialier.text(info.getDate() + ""); serialier.endTag(null, "date"); serialier.endTag(null, "sms"); } serialier.endTag(null, "sms"); serialier.endDocument(); os.close(); Toast.makeText(context, "备份成功", 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "备份失败", 0).show(); } } }
AndroidMainFest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zong.testcopy"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <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" </manifest>