Sensor Manager getOrientation, doesnt display text and only works in debug mode?

Posted by Aidan on Stack Overflow See other posts from Stack Overflow or by Aidan
Published on 2010-06-07T19:03:50Z Indexed on 2010/06/07 19:22 UTC
Read the original article Hit count: 680

Filed under:
|
|

Hi Guys,

My code appears to crash. I'm trying to setup a SensorManager for getting the Orientation of the device. I also have a listener that should update when conditions change. But when I run this code it crashes...

        public void sensor(){
        // Locate the SensorManager using Activity.getSystemService
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        sm.getOrientation(mR, mOrientation);
        // Register your SensorListener
        sm.registerListener(sl, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL);
    }

   private final SensorEventListener sl = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {


    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType()==Sensor.TYPE_ORIENTATION) {
            Global.Orientation = "Orientation is equal to: "+ SensorManager.getOrientation(mR, mOrientation);
        }
    }

sm is defined as SensorManager sm; as a class wide variable also. I've got some other classes outputting the Orientation to a screen which I know works. The problem is somewhere in these methods. Am I doing it wrong or Is there a better way of doing this?

© Stack Overflow or respective owner

Related posts about java

Related posts about android