短信备份

/**
 * 将短信写成XML的格式,备份到SDCard上 可以把数据同步到网上
 */
public class SmsUtil {


public interface SmsbackUpCallBack {
public void beforeBackUp(int max);


public void onSmsBackUp(int progress);
}


/**
* 备份短信    注意判断SDCard是否存在
*/
public static void smsBackUp(Context context, SmsbackUpCallBack callable)
throws Exception {
ContentResolver resolver = context.getContentResolver();
File file = new File(Environment.getExternalStorageDirectory(),
"backupsms.xml");
FileOutputStream fos = new FileOutputStream(file);
// 读用户的短信
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument("UTF-8", true);


serializer.startTag(null, "smss");
Uri uri = Uri.parse("content://sms/");// 获取所有的短信
Cursor cursor = resolver.query(uri, new String[] { "date", "body",
"type", "address" }, null, null, null);
// 设置进度条的最大值
int max = cursor.getCount();
serializer.attribute(null, "max", String.valueOf(max));// 保存短信条数
if (callable != null) {
callable.beforeBackUp(max);
}


int progress = 0;
while (cursor.moveToNext()) {
String date = cursor.getString(0);
String body = cursor.getString(1);
String type = cursor.getString(2);
String address = cursor.getString(3);


serializer.startTag(null, "sms");


serializer.startTag(null, "date");
serializer.text(date);
serializer.endTag(null, "date");


serializer.startTag(null, "body");
serializer.text(body);
serializer.endTag(null, "body");


serializer.startTag(null, "type");
serializer.text(type);
serializer.endTag(null, "type");


serializer.startTag(null, "address");
serializer.text(address);
serializer.endTag(null, "address");


serializer.endTag(null, "sms");


progress++;
if (callable != null) {
callable.onSmsBackUp(progress);
}


}
cursor.close();
serializer.endTag(null, "smss");
serializer.endDocument();
serializer.flush();


fos.close();
}


/**
* 短信还原过程中的回调
*/
public interface SmsRestoreCallBack {
public void beforeRestore(int max);


public void onSmsRestore(int progress);
}


/**
* 还原短信

* @param context
*            上下文
* @param callable
*            备份过程的回调
* @param flag
*            还原以前的短信之前是否保留现有的短信 true保留,false不保留
*/
public static void smsRestore(Context context, SmsRestoreCallBack callable,
boolean flag) throws Exception {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
if (!flag) {
resolver.delete(uri, null, null);
}
// 读取sdcard上的xml文件
File file = new File(Environment.getExternalStorageDirectory(),
"backupsms.xml");
// pull解析
XmlPullParser parser = Xml.newPullParser();
FileInputStream fin = new FileInputStream(file);
parser.setInput(fin, "UTF-8");
int next = parser.next();
int max = 0;
int progress = 0;
String[] strs = new String[4];
while (next != XmlPullParser.END_DOCUMENT) {
switch (next) {
case XmlPullParser.START_DOCUMENT:
max = Integer.parseInt(parser.getAttributeValue(0));
if (callable != null) {
callable.beforeRestore(max);
}
break;
case XmlPullParser.START_TAG:
if ("body".equals(parser.getName())) {
strs[0] = parser.nextText();
}
if ("type".equals(parser.getName())) {
strs[1] = parser.nextText();
}
if ("date".equals(parser.getName())) {
strs[2] = parser.nextText();
}
if ("address".equals(parser.getName())) {
strs[3] = parser.nextText();
}
break;
case XmlPullParser.END_TAG:
if (parser.getName().equals("sms")) {
ContentValues values = new ContentValues();
values.put("body", strs[0]);
values.put("type", strs[1]);
values.put("date", strs[2]);
values.put("address", strs[3]);
// 插入短信
context.getContentResolver().insert(uri, values);
progress++;
if (callable != null) {
callable.onSmsRestore(progress);
}
}
break;
case XmlPullParser.END_DOCUMENT:
break;
default:
break;
}
next = parser.next();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值