Blog Address: www.zalezone.cn
Foreword Source of Demand
These days to help others do a simple androidclient, there is no function, the main thing is to call C code to the phone's WiFi network settings. So it also leads to the technical difficulties, how to call the C program to achieve the effect we need.
How to Solve
For this, I came up with two ways to solve this problem:
-
The first scenario is the use of
Fr=aladdin "target=" _blank "rel=" external ">JNI for local invocation. As for what is JNI, JNI is in fact the abbreviation for Java Native Interface , the Java local interface, which provides several APIs that enable communication between Java and other languages (mainly C and C + +).
-
Another way is to compile the C code you want to run into a running file , and then package the executable file with the program as an APK to invoke the executable file when it is needed.
Finally choose
In the end, I chose another option, on the grounds that the other approach was relatively simple in terms of what I already had to run the files. Operability is strong.
And the first option, since the Android Ndk,ndk is also a collection of tools, provides a dynamic library that helps developers develop C or C + + at a high speed. And be able to voluntarily package so and Java applications together as APK. Very convenient.
Technology to implement operational files
First you need to get a running file. Of course, it's not as simple as it might seem to be to run files. Not in Linux directly to gcc , where the C code needs to be
Fr=aladdin "target=" _blank "rel=" external "> cross-compilation to get a running file that can run on Android. Details how to cross-compile C files. Here will not repeat, we can search the Internet. In addition, the NDK is a good tool.
Resource Storage
The Resource storage page here is a small pit, ordinary we write Java program, the assumption is to open a file directly into the path, for example, assume that the file to be used in the project folder, directly input the file name can be called, but the execution environment here is embedded device, not a PC, This involves a problem. How resources are stored.
Let's talk about the similarities and differences between the Asset directory and the Res/raw directory in android:
- Same point
- The files in both folders will be stored intact in the APK package after packaging. Will not be compiled into binary.
- Different points
- The files in the Res/raw will be mapped to the R.java, and the resource ID will be used directly when visiting. Files under the Assets folder are not mapped to R.java.
- Res/raw cannot have a folder structure , and a folder can be created under the Assets folder.
Resource acquisition
Here, incidentally, the Read method of the file resource under Res/raw , gets the input stream to write by the following way
1 |
InputStream is =getresources (). Openrawresource (R.id.filename); |
The next thing I'm going to use is to read the assets method, and the same way I write it by getting the input stream.
123 |
null; am = Getassets (); InputStream is = Am.open ("filename"); |
Note: It is said that the assert can only put a single file not more than 1M files, but is not really detailed has not been verified, assuming that the problem should be considered this point of attention.
Although the read was successful. But to run it with a shell script. Should have this file on the phone's storage, just read the words in the phone is not found, so we need a file to save the operation.
Here I wrote a function to save the file, in which the methods to get the data in assets are combined.
123456789101112131415 |
Public void copydatatosd (String outfilename) throws ioexception{inputstream myinputstream;outputstream myoutputstream = new FileOutputStream (outfilename); myinputstream = this . Getassets (). Open ( "a.out" ); byte [] buffer = new byte [1024 ];< span class= "keyword" >int length = myinputstream.read (buffer); while (Length > 0 ) {myoutputstream.write (buffer, 0 , length); length = myinputstream.read (buffer);}
Myoutputstream.flush (); Myinputstream.close (); Myoutputstream.close ();} |
And then I define the incoming outfilename that is defined by the file path plus the file name
12 |
Private Static "Data/data/com.example.g3wifi/a.out"; Private Static File Exe_file; |
Shell command Run
In this case, "Everything is ready, only the East wind", we need to run the resulting executable files, because Android is Linux -based, so some of the main commands are still supported. To run a shell command in Android, you can do this in the following format:
12345678 |
Public void ExeC (String cmd) throws Ioexception{runtime Runtime =runtime.getruntime (); Process process = runtime.exec (cmd); //process Process = runtime.exec (new string[]{"su", "reboot"});//ability to run two commands //This can get the result after running the shell command New BufferedReader (new InputStreamReader (Process.geterrorstream ())); |
Android Compile call C code