This is my first post so sorry if I make any etiquette mistake... I'm a newbie. I've seen a lot of examples on how to use the accelerometer but at the end I read the documentation and created my own in order to supply the deprecation changes done after OS 1.6. Here is my first implementation of a shaker based on the new API for accelerometer. Basically this is a controller class for the shaking movement. It just consider to the x axis accelerometer changes but can be improved.
Using java Syntax Highlighting
- /**
- * ShakeController: provide a vibration when the user shake the device
- * Copyright (C) 2010 Ivomania
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.ivomania.controller;
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Vibrator;
- /**
- * Controls the shaking movement. Calibration made on Google Nexus One
- */
- public class ShakeController {
- /** Gravity threshold module set to 1,33 G */
- public static final float GRAVITY_MODULE = SensorManager.GRAVITY_EARTH + SensorManager.GRAVITY_EARTH/3;
- //Application context
- Context context;
- Vibrator vibrator;
- SensorManager sm;
- Sensor accelerometer;
- IvomaniaSensorListener accelerometerListener;
- private static ShakeController instance;
- /**
- * Singleton Implementation for this class (private constructor)
- * @param context the application context
- */
- private ShakeController(Context context) {
- this.context = context;
- //init the listener
- accelerometerListener = new IvomaniaSensorListener();
- //init the accelerometer
- initAccelerometer();
- //init the vibrator
- initVibrator();
- }
- /**
- * Singleton Implementation for this class
- * @param context the application context
- * @return ShakeController this class unique instance
- */
- public static ShakeController getInstance(Context context) {
- if (instance == null) {
- instance = new ShakeController(context);
- }
- return instance;
- }
- /**
- * System call to initialize the vibrator service
- * needs "android.permission.VIBRATION" on the AndroidManifest.xml
- */
- private void initVibrator() {
- vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
- }
- /**
- * System call to initialize the accelerometer service
- */
- private void initAccelerometer() {
- sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
- this.accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- }
- /**
- * External call to start sensor the service
- */
- public void startService() {
- sm.registerListener(accelerometerListener, accelerometer, SensorManager.SENSOR_DELAY_GAME);
- }
- /**
- * External call to stop sensor the service
- */
- public void stopService() {
- sm.unregisterListener(accelerometerListener, accelerometer);
- }
- /**
- * The accelerometer listener implementation
- */
- class IvomaniaSensorListener implements SensorEventListener {
- private static final int SCROLL_TO_VIBE = 2;
- //Counter for the left shake
- int rightCounter = 0;
- //Counter for the right shake
- int leftCounter = 0;
- protected IvomaniaSensorListener() {
- }
- /**
- * Use the x value to understand how much the scroll took place
- * After 4 intense scrolls left and right this method produces a vibration
- * @param sensorEvent
- */
- public void onSensorChanged(SensorEvent sensorEvent) {
- float[] coord = sensorEvent.values;
- if (coord[0] > GRAVITY_MODULE) {
- leftCounter++;
- } else if (coord[0] < - GRAVITY_MODULE) {
- rightCounter++;
- }
- if (rightCounter > SCROLL_TO_VIBE || leftCounter > 4) {
- leftCounter = 0;
- rightCounter = 0;
- }
- if (leftCounter == SCROLL_TO_VIBE && rightCounter == SCROLL_TO_VIBE) {
- vibrator.vibrate(500);
- leftCounter = 0;
- rightCounter = 0;
- }
- }
- /**
- * Not implemented
- * @param se
- * @param accuracy
- */
- public void onAccuracyChanged(Sensor se, int accuracy) {
- //Log.error("Accuracy = " + accuracy);
- }
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
This controller can be used in external classes passing a context, for example:
Using java Syntax Highlighting
- import com.ivomania.controller.ShakeController;
- public MyClass extends Activity {
- public void onCreate(Bundle bundle) {
- ...
- ShakeController.getInstance(this).startService();
- }
- public void onDestroy() {
- ShakeController.getInstance(this).stopService();
- super.onDestroy();
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
This way the registration of the listener will be managed by the app lifecyle in order to avoid fast battery drain leaving the sensors up and running. The start and stop methods are the ones addressed to register and unregister the listener whenever the app opens and close (in this case).
Feel free to use the above code as you wish (If you wish
).
Thanks in advance for any comment or suggestion.
Ivomania

