• 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;
        }
    
    }
  • 相关阅读:
    Console.WriteLine输出字符格式化
    GridView动态生成列问题
    日历控件,可运行在XHTML1.0下
    GridView內容導出Excel時異常:必须置於有 runat=server 的表单标记之中
    圆弧分割
    矩阵变换
    使用group by 来统计的小作业
    group by 后使用 rollup 子句总结
    mysql字符串拼接,存储过程,(来自网上看到)
    【深入理解Linux内核】《第二章 内存寻址》笔记 (2014-06-28 12:38)
  • 原文地址:https://www.cnblogs.com/BigIdiot/p/2670133.html
Copyright © 2020-2023  润新知