android_day02

1.测试的相关概念
    [1]好的软件不是开发出来的  是不断测试出来
    [2]当当  1 2 3 -8 京东:
    
    1.1根据是否知道源代码
        黑盒
        白盒
    1.2根据测试的粒度
        方法测试
        单元测试
        集成测试
        系统测试
    1.3根据测试的暴力程度
        压力    12306
        冒烟
        谷歌工程师给我们提供了一个monkey+1000指令可以进行压力测试
        
2.单元测试(即java的main方法不能直接用了)
    [1]定义一个类继承AndroidTestCase
    [2]在清单文件配置instrumentation和uses-library
        <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:targetPackage="com.itheima.unit" />
        
         <uses-library android:name="android.test.runner" />
        
        例子:(清单文件):
                <?xml version="1.0" encoding="utf-8"?>
                <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                    package="com.itheima.unit"
                    android:versionCode="1"
                    android:versionName="1.0" >

                    <uses-sdk
                        android:minSdkVersion="8"
                        android:targetSdkVersion="19" />
                    
                    <instrumentation
                        android:name="android.test.InstrumentationTestRunner"
                        android:targetPackage="com.itheima.unit" />

                    <application
                        android:allowBackup="true"
                        android:icon="@drawable/ic_launcher"
                        android:label="@string/app_name"
                        android:theme="@style/AppTheme" >
                        
                        <!-- 配置函数库 -->
                        <uses-library android:name="android.test.runner" />
                        
                        <activity
                            android:name="com.itheima.unit.MainActivity"
                            android:label="@string/app_name" >
                            <intent-filter>
                                <action android:name="android.intent.action.MAIN" />

                                <category android:name="android.intent.category.LAUNCHER" />
                            </intent-filter>
                        </activity>
                    </application>

                </manifest>
        
    [3]如果ppt搞丢了 可以自己手动创建一个Android测试工程  在测试工程里面的清单文件有
    
    
3.日志猫的使用
        (import android.util.Log;)
        Log.v(tag, "我是verbose级别");  ----蓝色
        Log.i(tag, "我是info级别");        ----绿色
        Log.d(tag, "我是debug级别");    ----黑色
        Log.w(tag, "我是warn级别");        ----黄色
        Log.e(tag, "我是error级别");    ----红色
        

4.login登录案例
    [1]画ui
        <LinearLayout 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:orientation="vertical"
            tools:context=".MainActivity" >

            <EditText
                android:id="@+id/et_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入用户名" />

            <EditText
                android:id="@+id/et_userpassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入密码"
                android:password="true" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="20dp" >

                <CheckBox
                    android:id="@+id/cb_ischeck"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="记住用户名密码" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:onClick="login"
                    android:text="登录" />
            </RelativeLayout>

        </LinearLayout>
        
    [2]根据我们画的ui,写对应的业务逻辑
        //2.写按钮的点击事件
        public void login(View v){
            //2.1获取用户名和密码
            String name=et_name.getText().toString().trim();
            String pwd=et_userpassword.getText().toString().trim();
            //2.2判断name和pwd是否为空
            if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                Toast.makeText(this, "用户名或密码不能为空", 1).show();
            }else{
                //2.3进行登录的逻辑
                System.out.println("连接服务器  进行登录 等我们讲到  第四天  网络  编程  再说");
                
                if(cb_ischeck.isChecked()){
                    //2.4把用户名和密码的数据给我存起来
                    boolean result=UserInfoUtils.saveInfo(name, pwd);
                    if(result){
                        Toast.makeText(this, "保存成功", 1).show();
                    }else{
                        Toast.makeText(this, "保存失败", 1).show();
                    }
                    
                    
                }else{
                    Toast.makeText(this, "请勾选cb", 1).show();
                }
                
            }
            
        }
    
    [3]写一个保存用户名和密码的业务方法
        public class UserInfoUtils {

            //保存用户名和密码的业务方法
            public static boolean saveInfo(String username,String pwd){
                try {
                    String result=username+"##"+pwd;
                    //1.创建file类指定我们要把数据存储的位置
                    File file=new File("/data/data/com.itheima.login/info.txt");
                    //2.创建一个文件输出流
                    FileOutputStream fos=new FileOutputStream(file);
                    
                    fos.write(result.getBytes());
                    fos.close();
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                
            }
            
            //读取用户的信息
            public static Map<String,String> readInfo(){
                try {
                    //1.定义map
                    Map<String,String> maps=new HashMap<String, String>();
                    File file=new File("/data/data/com.itheima.login/info.txt");
                    FileInputStream fis=new FileInputStream(file);
                    BufferedReader bufr=new BufferedReader(new InputStreamReader(fis));
                    String content=bufr.readLine();//读取数据
                    
                    //切割字符串  封装到map集合中
                    String[] splits = content.split("##");
                    String name=splits[0];
                    String pwd=splits[1];
                    //把name和pwd放入map中
                    maps.put("name", name);
                    maps.put("pwd", pwd);
                    fis.close();
                    return maps;
                    
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
                
                
            }
        }

    
    
5.使用上下文获取常见目录
    [1]String path = getFilesDir().getPath();
    [2]使用上下文快速获取文件的输出流和输入流
        FileOutputStream fos=context.openFileOutput("infoo.txt", 0);
        FileInputStream fis=context.openFileInput("infoo.txt");
        
6.登录数据存储到sd卡
    [1]获取sd卡的目录
    String sdPath=Environment.getExternalStorageDirectory().getPath();
    [2]如何判断sd卡状态是否可用
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){}
    
    【注意】需要权限sd卡写:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

7.获取sd卡的可用空间
    //1.找到我们关心的控件
    TextView tv_total_size=(TextView)findViewById(R.id.textView1);
    TextView tv_useanle_size=(TextView)findViewById(R.id.textView2);
    
    //2.获取sd卡总大小  和可用空间
    File file=Environment.getExternalStorageDirectory();
    long totalSpace = file.getTotalSpace();//总大小
    long usableSpace = file.getUsableSpace();//可用空间
    
    //3.转换数据格式
    String formatTotalSpace = Formatter.formatFileSize(this, totalSpace);
    String formatUseableSpace = Formatter.formatFileSize(this, usableSpace);
    
    //4.展示到textview上
    tv_total_size.setText("总大小:"+formatTotalSpace);
    tv_useanle_size.setText("可用的:"+formatUseableSpace);

8.文件权限的介绍
    d rwx rwx rwx   第一个表示文件的类型,
                    然后后三位表示当前用户,
                    再后三位表示当前用户所在的组
                    再后三位表示其他用户
    修改文件的权限  使用linux下一个指令chmod
    chmod 777 private.txt

9.SharedPreferences介绍
    [1]获取sp的实例
        SharedPreferences sp = getSharedPreferences("config", 0);
    [2]获取编辑器
        Editor edit = sp.edit();        
    [3]存数据
        edit.putString("name", name);
        edit.putString("pwd", pwd);
    [4]最后一步  一定要commit
        edit.commit();
        
10.xml的序列化
    [1]StringBuffer自己组拼(生成xml在sdcard上)
        //点击按钮  通过stringbuffer的方式生成一个xml文件
        public void click(View v){
            //1.创建sb对象
            StringBuffer sb=new StringBuffer();
            
            //2.开始组拼xml文件头
            sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            //3.开始组拼xml根节点
            sb.append("<smss>");
            //4.开始组拼sms节点
            for (Sms sms : smsLists) {
                sb.append("<sms>");
                
                //5.开始组拼address节点
                sb.append("<address>");
                sb.append(sms.getAddress());
                sb.append("</address>");
                
                //6.开始组拼body节点
                sb.append("<body>");
                sb.append(sms.getBody());
                sb.append("</body>");
                
                //7.开始组拼date节点
                sb.append("<date>");
                sb.append(sms.getDate());
                sb.append("</date>");
                
                
                sb.append("</sms>");
            }
            
            
            sb.append("</smss>");
            
            //8.把数据保存到sd卡中
            try {
                File file=new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup.xml");
                FileOutputStream fos=new FileOutputStream(file);
                fos.write(sb.toString().getBytes());
                fos.close();//关闭流
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    [2]使用XmlSerializer序列化器  (生成xml在sdcard上)生成xml的步骤
        1.获取XmlSerializer的实例
            XmlSerializer serializer = Xml.newSerializer();
        2.设置XmlSerializer的序列化参数
            File file=new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup2.xml");
            FileOutputStream fos=new FileOutputStream(file);
            serializer.setOutput(fos,"utf-8");
        3.开始写xml的文档开头
            serializer.startDocument("utf-8", true);
        4.写xml根节点
            serializer.startTag(null, "smss");
        5.循环写其他节点
            for (Sms sms : smsLists) {
                serializer.startTag(null, "sms");
                
                //6.开始address节点
                serializer.startTag(null, "address");
                serializer.text(sms.getAddress());
                serializer.endTag(null, "address");
                
                //7.开始body节点
                serializer.startTag(null, "body");
                serializer.text(sms.getBody());
                serializer.endTag(null, "body");
                
                //8.开始date节点
                serializer.startTag(null, "date");
                serializer.text(sms.getDate());
                serializer.endTag(null, "date");
                
                //sms节点结束
                serializer.endTag(null, "sms");
                
            }
        6.结束并关闭流
            serializer.endTag(null, "smss");
            serializer.endDocument();
            fos.close();
            

11.xml的解析
    [1]xml的数据来源 来源于服务器 服务器开发人员通过服务器的技术把数据准备一个xml返回给客户端
    [2]对应Android的开发人员 需要做解析的操作 把我们关心的数据取出来就可以了
    [3]服务器是以流的形式把数据返回
    
    解析步骤:
        1.获取XmlPullParser解析的实例
        XmlPullParser parser = Xml.newPullParser();
        2.设置XmlPullParser的参数
        parser.setInput(in, "utf-8");
        3.获取解析的文档的事件类型
        int type=parser.getEventType();
        4.具体判断一下解析的是哪个标签
        while (type!=XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_TAG://解析开始标签
                //4.具体判断一下  解析到哪个开始标志
                if("weather".equals(parser.getName())){
                    //创建一个集合对象
                    weatherLists=new ArrayList<Channel>();
                }else if("channel".equals(parser.getName())){
                    //6.创建Channel对象
                    channel=new Channel();
                    //7.获取id值
                    String id=parser.getAttributeName(0);
                    channel.setId(id);
                    
                }else if("city".equals(parser.getName())){
                    //8.获取city的数据
                    String city=parser.nextText();
                    channel.setCity(city);
                    
                }else if("temp".equals(parser.getName())){
                    //8.获取temp的数据
                    String temp=parser.nextText();
                    channel.setTemp(temp);
                    
                }else if("wind".equals(parser.getName())){
                    //8.获取wind的数据
                    String wind=parser.nextText();
                    channel.setWind(wind);
                    
                }else if("pm250".equals(parser.getName())){
                    //8.获取pm250的数据
                    String pm250=parser.nextText();
                    channel.setPm250(pm250);
                    
                }
                
                
                break;
                
            case XmlPullParser.END_TAG://解析结束标签
                //判断要解析的结束标签
                if("channel".equals(parser.getName())){
                    //把javabean对象存到集合中
                    weatherLists.add(channel);
                }
                break;

            default:
                break;
            }
            
            //不停的向下解析
            type = parser.next();
            
        }
        
        
        
12.今日总结
    [1]生成/data/data/com.itheima.login/info.txt文件,权限-rw是-------
        //1.创建file类指定我们要把数据存储的位置
        File file=new File("/data/data/com.itheima.login/info.txt");
        //2.创建一个文件输出流
        FileOutputStream fos=new FileOutputStream(file);
        fos.write(result.getBytes());
        fos.close();
    [2]生成/data/data/com.itheima.login/files/info2.txt文件,权限是-rw-------
        String path = context.getFilesDir().getPath();
        File file=new File(path,"info2.txt");
        //2.创建一个文件输出流
        FileOutputStream fos=new FileOutputStream(file);
        fos.write(result.getBytes());
        fos.close();
    [3]生成/data/data/com.itheima.login/files/infoo.txt文件,权限是-rw-rw----,现在是private模式,还有别的模式让其他用户可读或可写
        FileOutputStream fos=context.openFileOutput("infoo.txt", 0);
        fos.write(result.getBytes());
        fos.close();
    [4]生成/mnt/sdcard/haha.txt文件,权限是----rwxr-x
        String sdPath=Environment.getExternalStorageDirectory().getPath();
        File file=new File(sdPath,"haha.txt");            
        FileOutputStream fos=new FileOutputStream(file);            
        fos.write(result.getBytes());
        fos.close();
    [5]生成/data/data/com.itheima.login/shared_prefs/config.xml文件,权限是-rw-rw----,现在是private模式,还有别的模式让其他用户可读或可写
        SharedPreferences sp= getSharedPreferences("config", 0);
        Editor edit = sp.edit();
        edit.putString("name", name);
        edit.putString("pwd", pwd);
        edit.commit();
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
        
    
    
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值