zoukankan      html  css  js  c++  java
  • android_lesson_2

    android单元测试

    1.首先在AndroidManifest.xml中加入下面红色代码

      <uses-library android:name="android.test.runner" /> <!-- append to application noe-->

      <!-- append to manifest;  com.example.myfirstapp == manifest.package -->
      <instrumentation android:name="android.test.InstrumentationTestRunner"  android:targetPackage="com.example.myfirstapp" />

    2.l编写单元测试代码(选择要测试的方法,右键点击“Run As”--“Android Junit Test” )
      public class InputActivityTest extends AndroidTestCase{
           public void test() throws Exception {
              InputService inputService=new InputService();
              Integer r=inputService.queryPort();
              Assert.assertEquals(new Integer(1), r);
          }
      }

    note:
              无法导出文件:"Failed to pull selection",查看文件名是否存在中文、权限是否为rw

    <!--申请操作sd卡读和删权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 申请操作sd卡写的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    文件操作:
     
    package com.example.myfirstapp;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import android.content.Context;
    import android.os.Environment;
    
    public class FileOperatorService {
        private Context context;
    
        public void SetContext(Context context) {
            this.context = context;
        }
    
        public void write(String fileName, String content) throws Exception {
            FileOutputStream out = context.openFileOutput(fileName,context.MODE_PRIVATE);
            out.write(content.getBytes());
            out.close();
        }
    
        public String read(String fileName) throws Exception {
            FileInputStream reader = context.openFileInput(fileName);
            ByteArrayOutputStream memoryArr = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = reader.read(buf)) != -1) {
                memoryArr.write(buf, 0, len);
            }
            byte[] data = memoryArr.toByteArray();
            memoryArr.close();
            reader.close();
            return new String(data);
        }
        
        public void write2sdCard(String fileName, String content) throws Exception {
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                File sdCardDir=Environment.getExternalStorageDirectory();
                File file=new File(sdCardDir,fileName);
                FileOutputStream out=new FileOutputStream(file);
                out.write(content.getBytes());
                out.close();
            }
        }
    
        public String readFromSDCard(String fileName) throws Exception {
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                File sdCardDir=Environment.getExternalStorageDirectory();
                File file=new File(sdCardDir,fileName);
                FileInputStream reader = new FileInputStream(file);
                ByteArrayOutputStream memoryArr = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int len;
                while ((len = reader.read(buf)) != -1) {
                    memoryArr.write(buf, 0, len);
                }
                byte[] data = memoryArr.toByteArray();
                memoryArr.close();
                reader.close();
                return new String(data);
            }
            return null;
        }
    
    }
  • 相关阅读:
    罗马数字转整数
    对称的二叉树
    python中列表,元组,字符串 互相转换
    python django 批量上传文件并绑定对应文件的描述
    python django mkdir和makedirs的用法
    python 获取两位的月份(09)和天数(09)
    django 注册后台管理 在debug=true能行,在debug=false不能显示出管理标签
    django OperationalError: unable to open database file 创建数据库
    网站安全保证设置及网站认证
    动态的有序分类导航:每个一级标题下都有多个二级标题。
  • 原文地址:https://www.cnblogs.com/BigIdiot/p/2670133.html
Copyright © 2011-2022 走看看