• 安卓开发之生成XML文件


     1 package com.lidaochen.phonecall;
     2 
     3 import android.net.Uri;
     4 import android.os.Environment;
     5 import android.support.v7.app.AppCompatActivity;
     6 import android.os.Bundle;
     7 import android.util.Log;
     8 import android.view.View;
     9 import android.widget.Button;
    10 import android.widget.TabHost;
    11 import android.widget.ThemedSpinnerAdapter;
    12 import android.widget.Toast;
    13 
    14 import java.io.File;
    15 import java.io.FileOutputStream;
    16 import java.util.ArrayList;
    17 import java.util.List;
    18 import java.util.Map;
    19 
    20 public class MainActivity extends AppCompatActivity{
    21     private List<Sms> smsLists;
    22     @Override
    23     protected void onCreate(Bundle savedInstanceState) {
    24         super.onCreate(savedInstanceState);
    25         setContentView(R.layout.activity_main);
    26         // 初始化我们要备份的数据
    27         smsLists = new ArrayList<Sms>();
    28         for (int i = 0; i < 10; i++)
    29         {
    30             Sms sms = new Sms();
    31             sms.setAddres("10000" + i);
    32             sms.setBody("Hello" + i);
    33             sms.setDate("500" + i);
    34             // 把sms对象加入到集合中
    35             smsLists.add(sms);
    36         }
    37     }
    38     // 按钮点击事件
    39     public void click(View v)
    40     {
    41         // 创建sb对象
    42         StringBuffer sb = new StringBuffer();
    43         // 开始拼接XML文件
    44         sb.append("<?xml version="1.0" encoding="utf-8"?>");
    45         sb.append("<Sms>");
    46         // 开始组拼sms节点
    47         for (Sms sms:smsLists )
    48         {
    49             sb.append("<sms>");
    50 
    51             sb.append("<Addres>");
    52             sb.append(sms.getAddres());
    53             sb.append("</Addres>");
    54 
    55             sb.append("<body>");
    56             sb.append(sms.getBody());
    57             sb.append("</body>");
    58 
    59             sb.append("<date>");
    60             sb.append(sms.getDate());
    61             sb.append("</date>");
    62 
    63             sb.append("</sms>");
    64         }
    65         sb.append("</Sms>");
    66         // 保存数据到SD卡
    67         if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
    68         {
    69             try{
    70                 // 创建FILE类指定我们要保存的位置
    71                 File file = new File(Environment.getExternalStorageDirectory().getPath(), "HelloWorld.xml");
    72                 FileOutputStream fos = new FileOutputStream(file);
    73                 fos.write(sb.toString().getBytes());
    74                 fos.close();
    75             }
    76             catch (Exception e)
    77             {
    78                 e.printStackTrace();
    79             }
    80         }
    81         else
    82         {
    83             Toast.makeText(MainActivity.this, "SD卡不可用!", Toast.LENGTH_SHORT).show();
    84         }
    85     }
    86 }
     1 package com.lidaochen.phonecall;
     2 
     3 public class Sms {
     4     private String addres;
     5     private String body;
     6     private String date;
     7 
     8     public String getAddres() {
     9         return addres;
    10     }
    11 
    12     public String getBody() {
    13         return body;
    14     }
    15 
    16     public String getDate() {
    17         return date;
    18     }
    19 
    20     public void setAddres(String addres) {
    21         this.addres = addres;
    22     }
    23 
    24     public void setBody(String body) {
    25         this.body = body;
    26     }
    27 
    28     public void setDate(String date) {
    29         this.date = date;
    30     }
    31 }
  • 相关阅读:
    MySQL命名、设计及使用规范--------来自标点符的《MySQL命名、设计及使用规范》
    Mybatis 在 insert 之后想获取自增的主键 id
    MyBatis动态SQL foreach标签实现批量插入
    PowerDesigner设置表字段唯一约束
    Hadoop基本介绍
    oracle的行级触发器使用
    RFID-RC522、FM1702SL、M1卡初探
    PHP处理海量样本相似度聚类算法
    RFID(Radio Frequency Identification 射频识别)基本概念
    USB Keyboard Recorder
  • 原文地址:https://www.cnblogs.com/duxie/p/10883498.html
Copyright © 2020-2023  润新知