我正在使用NDK创建数学运算的演示,如加法,减法,乘法和除法.
我能够创建库并从本机代码获取响应但结果不正确我的意思是它是随机静态值.
Calculator.c类
#include <stdio.h> #include <jni.h> jint Java_com_example_jni_calculator_Calculator_add(JNIEnv* env,jint a,jint b) { return (jint)(a + b); } jint Java_com_example_jni_calculator_Calculator_substract(JNIEnv* env,jint b) { return (jint)(a - b); } jint Java_com_example_jni_calculator_Calculator_multiply(JNIEnv* env,jint b) { return (jint)(a * b); } jint Java_com_example_jni_calculator_Calculator_devide(JNIEnv* env,jint b) { return (jint)(a / b); }
Calculator.java类用于加载库和启动本机方法.
public class Calculator { static { System.loadLibrary("Calculator"); } public native int add(int a,int b); public native int substract(int a,int b); public native int multiply(int a,int b); public native int devide(int a,int b); }
int num1 = Integer.parseInt(txtNumber1.getText().toString().trim()); int num2 = Integer.parseInt(txtNumber2.getText().toString().trim()); tvResult.setText(String.format("%1$d + %2$d is equals to %3$d",num1,num2,mCalculator.add(num1,num2)));
产量