简体   繁体   English

在Android中的新进程中运行命令行工具

[英]Run command line tool in new process in Android

We have a unix command line tool that we need to run from an Android app. 我们有一个unix命令行工具,我们需要从Android应用程序运行。 There isn't a library that exists that achieves the same functionality we are looking for. 没有一个库可以实现我们正在寻找的相同功能。

What is the proper way to bundle and and execute this tool within an Android app? 在Android应用中捆绑和执行此工具的正确方法是什么? It is a single file we need to execute as a new process. 它是我们需要作为新进程执行的单个文件。 We want to avoid any private APIs, etc. 我们希望避免使用任何私有API等。

NOTE: We are fine with using services or extracting to a certain directory if that is what is optimal. 注意:如果这是最佳的,我们可以使用服务或提取到某个目录。 We are using Mono for Android (Xamarin.Android), but we are fine with Java examples. 我们正在使用Mono for Android(Xamarin.Android),但我们对Java示例很好。

Hope this helps ... 希望这可以帮助 ...

try{
    Process process;            
    process = Runtime.getRuntime().exec("top -n 1 -d 1");
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

} catch (InterruptedException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Cheers! 干杯!

In order to run anything on a new process , you simply need to define it in the manifest. 为了在新进程上运行任何内容,您只需在清单中定义它。 In your case , you might want to set it for a service: 在您的情况下,您可能希望将其设置为服务:

http://developer.android.com/guide/topics/manifest/service-element.html

(see the process attribute) . (参见process属性)。

However , I think you don't need such a thing , and you can use a new thread instead ,which will probably run on your service , and might need the service to be foreground service, depending on your needs. 但是,我认为您不需要这样的东西,并且您可以使用新的线程,这可能会在您的服务上运行,并且可能需要该服务作为前台服务,具体取决于您的需求。

In order to run the file you want (and i've never done it on android) , you will probably need to copy the file somewhere (preferably the internal storage used for your app) , and then reach it and run it just like in the terminal of unix/linux , using Runtime.getRuntime().exec(... 为了运行你想要的文件(我从来没有在android上完成它),你可能需要将文件复制到某个地方(最好是用于你的应用程序的内部存储),然后到达并运行它就像在unix / linux的终端,使用Runtime.getRuntime()。exec(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM