On the HelloWorld of ANDROIDNDK programming, today, I learned how to print out the log in the C code, as in Java code, in the development of the NDK.
1. We need to introduce the header file:
#include <android/log.h>
2. Define the print tag, which is equivalent to the static final type in Java
#define LOG_TAG "OnEvent"
3. Define a macro, which is equivalent to declaring a method:
#define LOGD (FMT, args ...) __android_log_print (Android_log_info, Log_tag, FMT, # #args)
4. Use in C code:
LOGD ("I am the string that needs to be printed");
With the above four steps, we can print the log as in Java code, and the tag we declare here is "onEvent", so we can also use the ADB logcat | Grep-n "OnEvent", such a command to filter the query.
The following how to solve the problem of Chinese garbled:
If I return such a string in C code: return (*ENV)->newstringutf (env, "Hello World I am Chinese"); This is possible to have garbled situation, then how to solve it, in fact, is very simple, just need to be the Android project and corresponding. c file encoding method is also set to UTF-8 format.
One thing to note here is that if it was in the earlier NDK version, we needed to write:
1. First call the declared native method, get the C code summary returned by the string
String TempStr = HELLOWORLDNDK ();
2. Use the following code to implement the conversion of the encoding:
try {
New String (Tempstr.getbytes ("iso-8859-1"), "Utf-8");
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
Step-by-step learning ANDROIDNDK Programming (log Debugging and solving Chinese garbled characters)