接着上文《Android 内容提供者的实现》,继续实战

打开File Exploer,找到mmssms.db数据库,导出

打开mmssms.db

新建项目,布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取所有的短信" /> </RelativeLayout>

代码如下:

package com.wuyudong.readsms;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void click(View view) {
// content://sms/
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, new String[] { "address", "date",
"type", "body" }, null, null, null);
while (cursor.moveToNext()) {
String address = cursor.getString(0);
String date = cursor.getString(1);
String type = cursor.getString(2);
String body = cursor.getString(3);
System.out.println("address:" + address);
System.out.println("date:" + date);
System.out.println("type:" + type);
System.out.println("body:" + body);
System.out.println("--------------------");
}
cursor.close();
} }

运行之,提示错误:权限问题。于是添加权限android.permission.READ_SMS和android.permission.WRITE_SMS

再次运行,搞定,成功读取系统短信

接下来继续修改程序,使之完成短信的备份功能

新建文件smsInfo.java

package com.wuyudong.domain;
public class smsInfo {
private String body;
private String date;
private String type;
private String address; public smsInfo(String body, String date, String type, String address) {
super();
this.body = body;
this.date = date;
this.type = type;
this.address = address;
}
public String getBody() {
return body;
} public void setBody(String body) {
this.body = body;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}

新建SmsUtils.java

package com.wuyudong.readsms;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List; import org.xmlpull.v1.XmlSerializer; import com.wuyudong.domain.smsInfo; import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast; public class SmsUtils {
/**
*
* @param smsInfos 短信的集合
* @param context 上下文
*/
public static void backUpSms(List<smsInfo> smsInfos, Context context){
File file = new File(Environment.getExternalStorageDirectory(),
"backup1.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
// 获取xml序列化器
XmlSerializer xs = Xml.newSerializer();
xs.setOutput(fos, "utf-8");
//生成xml头
xs.startDocument("utf-8", true);
//添加xml根节点
xs.startTag(null, "message");
for (smsInfo info : smsInfos) {
xs.startTag(null, "sms");
xs.startTag(null, "body");
xs.text(info.getBody());
xs.endTag(null, "body");
xs.startTag(null, "date");
xs.text(info.getDate());
xs.endTag(null, "date");
xs.startTag(null, "address");
xs.text(info.getAddress());
xs.endTag(null, "address");
xs.startTag(null, "type");
xs.text(info.getType());
xs.endTag(null, "type");
xs.endTag(null, "sms");
}
xs.endTag(null, "message");
//生成xml头
xs.endDocument();
fos.close();
Toast.makeText(context, "备份成功", 0).show(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(context, "备份失败", 0).show();
}
}
}

添加权限:android.permission.WRITE_EXTERNAL_STORAGE

MainActivity.java中的代码如下:

package com.wuyudong.readsms;

import java.util.ArrayList;
import java.util.List; import com.wuyudong.domain.smsInfo; import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void click(View view) {
// content://sms/
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, new String[] { "body", "date",
"type", "address" }, null, null, null);
List<smsInfo> smsinfos = new ArrayList<smsInfo>();
while (cursor.moveToNext()) {
String body = cursor.getString(0);
String date = cursor.getString(1);
String type = cursor.getString(2);
String address = cursor.getString(3);
smsInfo smsinfo = new smsInfo(body, date, type, address);
smsinfos.add(smsinfo);
}
cursor.close();
SmsUtils.backUpSms(smsinfos, this);
} }

运行项目后,生成backup1.xml文件,打开后

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<message>
<sms>
<body>ggg</body>
<date>1466043889225</date>
<address>1223</address>
<type>1</type>
</sms>
<sms>
<body>dsfgfdg</body>
<date>1466043867803</date>
<address>34543</address>
<type>1</type>
</sms>
……………………
……………………
……………………
</message>

Android 短信的备份的更多相关文章

  1. Android之——短信的备份与还原

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47091281 眼下,Android手机中的一些软件能够实现手机短信的备份与还原操作 ...

  2. Android 短信的还原

    上篇文章讲到<Android 短信的备份>,本文主要实现Android 短信的还原,即是将一条 布局文件: <RelativeLayout xmlns:android="h ...

  3. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  4. Android短信备份及插入笔记

    实现备份短信到xml文件和像短信中插入一条数据 一.实现短信将备份到xml文件中 在布局文件中定义一个按钮,定义点击事件为copyClick MainActivity.java: package co ...

  5. Android短信监听实现,及Android4.4之后短信机制变更

    前阵子公司有一个项目,简单的监听短信应用,功能只有如下两个: 1.监听短信并获取短信内容上传服务器: 2.从服务器获取短信内容,发送出去    按照传统的思路,监听短信我们有两种方式:第一种是使用广播 ...

  6. Android短信发送器(2)

    在上一篇的<Android短信发送器>当中.发送功能并不完好.当发送内容超过限定字数时,短信就会发送失败,此时就须要推断内容是否超过限制,假设不超过限制.就直接发送,反之.则对其进行处理再 ...

  7. 【mob】Android短信验证+源码

    在很多的应用当中,都涉及到了短信验证的功能,比如在注册或者找回密码的时候,那么我们如何通过第三方的平台来完成这个功能呢? 本面博文就实现短信验证,来做一个小的栗子. 第一步-下载开发包 第二步-将SD ...

  8. Android 短信验证码控件

    Android 短信验证码控件,便于项目中使用统一样式,统一提示改动.个人觉得挺好用的 <span style="font-size:18px;">public cla ...

  9. Android 短信监听及用途分析

    监听系统短信这个只能作为一个技术点来研究下,读者可能在工作中可能不会哦涉及到,一般的应用软件也不会有这个需求 但是作为程序员呢,多了解一下也是好的. Android 监听系统短信有什么用? 1.对系统 ...

随机推荐

  1. flexbox简介

    flexbox简介 什么是flexbox flexbox是一种新的布局方式,这种布局方式是2009年W3C提出的方案.它可以简便,完整,完成页面的布局.目前,它已经得到所有浏览器的支持. 但是flex ...

  2. php多进程处理

    php多进程处理 往往我们会碰到一个情况,需要写一个脚本,这个脚本要处理的数据量极大,单进程处理脚本非常慢,那么这个时候就会想到使用多进程或者多线程的方式了. 我习惯使用多进程的方式,php中使用多进 ...

  3. 【Swift学习】Swift编程之旅---类和结构体(十三)

    与其他编程语言所不同的是,Swift 并不要求你为自定义类和结构去创建独立的接口和实现文件.你所要做的是在一个单一文件中定义一个类或者结构体,系统将会自动生成面向其它代码的外部接口. 注意:通常一个类 ...

  4. Windows Server 2016正式版14393英文版ISO镜像下载:_X64FRE_ZH-CN.ISO

    http://care.dlservice.microsoft.com/dl/download/F/8/3/F83C7D26-787A-4F43-82B0-7C7BF8A12791/14393.0.1 ...

  5. SQLite中文排序

    定义一个类: using System.Data.SQLite; namespace DAL { /// <summary> /// SQLite中文排序 /// </summary ...

  6. DIP依赖倒置原则

    一.定义 1.高层模块不应该依赖低层模块,二者都应该依赖抽象 2.抽象不应该依赖于细节.细节应该依赖于抽象 二.层次化 1.简单介绍 结构良好的面向对象架构都具有清晰的层次定义,每个层次通过一个定义良 ...

  7. Redis系列四之复制

    一.复制基本配置与演示 为了避免单点故障,Redis提供了复制功能,可以实现自动同步的过程. 1.配置 同步后的数据分为两类:一类是主数据库(master),一类是从数据库(slave).主数据库可以 ...

  8. 【原创】asp.net导出word 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a

    我的服务器:windows server 2008(64位)+microsoft office 2007 企业版 业务:网站导出应聘word简历. 出现以下错误: 检索 COM 类工厂中 CLSID ...

  9. 数据库SQL server规则的创建、查看、修改和规则的绑定与松绑、删除

    用CREATE RULE语句创建规则 创建雇佣日期规则 hire_date_rule CREATE RULE hire_date_rule AS @hire_date>='1980-01-01' ...

  10. Focus

    在尘世中沉浸得越久,越无法集中精力去做好一件事. 缺乏学习!