简体   繁体   English

在Android中从C ++调用getUnicodeChar方法

[英]Calling getUnicodeChar method from C++ in Android

I need unicode value of the key I pressed on Android's soft keyboard on C++ code but it looks like NativeActivity does not expose the 'getUnicodeChar' method. 我需要在C ++代码上在Android软键盘上按下的键的unicode值,但看起来NativeActivity不公开'getUnicodeChar'方法。 So I tried to call it with JNI but GetMethodID for that function returns '0'. 所以我试着用JNI调用它,但该函数的GetMethodID返回'0'。 This is my first time with the JNI so I might be doing something very wrong. 这是我第一次使用JNI,所以我可能做错了。 I guess I have to give my 'AInputEvent' to Java somehow but I'm not sure how. 我想我必须以某种方式将我的'AInputEvent'提供给Java,但我不确定如何。 This is the code I wrote: 这是我写的代码:

JavaVM* java_vm = and_app->activity->vm; 
JNIEnv* jni_env = and_app->activity->env; 

JavaVMAttachArgs attach_args; 
attach_args.version = JNI_VERSION_1_6; 
attach_args.name = "NativeThread"; 
attach_args.group = NULL; 

jint result = java_vm->AttachCurrentThread(&jni_env, &attach_args); 
if(result == JNI_ERR)
{ 
    return 0;
}

jclass class_key_event = jni_env->FindClass("android.view.KeyEvent");
jmethodID method_get_unicode_char = jni_env->GetMethodID(class_key_event, "getUnicodeChar", "()I");
int return_key = jni_env->CallIntMethod(class_key_event, method_get_unicode_char);
java_vm->DetachCurrentThread();

return return_key;

The syntax for FindClass is quite special, and it requires what the JNI documentation calls a "a fully-qualified class name", which means a package name with the dots replaced by slashes. FindClass的语法非常特殊,它需要JNI文档称之为“完全限定的类名”,这意味着包名由斜杠替换。

In your case, using: 在您的情况下,使用:

jclass class_key_event = jni_env->FindClass("android/view/KeyEvent");

should make your code work, or at least push it in the right direction! 应该使你的代码工作,或至少推动它正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM