package com.tsh.backsmstrue;
import java.util.ArrayList;
java.util.List;
com.tsh.backsmstrue.domain.SmsInfo;
com.tsh.backsmstrue.utils.SmsUtil;
android.app.Activity;
android.content.ContentResolver;
android.database.Cursor;
android.net.Uri;
android.os.Bundle;
android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 备份短信
* @param v
*/
backupSms(View v){
Uri uri=Uri.parse("content://sms/");
ContentResolver resolver=getContentResolver();
String[] projection=new String[]{"address","date","type","body"};
Cursor cursor=resolver.query(uri,null,1)">null);
List<SmsInfo> smss=new ArrayList<SmsInfo>();
while(cursor.moveToNext()){
String body=cursor.getString(cursor.getColumnIndex("body"));
System.out.println(body);
System.out.println("===========");
SmsInfo smsinfo=new SmsInfo();
smsinfo.setAddress("110");
smsinfo.setMessage(body);
smss.add(smsinfo);
}
SmsUtil.backSms2(smss,this);
cursor.close();
}
}
com.tsh.backsmstrue.utils;
java.io.File;
java.io.FileOutputStream;
org.xmlpull.v1.XmlSerializer;
com.tsh.backsmstrue.domain.SmsInfo;
android.content.Context;
android.os.Environment;
android.util.Xml;
android.view.View;
android.widget.Toast;
class SmsUtil {
* 备份短信第二种
* static void backSms2(List<SmsInfo> smsInfos,Context context){
XmlSerializer serializer=Xml.newSerializer();
try {
File file=new File(Environment.getExternalStorageDirectory(),"backup2.xml");
FileOutputStream os= FileOutputStream(file);
serializer.setOutput(os,"utf-8");
serializer.startDocument("utf-8",1)">true);
serializer.startTag();
for(SmsInfo sms:smsInfos){
serializer.startTag();
serializer.attribute(,sms.getAddress());
serializer.text(sms.getMessage());
serializer.endTag();
}
serializer.endTag();
serializer.endDocument();
os.close();
Toast.makeText(context,"保存成功",0).show();
} catch (Exception e) {
Toast.makeText(context,"保存失败",1)">).show();
}
}
}