前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下。

Android 也是基于linux 的系统,当然也可以运行二进制的可执行文件。只不过Android 限制了直接的方式只能安装运行apk文件。虽然有NDK可以用动态链接库的方式来用C的二进制代码,但毕竟不方便。至少我们可以调用linux的一些基本命令,如ls,rm等。

第一种方法:Runtime.exec(String[] args)

这种方法是java语言本身来提供的,在Android里面也可以使用。args是要执行的参数数组。大概用法如下:

String[] args = new String[2];

args[0] = "ls";

args[1] = "-l";

try
 {
  Process process = Runtime.getRuntime().exec(arg);

//get the err line

InputStream stderr = process.getErrorStream();
  InputStreamReader isrerr = new InputStreamReader(stderr);
  BufferedReader brerr = new BufferedReader(isrerr);

//get the output line  InputStream outs = process.getInputStream();
  InputStreamReader isrout = new InputStreamReader(outs);
  BufferedReader brout = new BufferedReader(isrout);

String errline = null;

String result = "";

// get the whole error message string  while ( (line = brerr.readLine()) != null)
  {
   result += line;
   result += "/n";

}

if( result != "" )

{

// put the result string on the screen

}

// get the whole standard output string

while ( (line = brout.readLine()) != null)
  {
   result += line;
   result += "/n";
  }
  if( result != "" )
  {

// put the result string on the screen

}

}catch(Throwable t)
 {
  t.printStackTrace();
 }

以上代码执行了linux的标准命令 ls -l。执行此命令后的标准输出是在brout中。如果出错,像参数错误,命令错误等信息就会放在brerr中。

有需要的话从里面读出来便可。

第二种方法:Class.forName("android.os.Exec")

代码大概是这样:

try {

// android.os.Exec is not included in android.jar so we need to use reflection.  Class<?> execClass = Class.forName("android.os.Exec");  Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);  Method waitFor = execClass.getMethod("waitFor", int.class);  // Executes the command.  // NOTE: createSubprocess() is asynchronous.  int[] pid = new int[1];  FileDescriptor fd = (FileDescriptor)createSubprocess.invoke( null, "/system/bin/ls", "/sdcard", null, pid);  // Reads stdout.  // NOTE: You can write to stdin of the command using new FileOutputStream(fd).  FileInputStream in = new FileInputStream(fd);  BufferedReader reader = new BufferedReader(new InputStreamReader(in));  String output = "";  try {  String line;  while ((line = reader.readLine()) != null) {  output += line + "/n"; }  } catch (IOException e) {  // It seems IOException is thrown when it reaches EOF.  }  // Waits for the command to finish.  waitFor.invoke(null, pid[0]);  return output; } catch( ... )
...
 这种方法是用了 Android 提供的android.os.Exec类。在 Android 的源代码中已经提供了类似 terminal 的ap,在

/development/apps/Term/src/com/android/term 中,这个ap就是用android.os.Exec来调用linux的基本命令的。不过这个类只有在Android的内置ap中才可以使用。单独写一个非内置ap的话是无法直接调用android.os.Exec的。间接的方法就是类似上面,用Class.forName("android.os.Exec")来得到这个类,execClass.getMethod("createSubprocess", ... )来得到类里面的方法,这样就可以使用本来不能用的类了。这就是反射?...

这个方法看起来要麻烦一些,而且比较歪,我觉得还是用java本身提供的东西比较好,毕竟简单,移植性也好点。

至于自己写的二进制执行程序如何放到apk里面如何调用,原帖也说的很清楚,不过要提醒一下,assets文件夹对单个文件大小有限制为1MB. 所以如果有资源文件大于1MB我看只好自己先切割一下了,运行的时候再自己拼起来...

在 Android 中调用二进制可执行程序(native executable )的更多相关文章

  1. 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码

    原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...

  2. Android中调用C++函数的一个简单Demo

    这里我不想多解释什么,对于什么JNI和NDK的相关内容大家自己去百度或谷歌.我对Android的学习也只是个新手.废话少说直接进入正题. 一.在Eclipse中创建一个Android Applicat ...

  3. [转][android][利用JNI技术在Android中调用、调试C++代码]

    在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...

  4. 在Android中调用WebService

    某些情况下我们可能需要与Mysql或者Oracle数据库进行数据交互,有些朋友的第一反应就是直接在Android中加载驱动然后进行数据的增删改查.我个人不推荐这种做法,一是手机毕竟不是电脑,操作大量数 ...

  5. 在Android中调用C#写的WebService(附源代码)

    由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...

  6. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  7. Android 中调用本地命令

    Android 中调用本地命令 通常来说,在 Android 中调用本地的命令的话,一般有以下 3 种情况: 调用下也就得了,不管输出的信息,比如:echo Hello World.通常来说,这种命令 ...

  8. Android中调用系统所装的软件打开文件(转)

    Android中调用系统所装的软件打开文件(转) 在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下! 这个是打开文件的一个方法: /** ...

  9. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

随机推荐

  1. Hive基础学习

    Hive 学习记录Hive介绍:Hive 是起源于Facebook,使得Hadoop进行SQL查询成为可能,进而使得非程序员也可以进进行对其使用:它是一种数据仓库工具,将结构化的数据文件 映射为一张数 ...

  2. JavaScript高级程序设计:第五章

    引用类型 一.object类型: 创建object实例的方式有两种.第一种是使用new操作符后跟Object构造函数,如下所示: var  person = new  Object(): person ...

  3. List------Linked 链表

    1.Definition Linked list consists of a series of nodes. Each nodes contains the element and a pointe ...

  4. SQL Server 存储过程进行分页查询

    CREATE PROCEDURE prcPageResult -- 获得某一页的数据 -- @currPage INT = 1 , --当前页页码 (即Top currPage) @showColum ...

  5. Shell:进程的层级关系

    [luwenwei@appdev115 ~]$ ps -ef | grep initroot 1 0 0 Apr24 ? 00:08:25 init [3] [luwenwei@appdev115 ~ ...

  6. 【我与一道水题的抗争之路】 哈理工2323 Emirp(反素数)

    题目: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2323 1,打表的姿势不对. ...

  7. Xcode6无法用xib得问题解决方法

    1.创建一个新工程,选择singleView application 2.将storyboard和launchscreen删除,选择moveToTrash 3.删除info.plist文件中Main ...

  8. regress

    #! /bin/ksh ############### ###   UAT   ### ############### export ENVS=/test/change/env/env_test.sq ...

  9. html屏蔽右键、禁止复制与禁止查看源代码

    <script> function doNothing(){ window.event.returnValue=false; return false; } </script> ...

  10. Hidden Word

    Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...