O'Reilly logo

Android NDK Beginner's Guide 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 – invoking Java code from a native thread

Let's make a few changes on the Java side:

  1. Create a StoreListener interface as follows to define methods through which native code is going to communicate with Java code:
    public interface StoreListener {
        public void onAlert(int pValue);
    
        public void onAlert(String pValue);
    
        public void onAlert(Color pValue);
    }
  2. Open Store.java and make a few changes:
    • Declare one Handler member. A Handler is a message queue associated with the thread it was created on (here, it will be the UI thread). Any message posted from whatever thread is received in an internal queue processed magically on the initial thread. Handlers are a popular and easy inter-thread communication technique on Android.
    • Declare a delegate ...

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