Androiid备份短信

来源:互联网 发布:mac怎么用html5看视频 编辑:程序博客网 时间:2024/05/22 03:07

xml文件代码:

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:onClick="bfdx"        android:text="短信的备份1" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="89dp"        android:onClick="bfdx2"        android:text="短信的备份2" /></RelativeLayout>
dxxx类文件代码:

package com.example.shenchao3.domain;public class dxxx {private long date;private int type;private String body;private String address;private int id;public dxxx() {}public dxxx(long date, int type, String body, String address,int id) {this.date = date;this.type = type;this.body = body;this.address = address;this.id = id;}public long getDate() {return date;}public int getId() {return id;}public void setId(int id) {this.id = id;}public void setDate(long date) {this.date = date;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}

main文件代码:

package com.example.shenchao3;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.xmlpull.v1.XmlSerializer;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Xml;import android.view.View;import android.widget.Toast;import com.example.shenchao3.domain.dxxx;public class MainActivity extends Activity {private List<dxxx> dxxx;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dxxx = new ArrayList<dxxx>();for (int i = 0; i < 10; i++) {Random random = new Random();long number = 1530000000;// 获取当前系统时间、短信的内容、短信的内容、数据dxxx.add(new dxxx(System.currentTimeMillis(),random.nextInt(2) + 1, "短信内容" + i, Long.toString(number + i), i));}}/** * 备份手机的的短信 */public void bfdx(View view) {StringBuilder sb = new StringBuilder();sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");// 头sb.append("<dxxx>");// 根节点for (dxxx info : dxxx) {sb.append("<sms>");sb.append("<address>");sb.append(info.getAddress());sb.append("</address>");sb.append("<type>");sb.append(info.getType());sb.append("</type>");sb.append("<body>");sb.append(info.getBody());sb.append("</body>");sb.append("<date>");sb.append(info.getDate());sb.append("</date>");sb.append("</sms>");}sb.append("</dxxx>");// 获取外部存储的路径 Environment.getExternalStorageDirectory(),try {File file = new File(Environment.getExternalStorageDirectory(),"backup.xml");// 写入输入流 FileOutputStreamFileOutputStream fos = new FileOutputStream(file);fos.write(sb.toString().getBytes()); // getBytes()是将一个字符串转化为一个字节数组fos.close();Toast.makeText(this, "备份成功", 0).show();} catch (IOException e) {e.printStackTrace();Toast.makeText(this, "备份失败", 0).show();}}/** * 备份手机的的短信2,使用xml序列化器 */public void bfdx2(View view) {// XmlSerializer是序列化器try {XmlSerializer serializer = Xml.newSerializer();// 写的路径、用什么编码方式生成File file = new File(Environment.getExternalStorageDirectory(),"backup2.xml");FileOutputStream os = new FileOutputStream(file);// 指定写入的路径和方式serializer.setOutput(os, "utf-8");serializer.startDocument("utf-8", true);// 写入文档的头serializer.startTag(null, "smss");// 开始节点for (dxxx info : dxxx) {serializer.startTag(null, "smss");// 开始标签serializer.attribute(null, "id", info.getId() + "");serializer.startTag(null, "body");serializer.text(info.getBody());serializer.endTag(null, "body");serializer.startTag(null, "address");serializer.text(info.getAddress());serializer.endTag(null, "address");serializer.startTag(null, "type");serializer.text(info.getType() + "");serializer.endTag(null, "type");serializer.startTag(null, "date");serializer.text(info.getDate() + "");serializer.endTag(null, "date");serializer.endTag(null, "smss");// 结束标签}serializer.endTag(null, "smss");// 结束节点serializer.endDocument();// 写入文档的头的结束os.close();Toast.makeText(this, "备份成功", 0).show();} catch (IOException e) {e.printStackTrace();Toast.makeText(this, "备份失败", 0).show();}}}



0 0