Sensor Event Listener from non activity class

40
May 18, 2019, at 11:20 PM

I want to get Heading of my device. For this, I have used accelerometer and magnetometer sensors and also I have implemented a class. But when I am calling this class, the class isn't returning any value. I am new in android development. Please help me. Thanks in advance.

I have tried the same code in the main activity and it is working in the main activity.

 public Heading(Context context) {
        this.context = context;
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    }
    public void registerSensor() {
        Sensor sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(mSensorListener, sensorAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        Sensor sensorMagnetic = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(mSensorListener, sensorMagnetic, SensorManager.SENSOR_DELAY_NORMAL);
    }
    public final SensorEventListener mSensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                mGravity = event.values;
            }
            if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                mGeomagnetic = event.values;
            }
            heading = "event";
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };
String getHeading() {
    double azimuth = 0.0;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {
            float[] orientation = new float[3];
            SensorManager.getOrientation(R, orientation);
            azimuth = orientation[0];
        }
        heading = String.valueOf((float)(Math.toDegrees(azimuth)+ 360) % 360);
    }
    return heading;
}

}

I need heading of my device.

READ ALSO
io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found

io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found

Using Springboot starter appEverything working

49
Can't find a way to color the Mandelbrot-set the way i'm aiming for

Can't find a way to color the Mandelbrot-set the way i'm aiming for

I've been successful in coloring the Mandelbrot-set although I can't zoom in very far until it becomes "blurry" and the pattern stopsI fix this by increasing the max_iteration, this works but I get very few colors at *1 magnification and lot's of colors...

48
How can I get Spring's WebFlux to emit a response

How can I get Spring's WebFlux to emit a response

When I create a Spring-webflux webclient I'm not able to get it to go into the subscribe or doOnNext callbackI keep getting this error: java

33