写一个布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="com.example.myapplicationqimo.MainActivity"> <Button android:text="备份短信" android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
然后MainActivity里面参考如下代码:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onclick_bak(View view) throws IOException { //content://sms查新系统所有短信的uri Uri uri=Uri.parse("content://sms"); //获得 访问内容提供者 ContentResolver contentResolver=getContentResolver(); //通过contentResolver查询数据 Cursor cursor=contentResolver.query(uri,new String[]{"address","date","type","body"},null,null,null); List<SmsInfo> smsInfoList=new ArrayList<SmsInfo>(); //cursor.moveToNext()移动到下一行 while (cursor.moveToNext()){ String address=cursor.getString(0); long date=cursor.getLong(1); int type=cursor.getType(2); String body=cursor.getString(3); SmsInfo smsInfo=new SmsInfo(date,type,body,address); smsInfoList.add(smsInfo); } //读取数据结束 cursor.close(); //备份 SmsUtils.backUpSms(smsInfoList,this); } }
SmsInfo:
public class SmsInfo { public SmsInfo(long date,int type,String body,String address){ this.address=address; this.body=body; this.date=date; this.type=type; } private long date; private int type; private String body; private String address; private int 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; } }
SmsUtils:
public class SmsUtils { public static void backUpSms(List<SmsInfo> smsInfoList,Context context) throws IOException { //创建序列化器serializer XmlSerializer serializer= Xml.newSerializer(); //获取sd卡根目录的路径 File file=new File(Environment.getExternalStorageDirectory(),"sms.xml"); //获得输出流 FileOutputStream os=new FileOutputStream(file); //设置编码方式 serializer.setOutput(os,"utf-8"); //写入xml文件标识 serializer.startDocument("utf-8",true); serializer.startTag(null,"smss"); for(SmsInfo info:smsInfoList){ 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,"type"); serializer.text(info.getType()+""); serializer.endTag(null,"type"); serializer.startTag(null,"address"); serializer.text(info.getAddress()); serializer.endTag(null,"address"); serializer.startTag(null,"date"); serializer.text(info.getDate()+""); serializer.endTag(null,"date"); serializer.endTag(null,"sms"); } serializer.endTag(null,"smss"); serializer.endDocument(); Toast.makeText(context,"cg",Toast.LENGTH_LONG).show(); } }
Androidmainfest:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission> <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>build.gradle:compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'