How to print on Android screen from native code in NDK?

I want to print the output from statements printfin my native code (in C) on an Android screen. Since I have a lot of output that I want to see on the Android screen, I want something more than the return statements at the end of the JNI functions that print one text for each function. How to do it?

EDIT:

For example, in my C code, if I want to print something in the middle of a function like " Hello World", what should I do? Right now, I can type " only from return!" on the Android screen using methods setText.

jstring Java_com_example_encryptString( JNIEnv* env, jobject thiz) 
{
   printf("Hello World");
   return (*env)->NewStringUTF(env, "only from return!");
}

I know a method when I call this method from a Java class and use TextViewsit to print on an Android screen. But this can only accept and print the value returned by the function, and nothing more. Can't I print any other value that is not returned by the function?

Thanks.

Note . I am not looking for android logs in logcat.

+4
source share
1 answer

If I understood your question correctly, you wanted to set the text in the TextView from c code, right?

If so, you can do it. You need to pass TextViewas a parameter to your own method. Then, in your native code call, find its method setTextand call it.

SO .

- :

jstring Java_com_example_encryptString( JNIEnv* env, jobject thiz, jobject jtextViewObject, ...)
{
    //getting set text method
    jclass clazz = (*env)->FindClass(env, "android/widget/TextView");
    jmethodID setText = (*env)->GetMethodID(env, clazz, "setText", "(Ljava/lang/CharSequence;)V");

    ... do stuff ...

    //set text to text view
    jstring jstr = (*env)->NewStringUTF(env, "This comes from jni.");
    (*env)->CallVoidMethod(env, jtextViewObject, setText, jstr);

    ... do stuff ...

    return (*env)->NewStringUTF(env, "only from return!");
}

java-, TextView params.

+2

Source: https://habr.com/ru/post/1615025/


All Articles