接着上文《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. Elasticsearch嵌套聚合

    Elasticserch在新版本中支持聚合操作,而聚合操作也可以嵌套使用,方法如下: curl -XGET 10.4.44.19:9200/test/test/_search?pretty -d ' ...

  2. MVC学习之前必须掌握的c#基础知识

    一.类自动属性 public class Person { //自动属性 public string Name { get; set; } private int _age; public int a ...

  3. JAVA - Collections用法总结

    一生二,二生三,三生万物,基础永远是一个计算机人的立身之本.数据结构这门课程的分析奠定了工程师对各种平台中的容器类,集合类的理解基础,正如好多人所说的,如果你对某个平台的集合类理解的不透彻,很可能,你 ...

  4. 代码创建数据库_表--SqlServer数据库

    /*1.创建数据库的时候需要设置的基本属性: 数据库名称 逻辑名称 初始大小 文件增长 路径*/ --语法: -- create database 数据库名称 -- on [primary]--创建数 ...

  5. js一篇汇总

    一.js的数据类型和变量 JavaScript 有六种数据类型.主要的类型有 number.string.object 以及 Boolean 类型,其他两种类型为 null 和 undefined. ...

  6. 为ASP.NET配置IIS7服务器支持十万个同时请求

    1. IIS7中应用程序池队列长度调整为65535(默认为1000) 打开IIS7管理器,选择应用程序池,右键选择应用程序池,选择高级设置,把1000改为65535

  7. WITH RECURSIVE and MySQL

    WITH RECURSIVE and MySQL If you have been using certain DBMSs, or reading recent versions of the SQL ...

  8. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  9. tmpfs:一种基于内存的文件系统

    tmpfs是一种基于内存的文件系统, tmpfs有时候使用rm(物理内存),有时候使用swap(磁盘一块区域).根据实际情况进行分配. rm:物理内存.real memery的简称? 真实内存就是电脑 ...

  10. Bash中的任务(job)管理

    本来不准备写这篇博客的,因为任务管理(job管理)非常非常常用,以至于觉得根本没有必要去写这样一个东西.但想了下,还是记录一下吧,也许有人会用到呢. 不知你是否碰到过这样的情况,当你兴致勃勃的打开VI ...