Android 存储学习之保存系统短信到SD卡

2022-05-08 16:35:11 浏览数 (1)

本节学习,保存系统短信到SD卡中。既然是要保存系统短信到SD卡中,前提是先要拿到系统的短信,关于如何读取系统的短信,请看我的关于ContentProvider文章:

Android 四大组件学习之ContentProvider三

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

1: 先将系统短信读出

代码语言:javascript复制
//得到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是我定义的关于短信的类

代码语言:javascript复制
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文件

代码语言:javascript复制
//使用stringBuffer将短信内容拼接为xml文件
    	StringBuffer sBuffer = new StringBuffer();
    	//xml文件的头
    	sBuffer.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>");
    	//xml文件的开始标签
    	sBuffer.append("<Message>");
    	for (Message sms : list) {
			sBuffer.append("<sms>");
			
			//插入联系人号码字段
			sBuffer.append("<address>");
			sBuffer.append(sms.getAddress());
			sBuffer.append("</address>");
			
			//插入联系人类型字段
			sBuffer.append("<type>");
			sBuffer.append(sms.getType());
			sBuffer.append("</type>");
			
			//插入消息的内容
			sBuffer.append("<body>");
			sBuffer.append(sms.getBody());
			sBuffer.append("</body>");
			
			//插入消息的时间
			sBuffer.append("<date>");
			sBuffer.append(sms.getDate());
			sBuffer.append("</date>");
			
			sBuffer.append("</sms>");
		}
    	//结束标签
    	sBuffer.append("</Message>");

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

代码语言:javascript复制
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();
		}

整个代码流程为:

代码语言:javascript复制
public class MainActivity extends Activity {

	List<Message> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        list = new ArrayList<Message>();
    }
    
    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("<Message>");
    	for (Message sms : list) {
			sBuffer.append("<sms>");
			
			//插入联系人号码字段
			sBuffer.append("<address>");
			sBuffer.append(sms.getAddress());
			sBuffer.append("</address>");
			
			//插入联系人类型字段
			sBuffer.append("<type>");
			sBuffer.append(sms.getType());
			sBuffer.append("</type>");
			
			//插入消息的内容
			sBuffer.append("<body>");
			sBuffer.append(sms.getBody());
			sBuffer.append("</body>");
			
			//插入消息的时间
			sBuffer.append("<date>");
			sBuffer.append(sms.getDate());
			sBuffer.append("</date>");
			
			sBuffer.append("</sms>");
		}
    	//结束标签
    	sBuffer.append("</Message>");
    	
    	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文件

导出xml文件打开:

以其中一条短信为例:

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

0 人点赞