织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

在 Android 中调用二进制可执行程序(native execut

jackyhwei 发布于 2020-09-16 16:26 点击:次 
前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下。 Android 也是基于linux 的系统,当然也可以运行二进制的可执行文件。只不过Android 限制了直接的方式只能安
TAG: 可执行程序  

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

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

 

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

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

  1. String[] args = new String[2]; 
  2.  
  3.  args[0] = "ls"; 
  4.  
  5.  args[1] = "-l"; 
  6.  
  7.  try 
  8.  { 
  9.   Process process = Runtime.getRuntime().exec(arg); 
  10.  
  11.   
  12.  
  13.   //get the err line 
  14.  
  15.   InputStream stderr = process.getErrorStream(); 
  16.   InputStreamReader isrerr = new InputStreamReader(stderr); 
  17.   BufferedReader brerr = new BufferedReader(isrerr); 
  18.  
  19.   
  20.  
  21.   //get the output line  InputStream outs = process.getInputStream(); 
  22.   InputStreamReader isrout = new InputStreamReader(outs); 
  23.   BufferedReader brout = new BufferedReader(isrout); 
  24.  
  25.   String errline = null
  26.  
  27.   String result = ""
  28.  
  29.    
  30.  
  31.   // get the whole error message string  while ( (line = brerr.readLine()) != null) 
  32.   { 
  33.    result += line; 
  34.    result += "/n"; 
  35.  
  36.  
  37.   }  
  38.  
  39.   if( result != "" ) 
  40.  
  41.   { 
  42.  
  43.    // put the result string on the screen 
  44.  
  45.   } 
  46.  
  47.   
  48.  
  49.   // get the whole standard output string 
  50.  
  51.   while ( (line = brout.readLine()) != null) 
  52.   { 
  53.    result += line; 
  54.    result += "/n"; 
  55.   } 
  56.   if( result != "" ) 
  57.   { 
  58.  
  59.    // put the result string on the screen 
  60.  
  61.   } 
  62.  
  63.  }catch(Throwable t) 
  64.  { 
  65.   t.printStackTrace(); 
  66.  } 

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

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

 

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

 

代码大概是这样:

  1. try { 
  2.  
  3.   // android.os.Exec is not included in android.jar so we need to use reflection. 
  4.   Class<?> execClass = Class.forName("android.os.Exec"); 
  5.   Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class); 
  6.   Method waitFor = execClass.getMethod("waitFor", int.class); 
  7.   // Executes the command. 
  8.   // NOTE: createSubprocess() is asynchronous. 
  9.   int[] pid = new int[1]; 
  10.   FileDescriptor fd = (FileDescriptor)createSubprocess.invoke( null, "/system/bin/ls", "/sdcard", null, pid); 
  11.   // Reads stdout. 
  12.   // NOTE: You can write to stdin of the command using new FileOutputStream(fd). 
  13.   FileInputStream in = new FileInputStream(fd); 
  14.   BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
  15.   String output = ""
  16.   try 
  17.  { 
  18.   String line;  while ((line = reader.readLine()) != null) { 
  19.   output += line + "/n"; 
  20.  } 
  21.   } 
  22.  catch (IOException e) { 
  23.   // It seems IOException is thrown when it reaches EOF. 
  24.   } 
  25.   // Waits for the command to finish. 
  26.   waitFor.invoke(null, pid[0]); 
  27.   return output; 
  28.  } catch( ... ) 
  29. ... 

这种方法是用了 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我看只好自己先切割一下了,运行的时候再自己拼起来...

(qingchen1984)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/202009/17749.html]
本文出处:博客园 作者:qingchen1984 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容