SensorManager.registerListener wants a SensorListener despite its depreciation

@Override
protected void onResume(){
     //super.onResume();
        sensorManager.registerListener((SensorListener) listener,
                SensorManager.SENSOR_ACCELEROMETER
                |SensorManager.SENSOR_ORIENTATION,
                SensorManager.SENSOR_DELAY_NORMAL);
}

private SensorEventListener listener=new SensorEventListener() {
    public void onSensorChanged(SensorEvent event){
        if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
            System.out.println(event.values[0]);
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy){

    }
};

So the SensorManager.registerListener is where I'm having the issue. Eclipse insists on a SensorListener, and then proceeds to whine when I pass it one because SensorListener is depreciated. I can't seem to get this worked out and would really appreciate some input! I read the previous post and followed the instructions there to no avail. Thank you all very much!

Brad


Asked by: Roland299 | Posted: 24-01-2022






Answer 1

I was having the same problem earlier. For me it was a simple fix. I updated to the latest SDK and ADT and simply swapped out the SensorListener with a SensorEventListener

sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_OREINTATION), SensorManager.SENSOR_DELAY_NORMAL);

Works perfectly for me.

Answered by: Charlie660 | Posted: 25-02-2022



Answer 2

As suggested on this post: android SensorEventListener problem take a look at the code here: Commonsware compass demo

I'm stuck on the same thing and it's helping me out

Answered by: Blake620 | Posted: 25-02-2022



Similar questions

android - why does sensorManager.registerListener require a handler?

(sensorManager is a SensorManager) I'm trying to implement an orientation listener for my program. I declare the listener as a SensorEventListener. The API says I need to pass registerListener a SensorEventListener, Sensor, rate, and handler. What is the handler for? Also, when I mouse over the error, it insists I use the deprecated veresion of the method for a SensorListener instead of a SensorEventListener. And t...


android - "Custom" sensor event rates don't seem to work with SensorManager.registerListener(SensorEventListener listener, Sensor sensor, int rate)

UPDATED: I was able to solve the specific problem I was having by introducing a class-scope static counter and just ignoring ever x number of events. But I'd still like to know what I'm doing wrong re: registering the listener with a hint in microseconds instead of using one of the four given constants. An Activity in my app is engaging the sensors to obtain the orientation of the device, determine th...


java - sensorManager.registerListener in a background service

I have an Android background service written for a Cordova app, which I want to interact with the accelerometer via the sensorManager. I feel I'm pretty close, but just not quite there, I think. For some reason the sensorManager.registerListener doesn't seem to fire the onSensorChanged method - I'm checking the X value every 5 seconds from the MyService (my background service class). From what I hav...


android - Clarifying the use of maxReportLatencyUs in SensorManager.registerListener

From the latest documentation, boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxRe...


android - How should sensorManager.registerListener() be called in onResume()?

I'm testing with an app that samples accelerometer data and process it. First time the app is installed, the onSensorChanged() is called at 1/2 the rate of which is specify in samplePeriodUs I've tried unregistering and register the sensorManager Listener in onPause(). One hack that do seem to work is to enter 1/2 the sample rate in the onResume(). That cannot be the correct way of doing things?


java - Why does sensorManager.registerListener fail to register a listener for Step Counter?

I would like to figure out the cause of not registering a listener for a step counter sensor and how to overcome it. MainActivity.java package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.Sensor; import android.hardware.SensorEvent; import and...


android - SensorManager.registerListener causes app to crash

The app has no problems until I add the very last line, and when it is commented out, the app works again: class MainActivity : AppCompatActivity() { private lateinit var manager: SensorManager private lateinit var accelerometer: Sensor private var sensorEventListener = object :SensorEventListener { override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { //TODO(&qu...


`Xamarin.Android` SensorManager.RegisterListener() returns `false`

It is strange that the sensors SensorType.StepDetector, and SensorType.StepCounter do not work for me. They return false already on SensorManager.RegisterListener() such that I cannot do anything afterwards. This is not the case for SensorType.Accelerometer, which returns true. Also, I am checking the availability of the step sensors by a ...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top