以文件的方式在sd卡或者内存存储信息,这个方法不是特别方便,android提供了一个面向对象的方式,以xml文件存储,即序列化。
把所有要存储的信息以xml文件的格式存储,这个不仅可以避免用户信息中存在非标准字符引起的错误,而且更加简洁明了。
本文我们会构造大量短信,并且以xml的形式存储这些短信信息。本文提供实现了两种方式存储为xml文件,一种是认为创造一个xml文件,并且按照xml文件的格式填写信息。另外一种就是android标准的借口。
package com.hm.xmlserialize; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.xmlpull.v1.XmlSerializer; import com.hm.xmlserialize.SmsInfo.SmsInfo; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Xml; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private List<SmsInfo> smsInfos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Random random = new Random(); smsInfos = new ArrayList<SmsInfo>(); //构造10条短信信息,包括时间、发送/接收、短信内容、电话号码等 long number = 1350000000; for(int i = 0;i < 10;i++) { smsInfos.add(new SmsInfo(i,System.currentTimeMillis(),random.nextInt(2),"短信内容<"+i, Long.toString(number+i))); } } /** * xml序列化 * @param view */ public void backup2(View view){ XmlSerializer xmlSerializer = Xml.newSerializer(); try { File file = new File(Environment.getExternalStorageDirectory(),"backup2.xml"); FileOutputStream fos = new FileOutputStream(file); //初始化序列器,指定xml文件写到哪个文件,以什么编码格式 xmlSerializer.setOutput(fos, "utf-8"); xmlSerializer.startDocument("utf-8", true); xmlSerializer.startTag(null, "smss"); for(SmsInfo info:smsInfos){ xmlSerializer.startTag(null, "sms"); xmlSerializer.attribute(null, "id", info.getId()+""); xmlSerializer.startTag(null, "data"); xmlSerializer.text(info.getData()+""); xmlSerializer.endTag(null,"data"); xmlSerializer.startTag(null, "address"); xmlSerializer.text(info.getAddress()); xmlSerializer.endTag(null,"address"); xmlSerializer.startTag(null, "body"); xmlSerializer.text(info.getBody()); xmlSerializer.endTag(null,"body"); xmlSerializer.startTag(null, "type"); xmlSerializer.text(info.getType()+""); xmlSerializer.endTag(null,"type"); xmlSerializer.endTag(null,"sms"); } xmlSerializer.endTag(null, "smss"); xmlSerializer.endDocument(); fos.close(); Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } } /** * 备份短信 * @param view */ public void backup(View view){ StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.append("<smss>"); for(SmsInfo info:smsInfos){ 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.getData()); sb.append("</date>"); sb.append("</sms>"); } sb.append("</smss>"); File file = new File(Environment.getExternalStorageDirectory(),"backup.xml"); try { FileOutputStream fos = new FileOutputStream(file); fos.write(sb.toString().getBytes()); fos.close(); Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } } }上面backup方法是认为构造一个xml文件,按照xml文件的格式填写了标签及信息。backup2按照标准的借口填写需要保存的信息。
XmlSerializer xmlSerializer = Xml.newSerializer();获得一个xml序列化器。
xmlSerializer.setOutput(fos, "utf-8");设置序列化器的输出流及编码格式
xmlSerializer.startDocument("utf-8", true);xml文件的Document信息
xml文件的标签作为Tag在这里表示,有startTag就有endTag,在startTag和endTag之间设置信息即可。短信的信息代码如下:
package com.hm.xmlserialize.SmsInfo; public class SmsInfo { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } private long data; private String body; private int type; private String address; public SmsInfo(){ } public SmsInfo(int id,long data, int type, String body, String address) { super(); this.data = data; this.body = body; this.type = type; this.address = address; this.id = id; } public long getData() { return data; } public void setData(long data) { this.data = data; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }整个app的布局文件如下:
<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:id="@+id/bt_save" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerInParent="true" android:onClick="backup" android:text="@string/save"/> <Button android:id="@+id/bt_save2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_below="@id/bt_save" android:onClick="backup2" android:text="@string/save2"/> </RelativeLayout>布局采用相对布局,定义了两个按键,分别对应两种存储方式。
当然保存信息到sd卡要添加些sd卡权限,在AndroidManifest.xml文件中添加下面一句话即可:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>