1. JNI
(1) Java call native C
For the basic concepts of JNI, refer to the following documents:
Http://blog.csdn.net/believefym/archive/2007/06/08/1644635.aspx
Note that the javah command processes the. Class file instead of the. Java file. You must specify the package path and package name. Javap commands have similar requirements.
The example is very simple. You can obtain it from the following webpage. The C ++ Code does not have a configuration project. compile it into a DLL.
Http://www.namipan.com/d/21e857fd04fc086ce0b3ba90ba92f197e1b626ca55df0000
(2) native C call Java
For basic methods, refer to the following documents:
Http://icepeer.itpub.net/post/3982/19158
Note:
When using the constructor, The jdk1.1 statement is as follows:
Jmethodid mid = (* env)-> getmethodid (ENV, CLS, "", "(ljava/lang/string;) V ");
Jdk1.2 is written as follows:
Jmethodid mid = (* env)-> getmethodid (ENV, CLS, "<init>", "(ljava/lang/string;) V ");
The functions of the example are similar to those of the preceding example, which can be obtained from the following webpage.
Http://www.namipan.com/d/688b2aca53531d0aeb236015f95fd36b594005392d520000
(3) Java call native C & native C callback Java
Because Android provides Java interfaces, the most common situation here is
1) start the program in Java.
2) Java calls native C.
3) native C calls back Java.
The above programming experience is not too difficult. The example is as follows:
Http://www.namipan.com/d/54782eb9fc9480904e4152adff6e1df28f648976f83f0000
The difference between this example and (2) is: (2) because the program is started in C, you need to start JVM and instantiate relevant Java objects before calling Java, in Example (3), because the program is started in Java, the JVM has been started when C calls back Java, and the related Java object instance already exists, it is much easier to do so.
In the. h file generated by javah, before the parameters declared by the function in Java, there are two parameters jnienv and jobject. Env is an interface for calling Java methods. It has many functions. Jobject refers to the reference of the Java object instance that calls native C. The jobject pointer cannot be shared among multiple threads. that is to say, you cannot directly store the jobject pointer in a thread to a global variable and use it in another thread. fortunately, you can use
Gs_object = env-> newglobalref (OBJ );
To save the passed OBJ to gs_object, so that other threads can use this gs_object to manipulate that Java object.
2. Compile the C program on Android
(1) The Left Bank has made great achievements in this regard. The following is his blog:
Http://blog.sina.com.cn/flza
For the simplest Hello World Program, refer to the practices in the following article.
Android native C Development: Environment Building
Http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html
Here there is a concept to be clear, the previous example is for the x86-win32 platform, and here we want to program in arm-Linux. I have a colleague here who once put the DLL under the x86-win32 into the android simulator and complained that JNI could not be used, which is a typical example of such errors.
Another thing to note is that the cross-compilation tool used on the left bank, although compiled programs can be run in the simulator, it cannot be directly used for JNI and requires certain skills, see the following webpage:
Shared Library "Hello world! "For Android
Http://honeypod.blogspot.com/2007/12/shared-library-hello-world-for-android.html
From this article, we can see that the default format of the dynamic library used by the cross-compilation tool on the Left Bank is different from that of the dynamic library on the Android platform, this is why some people say Android cannot use JNI.
A better way is to directly use the cross-compilation tool provided by Google.
(2) obtain the android source code
The official method for obtaining source code is as follows:
Http://source.android.com/source/download.html
Although the source code in the http://source.android.com, but with the browser is unable to obtain the code, can only use the method mentioned above.
Of course, websites such as www.androidin.com also provide unofficial source code downloads.
The source code size is about 1 GB, extract the source code, in the source code prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin folder can find the cross-compilation tool provided by Google.
For how to use the prebuilt tool to compile C Programs, refer to the following articles:
Http://www.j2medev.com/android/native/200901/20090119222445.html
3. Android JNI
For Orthodox practices, refer to the following webpage:
Http://www.j2medev.com/android/native/200901/20090119222617.html
Of course, there are some simple practices. You can extract only the bionic, Dalvik, and prebuilt folders in the source code.
It should be noted that, unlike the third-party cross-compilation tool, prebuilt only has the relevant header file and does not have the corresponding one. O or. A file, so if you use a general makefile, there is no way to compile static executable files, but dynamic library files can be compiled.
There is another strange point here. If CC is used directly to generate. So, an error will occur. CC is used to generate. O and LD is used to generate. So. I don't know why, but I still need expert advice.
Here we also provide an example with the same functions as Example 1. (3. Put .apk and. So under/data/app.
Http://www.namipan.com/d/cb2c2eb8c1c6852eaf744e382b8d9805b44e5a2e7f660000
It should be pointed out that the common JNI call uses system. loadlibrary to load the dynamic library, while the Android platform uses system. Load to load the dynamic library. This is why many people apply the PC syntax but fail on Android.
4. A syntax Phenomenon
Alertdialog dialog = new alertdialog. Builder (smartguider. This)
. Settitle ("setting error ")
. Setmessage ("GPS function is not enabled. Please enable GPS before using this program! /N Click OK to go to the system settings page, and click Cancel to exit the program. ")
. Create ();
(1xx characters are omitted here)
In C language, there are similar situations, such
Char * strcpy (char * strdestination, const char * strsource );
Why not write
Void strcpy (char * strdestination, const char * strsource );
Or
Char * strcpy (const char * strsource );
What about it?
The first method is not used because it can be easily written, such as strlen (strcpy.
The second method is not used. Of course, it is related to memory allocation. I will not talk about it here.
5. Exploration of activity Lifecycle
The diagram on the activity lifecycle in the android SDK is undoubtedly authoritative, but there is no change in the activity lifecycle caused by the function keys of the Android mobile phone.
Here, I will reload the relevant functions of the activity derived class and output the LOG method to discuss this issue.
First, you can use the Android. util. Log class to output logs. eclipse can view the output logs in the debug status. There are many ways to output logs. They are not independent of each other, but a relationship of information content inclusion. from the simplest to the most detailed, they are error, warn, info, debug, verbose, that is, the logs in error must be visible in verbose. There are also a lot of logs generated during the android runtime. You can customize the filter to filter irrelevant logs.
Here, only oncreate, ondestroy, onpause, onrestart, onresume, onstart, and onstop are overloaded and logs are output.
1) start the program
Oncreate-> onstart-> onresume
2) restart the program (press the Home button on the left and the dial button on the left to disable the current activity)
Onrestart-> onstart-> onresume
3) press the Home key on the left and the dial button on the left.
Onpause-> onstop
4) press the return key on the right.
Onpause-> onstop-> ondestroy
5) The Hanging button on the right side
Onpause
6. Add a new view
There is only one view in the code generated by the wizard, and the corresponding UI layout file is in RES/layout/main. after searching for XML, I did not find how to quickly add a view. So I had to manually add the relevant XML file. Fortunately, eclipse can customize the template of the added XML file. copy the XML file and make it a template. It will be convenient to add it later.
7. Compile froyo (partial posting)
Bytes --------------------------------------------------------------------------------------------------------------------------
To put aside a few questions, this blog originally intended to completely write some of its original content. It is advantageous for posting only to links, but linking it. It often takes a few months for the original link to be difficult. So I had to paste some options.
Bytes --------------------------------------------------------------------------------------------------------------------------
Froyo has been out for a while. On the Rise of the moment, I started git code from the official website and planned to compile it. According to the error information, froyo and later versions require 64-bit OS for compilation. So we had to reinstall 64-bit ubuntu.
Follow the steps on the official site step by step, and then stuck on the apt-Get install sun-java5-jdk. The error message tells me that this package cannot be found. Google, find the following solution:
9.10/10.04 add Ubuntu 9.04 line to you/etc/APT/sources. List
Deb http://us.archive.ubuntu.com/ubuntu/ jaunty multiverse
Deb http://us.archive.ubuntu.com/ubuntu/ jaunty-Updates multiverse
# Sudo apt-Get update
# Sudo apt-Get install sun-java5-jdk
(Note that the installation will remain on Sun's consent form, and does not respond after pressing OK (the confirmation is text or not a button), and then press the tab on the keyboard .)
The method for changing the default JDK is as follows: similarly, to change the default javac, the method is
# Update-alternatives -- config Java
# Update-alternatives -- config javac
Display the following information and enter the Java-1.5.0-sun number:
There are two options to replace item Java (/usr/bin/Java is provided ).
Select the path priority status
------------------------------------------------------------
* 0/usr/lib/JVM/java-6-openjdk/JRE/bin/Java 1061 automatic mode
1/usr/lib/JVM/Java-1.5.0-sun/JRE/bin/Java 53 manual mode
2/usr/lib/JVM/java-6-openjdk/JRE/bin/Java 1061 manual mode
Selection path selection first checks status
------------------------------------------------------------
0/usr/lib/JVM/java-6-openjdk/bin/javac 1061 Auto Mode
1/usr/bin/ECJ 143 manual mode
2/usr/bin/gcj-wrapper-4.4 1044 manual mode
* 3/usr/lib/JVM/Java-1.5.0-sun/bin/javac 53 manual mode
4/usr/lib/JVM/java-6-openjdk/bin/javac 1061 manual mode
View the current Java version:
# Java-version
Java version "1.5.0 _ 22"
Java (TM) 2 Runtime Environment, Standard Edition (build 1.5.0 _ 22-b03)
Java hotspot (TM) Client VM (build 1.5.0 _ 22-b03, mixed mode, sharing)
The most depressing thing is that, after installing the sun-java5-jdk, make, the error message tells me that froyo has switched to Java 1.6.
Make everything goes smoothly, and then find the emulator in out/host/linux-x86/bin, but it cannot be executed, prompting that AVD device is missing, use export android_product_out = ~ /Android/out/target/product/generic.