Android Basics (9): Serialization of XML files

Source: Internet
Author: User

One: interface design

<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:id= "@+id/bt"
android:onclick= "Backsms"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "SMS Backup"/>
<button
android:layout_below= "@id/BT"
android:onclick= "BACKSMS2"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Second Way SMS Backup"/>
</RelativeLayout>


Two: Virtual one SMS Information class Smsinfo.java

Package com.cloud.sms;
public class Smsinfo {
private long date;
private int type;
Private String body;
Private String address;
private int id;
Public Smsinfo () {
}
Public Smsinfo (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 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;
}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
}


Three: Write the main program code

Package COM.EXAMPLE.XML1;


Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Random;
Import Org.xmlpull.v1.XmlSerializer;
Import Com.cloud.sms.SmsInfo;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import android.util.Xml;
Import Android.view.View;
Import Android.widget.Toast;


public class Mainactivity extends Activity {
Private list<smsinfo> Smsinfo;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Simulate some SMS messages for backup
Smsinfo=new arraylist<smsinfo> ();
Random random=new random ();
Long number=1350000000;
for (int i=0;i<10;i++) {
Smsinfo.add (New Smsinfo (System.currenttimemillis (), Random.nextint (2) +1,
"SMS Content" +i,long.tostring (number+i), i));
}
}
/**
* Back up your phone's SMS
* @param v
*/
public void Backsms (View v) {
Backing up files in XML format
StringBuilder sb=new StringBuilder ();
Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");
Sb.append ("<smss>");
for (Smsinfo Info:smsinfo) {
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 ("</smss>");
try {
File File=new file (Environment.getexternalstoragedirectory (), "Backup.xml");
FileOutputStream fos=new fileoutputstream (file);
Fos.write (Sb.tostring (). GetBytes ());
Fos.close ();
Toast.maketext (This, "Backup succeeded", 0). Show ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
Toast.maketext (This, "Backup failed", 0). Show ();
}
}

public void BackSms2 (View v) {
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 (Smsinfo Info:smsinfo) {
Serializer.starttag (NULL, "SMS");
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, "SMS");
}
Serializer.endtag (NULL, "SMSS");
Os.close ();
Toast.maketext (This, "Backup succeeded", 0). Show ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
Toast.maketext (This, "Backup failed", 0). Show ();
}
}

}


IV: Permissions used by the program

Android.permission.WRITE_EXTERNAL_STORAGE


Five: Program Run results


Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

Android Basics (9): Serialization of XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.