Step-by-step learning ANDROIDNDK Programming (log Debugging and solving Chinese garbled characters)

Source: Internet
Author: User

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)

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.