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 ...

Get Android NDK Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.