超级强大的 Android JNI:理解 "JNI(jintArray ..." 格式
Superpowered Android JNI: understanding "JNI(jintArray ..." format
Superpowered“Simple USB Example”使用下面的代码基于C++方法更新Java端的UI。这是我第一次看到 JNI
单独后跟一个方法。我在类似情况下见过 JNIEXPORT
,但不只是 JNI
。我想了解有关此用法的更多信息,但没有任何运气尝试 Google 它。任何解释或参考将不胜感激!
// 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);
}
查看该示例中的代码:
// Beautifying the ugly Java-C++ bridge (JNI) with these macros.
#define PID com_superpowered_simpleusb_SuperpoweredUSBAudio // Java package name and class name. Don't forget to update when you copy this code.
#define MAKE_JNI_FUNCTION(r, n, p) extern "C" JNIEXPORT r JNICALL Java_ ## p ## _ ## n
#define JNI(r, n, p) MAKE_JNI_FUNCTION(r, n, p)
他正在使用一些宏观魔法来制作东西 "cleaner"。实际上,它使它不那么冗长但更难理解,生产代码永远不应该有这样的东西。
Superpowered“Simple USB Example”使用下面的代码基于C++方法更新Java端的UI。这是我第一次看到 JNI
单独后跟一个方法。我在类似情况下见过 JNIEXPORT
,但不只是 JNI
。我想了解有关此用法的更多信息,但没有任何运气尝试 Google 它。任何解释或参考将不胜感激!
// 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);
}
查看该示例中的代码:
// Beautifying the ugly Java-C++ bridge (JNI) with these macros.
#define PID com_superpowered_simpleusb_SuperpoweredUSBAudio // Java package name and class name. Don't forget to update when you copy this code.
#define MAKE_JNI_FUNCTION(r, n, p) extern "C" JNIEXPORT r JNICALL Java_ ## p ## _ ## n
#define JNI(r, n, p) MAKE_JNI_FUNCTION(r, n, p)
他正在使用一些宏观魔法来制作东西 "cleaner"。实际上,它使它不那么冗长但更难理解,生产代码永远不应该有这样的东西。