jonathanpeppers
jonathanpeppers

Reputation: 26505

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. 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? It is a single file we need to execute as a new process. We want to avoid any private APIs, etc.

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.

Upvotes: 0

Views: 1265

Answers (2)

android developer
android developer

Reputation: 116050

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) .

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(...

Upvotes: 0

Trinimon
Trinimon

Reputation: 13967

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!

Upvotes: 2

Related Questions