接着上文《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. c# 游戏策划配置工具

    该工具是提供策划配置excel数据,导出到mysql数据库,以及生成xml文件,和对应的xml解析实体类 实现了程序 excel 列名 ID =P 表示ID这列是唯一字段 =S=300 表示这列类型是 ...

  2. Moon转告给你一个比Log4net更好日志框架--TracerX Logger 及其对应的日志查看器

    一.介绍 TracerX logger是一个易于上手,且拥有众多高级特性的.NET日志框架. 它能够发送输出结果到多目的地(循环文件.事件日志等....).它也能生成文本和二进制文件.它拥有一个强大的 ...

  3. MVC应用程序的生命周期图

  4. C# 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  5. C语言学习006:歌曲搜索

    #include <stdio.h> #include <string.h> //字符串处理库 ]={ "I left my heart in Harvard Med ...

  6. 参考例子,学习Func<T, TResult>委托

    这些天,开发ASP.NET MVC,其间有查找资料,发现一个全新的Func<T, TResult> 委托.让我们在开发时,节省与简化很多. 在开发过程中,我们需要把一个泛型List< ...

  7. iOS学习笔记——触控与手势

    触控 此部分内容已学良久,恨记之甚晚,忙矣,懒矣!本文简而记焉,恐日后忘也. 在iOS的触控事件中,有触控.事件以及响应者这三个角色,一个触摸则代表了一只手指和屏幕接触这个动作所包含的信息:而事件则包 ...

  8. jquery基本选择器匹配多个元素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Java并发编程:并发容器之ConcurrentHashMap(转载)

    Java并发编程:并发容器之ConcurrentHashMap(转载) 下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concu ...

  10. hdu-1213-How Many Tables

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...