Coin163

首页 > 短信备份

短信备份

相关标签: android sms

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

/**
 * 将短信写成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();
}
}
}

原文

/**  * 将短信写成XML的格式, 备份到SDCard上 可以把数据同步到网上  */ public class SmsUtil { public interface SmsbackUpCallBack { public void beforeBackUp(int max); public void onSm

------分隔线----------------------------
相关推荐