Avoid writing boilerplate when using sensors.

Code Quality Rank: L3
Programming language: Java
License: Apache License 2.0
Tags: Sensor     Android     Animations     Annotations     Sensors    
Latest version: v0.1.0

SensorAnnotations alternatives and similar packages

Based on the "Sensors" category.
Alternatively, view SensorAnnotations alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of SensorAnnotations or a related project?

Add another 'Sensors' Package



Annotate methods to use as listeners for sensor events.

Build Status Download Android Arsenal Awesome Android #22

public class MyActivity extends Activity {
     * Perform actions as accelerometer data changes...
    void accelerometerSensorChanged(@NonNull SensorEvent event) {

     * If the sensor isn't available, update UI accordingly...
    void testTemperatureSensorNotAvailable() {

    @Override protected void onResume() {

    @Override protected void onPause() {
        SensorAnnotations.unbind(this); // Unbind to save the user's battery life.

There are four possible annotations: @OnSensorChanged, @OnAccuracyChanged, @OnSensorNotAvailable, and @OnTrigger. The annotated methods must have the method signatures specified in the Sensors Overview Android docs.

void method(@NonNull SensorEvent event) {}

// or the following syntax can be used which accepts a delay value:
@OnSensorChanged(value = Sensor.TYPE_LIGHT, delay = SensorManager.SENSOR_DELAY_NORMAL)
void method(@NonNull SensorEvent event) {}

void method(@NonNull Sensor sensor, int accuracy) {}

void method() {}

void method(@NonNull TriggerEvent event) {}

For information about sensor delays and accuracy events see the "Monitoring Sensor Events" portion of the Android docs.

Calling SensorAnnotations.bind should be done when you want to start receiving sensor events. Because this consumes battery life you need to call unbind when you are finished. The bind method needs to take a Context object. There are two variations:

// Use this alternative to bind to a different target. See the example application.
SensorAnnotations.bind(this, context);

The @OnTrigger annotation is a specific annotation for sensors of TYPE_SIGNIFICANT_MOTION (introduced in 4.3). This type has a different method and parameter than the others. For more info see the Android docs on Using the Significant Motion Sensor.

View the Demo app for usage

SensorAnnotations Sample App

How does it work?

A binding class is created for each class that has annotations. In the example app, the classes MainActivity and AccelerometerManager will have two classes generated at compile time: MainActivity$$SensorBinder and AccelerometerManager$$SensorBinder. Because these classes are generated at compile time no reflection is needed.

These classes register the listener with the sensor system service. If the sensor isn't available on the device and a method has been annotated with @OnSensorNotAvailable it will be invoked. If an accuracy event occurs and a method has been annotated with @OnAccuracyChanged it will be invoked. The TYPE_SIGNIFICANT_MOTION sensor doesn't have an accuracy callback.

Use in your project

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.dvoiss:sensorannotations:0.1.0'
    apt 'com.dvoiss:sensorannotations-compiler:0.1.0'

Using Android Gradle Plugin version 2.2.0+:

dependencies {
    compile 'com.dvoiss:sensorannotations:0.1.0'
    annotationProcessor 'com.dvoiss:sensorannotations-compiler:0.1.0'