在 Android 中调用二进制可执行程序(native executable )
来源:互联网 发布:瞻博网络中国有限公司 编辑:程序博客网 时间:2024/04/29 19:13
在 Android 中调用二进制可执行程序(native executable )
前几天有需要在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 lineInputStream outs = process.getInputStream();InputStreamReader isrout = new InputStreamReader(outs);BufferedReader brout = new BufferedReader(isrout);
String errline = null;
String result = "";
// get the whole error message stringwhile ( (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")
这个方法是一个老外找到的,原贴在这里:http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App
代码大概是这样:
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 )
- 在 Android 中调用二进制可执行程序(native executable )
- 在 Android 中调用二进制可执行程序(native executable )
- 在 Android 中调用二进制可执行程序(native executable )
- 在 Android 中调用二进制可执行程序(native executable )
- 在 Android 中调用二进制可执行程序(native executable)
- 在 Android 中调用二进制可执行程序(native executable )-- 在代码中执行shell命令
- 在程序中如何调用可执行程序。
- 在程序中如何调用可执行程序。
- 在DELPHI中调用一个可执行程序得方法
- popen在程序内调用可执行程序
- Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API)
- VC中程序调用可执行程序
- 在 Android Native 程序中输出 LOG
- 怎样在一个C 程序中调用另一个程序(独立可执行的程序, 或系统命令)?
- Android 调用本机可执行程序
- 在java中调用c程序--native方法的学习
- 在 Visual C++/Native 代码中调用.NET 程序集
- docker build 命令的问题纠结三天了
- 短信发送器
- 制造业如何基于BPM做供应链管理?
- ios 中tableview和scrollView的区别
- tomcat报错org.springframework.web.context.ContextLoaderListener找不到,解决办法
- 在 Android 中调用二进制可执行程序(native executable )
- unity ngui学习(四)
- 几种任务调度的 Java 实现方法与比较
- SQL Server数据库被置疑的解决方案
- 企业争相全力以赴推进IT变革
- Android开发多线程断点续传下载器
- 微信js api支付
- 想要成为电脑高手必须掌握的八个cmd 命令
- 南阳题目39-水仙花数