android 短信 存储位置,Android 存储学习之保存系统短信到SD卡

既然知道了任务的目的,那我们就直接实现。我们先将系统的短信读出,然后保存到xml文件中,然后将xml文件写到sd卡中。

1: 先将系统短信读出

//得到ContentResolver

ContentResolver cr = getContentResolver();

//查询系统的短信,只需要查询我们关心的字段

Cursor cursor = cr.query(Uri.parse("content://sms"), new String[]{"address", "date", "type", "body"},

null, null, null);

//取出查询到的信息

while(cursor.moveToNext())

{

String address = cursor.getString(cursor.getColumnIndex("address"));

String date = cursor.getString(cursor.getColumnIndex("date"));

String type = cursor.getString(cursor.getColumnIndex("type"));

String body = cursor.getString(cursor.getColumnIndex("body"));

//将每条短信放入List集合中

Message msg = new Message(address, date, type, body);

list.add(msg);

}

2: 其中Message是我定义的关于短信的类

public class Message {

private String address; //号码

private String date; //时间

private String type; //类型:1为发生,2为接受

private String body; //短信内容

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

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 getBody() {

return body;

}

public void setBody(String body) {

this.body = body;

}

@Override

public String toString() {

return "Message [address=" + address + ", date=" + date + ", type="

+ type + ", body=" + body + "]";

}

public Message(String address, String date, String type, String body) {

super();

this.address = address;

this.date = date;

this.type = type;

this.body = body;

}

}

3:将短信的内容拼接为一个xml文件

//使用stringBuffer将短信内容拼接为xml文件

StringBuffer sBuffer = new StringBuffer();

//xml文件的头

sBuffer.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>");

//xml文件的开始标签

sBuffer.append("");

for (Message sms : list) {

sBuffer.append("");

//插入联系人号码字段

sBuffer.append("

");

sBuffer.append(sms.getAddress());

sBuffer.append("

");

//插入联系人类型字段

sBuffer.append("");

sBuffer.append(sms.getType());

sBuffer.append("");

//插入消息的内容

sBuffer.append("

");

sBuffer.append(sms.getBody());

sBuffer.append("");

//插入消息的时间

sBuffer.append("");

sBuffer.append(sms.getDate());

sBuffer.append("");

sBuffer.append("");

}

//结束标签

sBuffer.append("");

4:将xml文件写入到sd卡中

File file = new File("sdcard/sms.xml");

try {

FileOutputStream fos = new FileOutputStream(file);

fos.write(sBuffer.toString().getBytes());

fos.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

整个代码流程为:

public class MainActivity extends Activity {

List list;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list = new ArrayList();

}

public void storageMessage(View v)

{

//得到ContentResolver

ContentResolver cr = getContentResolver();

//查询系统的短信,只需要查询我们关心的字段

Cursor cursor = cr.query(Uri.parse("content://sms"), new String[]{"address", "date", "type", "body"},

null, null, null);

//取出查询到的信息

while(cursor.moveToNext())

{

String address = cursor.getString(cursor.getColumnIndex("address"));

String date = cursor.getString(cursor.getColumnIndex("date"));

String type = cursor.getString(cursor.getColumnIndex("type"));

String body = cursor.getString(cursor.getColumnIndex("body"));

//将每条短信放入List集合中

Message msg = new Message(address, date, type, body);

list.add(msg);

}

//使用stringBuffer将短信内容拼接为xml文件

StringBuffer sBuffer = new StringBuffer();

//xml文件的头

sBuffer.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>");

//xml文件的开始标签

sBuffer.append("");

for (Message sms : list) {

sBuffer.append("");

//插入联系人号码字段

sBuffer.append("

");

sBuffer.append(sms.getAddress());

sBuffer.append("

");

//插入联系人类型字段

sBuffer.append("");

sBuffer.append(sms.getType());

sBuffer.append("");

//插入消息的内容

sBuffer.append("

");

sBuffer.append(sms.getBody());

sBuffer.append("");

//插入消息的时间

sBuffer.append("");

sBuffer.append(sms.getDate());

sBuffer.append("");

sBuffer.append("");

}

//结束标签

sBuffer.append("");

File file = new File("sdcard/sms.xml");

try {

FileOutputStream fos = new FileOutputStream(file);

fos.write(sBuffer.toString().getBytes());

fos.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}当点击按钮保存系统短信后,可以在sd卡的目录下找到sms.xml文件

72ef0604db5be26f219b67b14b2237f5.png

导出xml文件打开:

0d3e4f2cbc0d13ef392194e892b28770.png

以其中一条短信为例:

f8b72011fbd080118c4a28fedd67c841.png

ok。关于如何备份短信就讲到这里

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/longwang155069/article/details/47254521

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值