Calling binary executable program (native executable) in Android

Source: Internet
Author: User

A few days ago there is a need to call the binary program in the Java code, on the Internet to find some information, write something to record.

Android is also a Linux-based system, and of course it can run binary executables. Only Android restricts the direct way to install run APK files only. Although the NDK can use the C binary code in a dynamic-link library, it is inconvenient. At least we can call some basic Linux commands, such as LS,RM.

First method: Runtime.exec (string[] args)


This approach is provided by the Java language itself and can be used in Android. Args is an array of arguments to execute. The approximate usage is as follows:

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 ();
}

The above code executes the Linux standard command ls-l. The standard output after executing this command is in Brout. If there is an error, such as a parameter error, command error, etc. information will be placed in the Brerr.

Read it from the inside if you need it.

Second method: Class.forName ("Android.os.Exec")

The code is probably like this:

Try {

  //Android.os.Exec is not a 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 the 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";}  } C Atch (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 (...)
...
This method uses the Android.os.Exec class provided by Android. An AP similar to terminal has been provided in the source code of Android,

In/development/apps/term/src/com/android/term, this AP is the basic command that uses Android.os.Exec to invoke Linux. However, this class can only be used in Android's built-in APS. If you write a non-built-in AP Separately, you cannot call android.os.Exec directly. The indirect method is similar to the above, with Class.forName ("Android.os.Exec") to get this class, Execclass.getmethod ("createsubprocess", ...) To get the method inside the class so that you can use a class that is not meant to be used. This is reflection? ...

This method seems to be troublesome, and more crooked, I think the Java itself to provide something better, after all, simple, transplant good point.

As for the binary execution of their own how to put into the apk inside how to call, the original post is also very clear, but to remind that the assets folder for a single file size limit of 1MB. So if there is a resource file larger than 1MB I think I had to cut myself first, and then put it together when I was running ...

Calling binary executable program (native executable) in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.