Example usage for android.content Context SENSOR_SERVICE

List of usage examples for android.content Context SENSOR_SERVICE

Introduction

In this page you can find the example usage for android.content Context SENSOR_SERVICE.

Prototype

String SENSOR_SERVICE

To view the source code for android.content Context SENSOR_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.hardware.SensorManager for accessing sensors.

Usage

From source file:com.thomasokken.free42.Free42Activity.java

public int shell_get_heading(DoubleHolder mag_heading, DoubleHolder true_heading, DoubleHolder acc_heading,
        DoubleHolder x, DoubleHolder y, DoubleHolder z) {
    if (!heading_inited) {
        heading_inited = true;/*w ww.  j  a  va  2 s.c  om*/
        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor s1 = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        Sensor s2 = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        if (s1 == null)
            return 0;
        SensorEventListener listener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // Don't care
            }

            public void onSensorChanged(SensorEvent event) {
                // TODO: Verify this on a real phone, and
                // check if the orientation matches the iPhone.
                // There doesn't seem to be an API to obtain true
                // heading, so I should set true_heading to 0
                // and heading_acc to -1; the current code just
                // exists to let me investigate the components
                // returned by Orientation events.
                if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    heading_mag = event.values[0];
                    heading_true = event.values[1];
                    heading_acc = event.values[2];
                } else {
                    heading_x = event.values[0];
                    heading_y = event.values[1];
                    heading_z = event.values[2];
                }
            }
        };
        boolean success = sm.registerListener(listener, s1, SensorManager.SENSOR_DELAY_UI);
        if (!success)
            return 0;
        sm.registerListener(listener, s2, SensorManager.SENSOR_DELAY_UI);
        heading_exists = true;
    }

    if (heading_exists) {
        mag_heading.value = heading_mag;
        true_heading.value = heading_true;
        acc_heading.value = heading_acc;
        x.value = heading_x;
        y.value = heading_y;
        z.value = heading_z;
        return 1;
    } else {
        return 0;
    }
}