Calling C functions from Java code

The powerful JNI interface, as referred to before, is able to manage interaction in both directions, from Java to C and from C to Java.

A regular Java class declaring a method with the keyword native declares that the method behavior is implemented in native code. Like a regular Java method, the JNI native method is able to receive Java objects or primitive types as arguments and return primitive types and objects.

Let's see how a native method definition will look like in a Java class:

public class MyNativeActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    cTv.setText(isPrime(2) ? "true" : "false");
  }
  …
  private native boolean isPrime(int number );
}

The preceding activity ...

Get Asynchronous Android Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.