Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 accelerometer sensor when the activity starts and stops.

    • processSensorEvent() retrieves and dispatches sensor events.

    • The callback callback_input() static method is bound to the Looper.

    Also, define the following members...