Blog Address: www.zalezone.cn
Foreword Source of Demand
These days to help others do a simple androidclient, there is no function. The main is to call the C code to the mobile phone WiFi network settings, so it also leads to technical difficulties. How to call the C program to achieve the effect we need.
How to Solve
For this. I've come up with two ways of solving this:
-
The first scenario is to use JNI for local invocation. about what is JNI. JNI is in fact the abbreviation for Java Native Interface . This is the Java local interface, which provides several APIs that enable the communication of 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, because the Android NDK,NDK is also a collection of tools, providing a dynamic library to help developers develop C or C + + at a high speed, and to be able to package the so and Java applications together as an apk in a very convenient way.
Technology to implement operational files
First you need to get a running file. Of course you want to run the file is not as simple as imagined, not in Linux directly to the gcc can be, here need to cross-compile C code to get to run on the Android machine executable files, Detailed how to cross-compile C files, here will not repeat. You can search the Internet for a bit.
In addition, the NDK is a good tool.
Resource Storage
The Resource store page here is a small pit. Usually when we write a Java program, we assume that you want to open a file and enter the path directly. For example, assume that the file you want to use is in the project folder, directly enter the file name can be called, but the execution environment here is embedded device, not a PC, which involves a problem, how the resources are stored.
Let's talk about the similarities and differences between the Asset directory and the Res/raw directory in android:
- Same point
- Files in both folders will be stored intact in the APK package after packaging, and will not be compiled into binary.
- Different points
- The files in the Res/raw are mapped to R.java. Use the resource ID directly when you visit. 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); |
Next is the method I used to read the assets . The same is done by getting the input stream in a way that writes
123 |
null; am = Getassets (); InputStream is = Am.open ("filename"); |
Note: It is said that assert can only put a single file not exceeding 1M files. But is not really detailed has not been verified. Suppose you have a problem, you should consider this point of attention.
Although the read is successful, but to run with the Shell script, should be on the phone's storage should have this file, 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. The way to get the data in assets is also 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." We need to run the resulting executable file. Because Android is Linux -based. So some of the main commands are still supported, and in Android you have to run the shell command in the following format, for example:
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