Android 短信备份完整版
发布日期:2021-06-30 22:35:12 浏览次数:4 分类:技术文章

本文共 4117 字,大约阅读时间需要 13 分钟。

public class BackUpSmsActivity extends BaseActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_backupsms); } public void addSms(View view){ try { Uri url = Uri.parse("content://sms"); ContentValues values = new ContentValues(); values.put("date", System.currentTimeMillis()); values.put("type", 1); values.put("address", "5446"); values.put("body", "早上好"); getContentResolver().insert(url, values); } catch (Exception e) { e.printStackTrace(); show("备份失败"); } } /** * 备份短信 * @param view */ public void backupSms(View view){ List
smsInfos = new ArrayList
(); Uri uri = Uri.parse("content://sms"); Cursor cursor = getContentResolver().query(uri, new String[]{
"date","type","address","body"}, null, null, null); while(cursor.moveToNext()){ long date = cursor.getLong(cursor.getColumnIndex("date")); int type = cursor.getInt(cursor.getColumnIndex("type")); String address = cursor.getString(cursor.getColumnIndex("address")); String body = cursor.getString(cursor.getColumnIndex("body")); SmsInfo info = new SmsInfo(date, type, body, address); smsInfos.add(info); doBackUp(smsInfos); } } private void doBackUp(List
smsInfos) { try { XmlSerializer serializer = Xml.newSerializer(); OutputStream os = new FileOutputStream(new File("/sdcard/sms.xml")); serializer.setOutput(os, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "smss"); for(SmsInfo info : smsInfos){ serializer.startTag(null, "sms"); serializer.startTag(null, "date"); serializer.text(info.getDate() + ""); serializer.endTag(null, "date"); 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, "body"); serializer.text(info.getBody() + ""); serializer.endTag(null, "body"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); os.close(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "备份失败", 1).show(); } }}

实体类

/** * 短信 * @author Administrator * */public class SmsInfo {
private int id; private long date; private int type; private String body; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public SmsInfo() { super(); // TODO Auto-generated constructor stub } public SmsInfo(long date, int type, String body, String address) { this.date = date; this.type = type; this.body = body; this.address = address; } public SmsInfo(int id, long date, int type, String body, String address) { super(); this.id = id; 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; }}

转载地址:https://lovoo.blog.csdn.net/article/details/51233038 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android 使用系统摄像头拍相片与拍视频,并显示
下一篇:Andorid 自定义ProgressBar控件

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月15日 00时58分05秒