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 extendsAndroidTestCase{
   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;    }}

更多相关文章

  1. Cocos2d-x权限问题
  2. Android应用程序安装过程源代码分析(4)
  3. Android平台mass storage相关代码
  4. 使用代码为textview设置drawableLeft
  5. [置顶] Android 2.3.5源代码 更新至android 6.0,可以下载,度娘网盘
  6. Android拨打电话权限总结
  7. cocos2dx 调用java层代码
  8. 在Android中使用Handler和Thread线程执行后台操作
  9. Android下获取Root权限和设置目录属性

随机推荐

  1. 图片压缩保存读取操作
  2. ProgressBar播放动画
  3. Android(Java):focus
  4. Android 多媒体
  5. android打电话,接电话,挂电话过程
  6. 关于"match_parent"这个xml的布局设定值
  7. Android JNI介绍
  8. 2011.11.22——— android jni简单用法
  9. android - Content-Type大全
  10. php android json