O'Reilly logo

Android NDK Beginner's Guide - Second Edition by Sylvain Ratabouil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – allocating an object with JNI

Let's define a background Watcher that will use an object shared between Java and C/C++ as a lock:

  1. In Store.java, add two new methods to start and stop a watcher thread. These methods respectively return and take a long as parameter. This value helps us hold a native pointer on the Java side:
    public class Store implements StoreListener {
        ...
        public native long startWatcher();
        public native void stopWatcher(long pPointer);
    }
  2. Create a new file, StoreThreadSafe.java. The StoreThreadSafe class inherits from the Store class and aims at making the Store instances thread-safe using synchronized Java blocks. Declare a static member field LOCK of type Object and define a default constructor:
    package com.packtpub.store; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required