[安卓基础]学习第二天

一、测试的相关概念

[1]好的软件是测试出来的

1.1 根据是否知道源代码
    黑盒
    白盒
1.2 根据测试粒度
    方法测试
    单元测试
    集成测试
    系统测试
1.3 根据测试的暴力程度
    压力
    冒烟
    - Google工程师给我们提供了一个monkey的指令进行压力测试

二、单元测试

[1]定义一个类继承AndroidTestCase
package com.elnui.hm_07;

import android.test.AndroidTestCase;

public class CalcTest extends AndroidTestCase {

    //写测试方法
    public void testAdd(){

        Calc calc = new Calc();

        int result = calc.add(1,2);

        //断言
        assertEquals(3,result);
    }
}
[2]在清单文件配置<instrumentation>和<uses-library>,如下
    <!-- 配置函数库 -->
    <application>
        <uses-library android:name="android.test.runner"/>
        .....
    </application>
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:label="Tests for My App"
        android:targetPackage="com.elnui.hm_07" />
    <!-- 上面的包名需要更改 -->
    ....
[3]也可以手动新建Android Test 项目,拷贝其中的清单文件即可

三、日志猫的使用

[1]使用Log类,调用它的静态方法即可
    Log.v(tag,msg);     --- 绿色
    Log.i(tag,msg);     --- 蓝色
    Log.d(tag,msg);     --- 黑色
    Log.w(tag,msg);     --- 黄色
    Log.e(tag,msg);     --- 红色
[2]过滤信息通过过滤器

四、login登录案列

//判断单选框是否选中
cb_ischeck.isChecked()
Toast.makeText(MainActivity.this, "save_success.", 1).show();

五、使用上下文获取常见目录

String path = context.getFilesDir().getPath();

六、登录数据存储到SD卡

需要获取权限,在配置文件中添加
    android.permission.WRITE_EXTERNAL_STORAGE
//判断内存卡是否可用
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
    Toast.makeText(MainActivity.this, "sd_success.", 1).show();
}
String sd_path = Environment.getExternalStorageDirectory().getPath();

七、获取SD卡可用空间

// 获取SD卡空间大小
File file = Environment.getExternalStorageDirectory();
long totalSpace = file.getTotalSpace();
long usableSpace = totalSpace - file.getUsableSpace();
// 转换格式
String str_totalSpace = Formatter.formatFileSize(this, totalSpace);
String str_usableSpace = Formatter.formatFileSize(this, usableSpace);
// 显示
tv_total_size.setText("total: "+str_totalSpace);
tv_free_size.setText("free: " + str_usableSpace);

八、文件权限的介绍

第一位、文件类型。-表示文件,d表示目录
后三位表示当前用户
再后三位表示当前用户所在的组,
后三位表示其他用户
    ---rwx

Open Declaration   FileOutputStream android.content.ContextWrapper.openFileOutput(String name, int mode) throws FileNotFoundException

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

Overrides: openFileOutput(...) in Context
Parameters:name The name of the file to open; can not contain path separators.
mode Operating mode. 
- Use 0 or MODE_PRIVATE for the default operation 
- MODE_APPEND to append to an existing file
- MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions.Returns:FileOutputStream Resulting output stream.Throws:FileNotFoundException
补充:修改文件权限
说明:可读可写可执行 7
adb shell命令行下进去目录,输入:
    chmod   777 private.txt

九、SharedPreferences介绍 ★★★★★

使用方法:
/*
 *  使用sp存
 *      name- 会自动生成一个xml文件
 *      mode- 代表模式
 *      第一步、获取sp实例
 *      第二步、获取编辑器
 *      第三步、存数据
 *      第四部、提交
 **/
SharedPreferences sp = getSharedPreferences("config", 0);
Editor ed = sp.edit();
ed.putString("name", name);
ed.putString("pwd", pwd);
//记住一定要提交
ed.commit();
-------------------------------------------------------
/*
*  使用sp取
* */
sp = getSharedPreferences("config", 0);
String name = sp.getString("name", "");
String pwd = sp.getString("name", "pwd");
et_username.setText(name);
et_pwd.setText(pwd);

十、xml序列化

[1]StringBuffer自己拼装
StringBuffer sb = new StringBuffer();

// 1.3 Start split joint xml header
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

// 1.4 Split joint xml root
sb.append("<smss>");

// 1.5 Split joint xml node
for(Sms sms : smsList){
    sb.append("<sms>");

    // Split joint address node
    sb.append("<address>");
    sb.append(sms.getAddress());
    sb.append("</address>");

    //Split joint body node 
    sb.append("<body>");
    sb.append(sms.getBody());
    sb.append("</body>");

    sb.append("<date>");
    sb.append(sms.getDate());
    sb.append("</date>");

    sb.append("</sms>");
}

sb.append("</smss>");
[2]使用XmlSerializer接口
    - 1. 获取serializer实例
    - 2. 使用serializer序列化器
    - 3. 写文档头
    - 4. 写xml根节点
    - 5. 循坏写其他节点
    - 6. 闭合根节点
    - 7. 写文档尾
// 1.1 Get instance of serializer
XmlSerializer serializer = Xml.newSerializer();

try {
    // 1.2 设置xmlserializer序列化器参数
    File file = new File(Environment.getExternalStorageDirectory().getPath(),"backup.xml");
    FileOutputStream fos = new FileOutputStream(file);
    serializer.setOutput(fos, "utf-8");

    // 1-3. 开始写文档开头
    serializer.startDocument("utf-8", true);

    // write xml root node
    serializer.startTag(null, "smss");

    // Write node 
    for(Sms sms : smsList){
        serializer.startTag(null, "sms");   // sms node start

        // address node 
        serializer.startTag(null, "address");
        serializer.text(sms.getAddress());
        serializer.endTag(null, "address");

        // body node
        serializer.startTag(null, "body");
        serializer.text(sms.getBody());
        serializer.endTag(null, "body");

        // date node
        serializer.startTag(null, "date");
        serializer.text(sms.getDate());
        serializer.endTag(null, "date");

        serializer.endTag(null, "sms");     // sms nde end
    }

    // Write xml root node end
    serializer.endTag(null, "smss");


    // end of the document
    serializer.endDocument();

    fos.close();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

十一、xml解析

xml的数据来源于服务器,服务器开发人员通过服务器的技术把数据准备一个xml返回给对应的开发人员,开发人员解析即可
步骤:
    [1]获取XmlPullParser
    [2]设置XmlPullParser的参数
    [3]获取解析文档的事件类型
    [3]判断解析的是哪个标签
public static List<Channel> parseXml(InputStream e) throws Exception{
    // 声明集合对象
    List<Channel> weatherList = null;
    Channel channel = null;

    // 获取xmlPullParse解析的实例
    XmlPullParser parse = Xml.newPullParser();
    parse.setInput(e,"utf-8");

    // 获取事件类型
    int eventType = parse.getEventType();
    while(eventType != XmlPullParser.END_DOCUMENT){
        // 判断解析到了那个节点
        switch(eventType){
        case XmlPullParser.START_TAG:
            if("weather".equals(parse.getName())){
                weatherList = new ArrayList<Channel>();

            }else if("channel".equals(parse.getName())){
                //创建channel对象
                channel = new Channel();
                // 获取id值
                String id = parse.getAttributeValue(0);
                channel.setId(id);

            }else if("city".equals(parse.getName())){
                String city = parse.nextText();
                channel.setCity(city);

            }else if("temp".equals(parse.getName())){
                String temp = parse.nextText();
                channel.setTemp(temp);

            }else if("wind".equals(parse.getName())){
                String wind = parse.nextText();
                channel.setWind(wind);

            }else if("pm250".equals(parse.getName())){
                String pm250 = parse.nextText();
                channel.setPm250(pm250);

            }
            break;
        case XmlPullParser.END_TAG:
            if("channel".equals(parse.getName())){
                // 把javabean存到集合中
                weatherList.add(channel);
            }
            break;
        }
        //不断解析
        eventType = parse.next();
    }
    return weatherList;
}

—————————————————————————————–

image

/* UserInfoUtils.java */
package com.elnui.hm_07;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Environment;

public class UserInfoUtils {
    public static boolean saveInfo(Context context,String username, String pwd){
        try {
            String result = username + "##" + pwd;
            /*****************************************************************
             *       保存数据到包目录下
            String path = context.getFilesDir().getPath();
            //指定存储位置
            File file = new File(path);
            FileOutputStream fos = new FileOutputStream(file + "info.txt");
            ******************************************************************/
            //直接用Context即可拿到文件输入流
            FileOutputStream fos = context.openFileOutput("info.txt", 0);
            fos.write(result.getBytes());
            fos.close();


            // 保存到内存卡info.txt,需要权限
            String sd_path = Environment.getExternalStorageDirectory().getPath();
            File file = new File(sd_path,"info.txt");
            FileOutputStream fos1 = new FileOutputStream(file);
            fos1.write(result.getBytes());
            fos1.close();

            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    //读取用户信息
    public static Map<String, String> readInfo(Context context) {
        Map<String, String> maps = new HashMap<String, String>();
        try {
            /*
            String path = context.getFilesDir().getPath();
            File file = new File(path + "info.txt");
            FileInputStream fis = new FileInputStream(file);
            */
            //直接用context拿到文件输出流
            FileInputStream fis = context.openFileInput("info.txt");
            BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));
            String content = bufr.readLine();

            //切割字符串
            String[] splits = content.split("##");
            String name = splits[0];
            String pwd = splits[1];
            maps.put(name, pwd);
            fis.close();

            //读取内存卡中的info.txt,需要权限
            String sd_path = Environment.getExternalStorageDirectory().getPath();
            File file = new File(sd_path,"info.txt");
            FileInputStream fis1 = new FileInputStream(file);
            BufferedReader bufr1 = new BufferedReader(new InputStreamReader(fis1));
            String content1 = bufr.readLine();

            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}x
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值