Time for action – handling accelerometer events

Let's handle accelerometer events in DroidBlaster:

  1. Open jni/InputHandler.hpp and add a new method onAccelerometerEvent(). Include the android/sensor.h official header for sensors:
    #ifndef _PACKT_INPUTHANDLER_HPP_
    #define _PACKT_INPUTHANDLER_HPP_
    
    #include <android/input.h>
    #include <android/sensor.h>
    
    class InputHandler {
    public:
        virtual ~InputHandler() {};
    
        virtual bool onTouchEvent(AInputEvent* pEvent) = 0;
        virtual bool onKeyboardEvent(AInputEvent* pEvent) = 0;
        virtual bool onTrackballEvent(AInputEvent* pEvent) = 0;
        virtual bool onAccelerometerEvent(ASensorEvent* pEvent) = 0;
    };
    #endif
  2. Create new methods in jni/EventLoop.hpp:
    • activateAccelerometer() and deactivateAccelerometer() to enable/disable the ...

Get Android NDK Beginner's Guide - 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.