超级强大的“简单 USB 示例”使用以下代码基于 C++ 方法更新 Java 端的 UI。这是我第一次看到JNI
一个人跟着一个方法。我见过JNIEXPORT
类似的情况,但不仅仅是JNI
。我想了解更多关于这种用法的信息,但没有任何运气尝试谷歌它。任何解释或参考将不胜感激!
// This is called by the MainActivity Java object periodically.
JNI(jintArray, getLatestMidiMessage, PID)(JNIEnv *env, jobject __unused obj) {
jintArray ints = env->NewIntArray(4);
jint *i = env->GetIntArrayElements(ints, NULL);
pthread_mutex_lock(&mutex);
i[0] = latestMidiCommand;
i[1] = latestMidiChannel;
i[2] = latestMidiNumber;
i[3] = latestMidiValue;
pthread_mutex_unlock(&mutex);
env->ReleaseIntArrayElements(ints, i, NULL);
return ints;
}
// Update UI every 40 ms.
Runnable runnable = new Runnable() {
@Override
public void run() {
int[] midi = getLatestMidiMessage();
switch (midi[0]) {
case 8: textView.setText(String.format(Locale.ENGLISH, "Note Off, CH %d, %d, %d", midi[1] + 1, midi[2], midi[3])); break;
case 9: textView.setText(String.format(Locale.ENGLISH, "Note On, CH %d, %d, %d", midi[1] + 1, midi[2], midi[3])); break;
case 11: textView.setText(String.format(Locale.ENGLISH, "Control Change, CH %d, %d, %d", midi[1] + 1, midi[2], midi[3])); break;
}
handler.postDelayed(this, 40);
}
};
handler = new Handler();
handler.postDelayed(runnable, 40);
}