Time for action – handling accelerometer events
Let's handle accelerometer events in
DroidBlaster
:
Open
jni/InputHandler.hpp
and add a new methodonAccelerometerEvent()
. Include theandroid/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
Create new methods in
jni/EventLoop.hpp
:activateAccelerometer()
anddeactivateAccelerometer()
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...