How to know if my SensorManager has a registered Sensor

南笙酒味 提交于 2020-01-12 07:48:25

问题


I'm using a sensor for my Android Application. I register the sensor with a line of code:

mySensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);

I have introduced a line of code to unregister the listener so it wont be running all time:

mySensorManager.unregisterListener(this);

So far it works, but i need to register it again when the application resumes. I need to know if my sensorManager has a registered listener so I can registered again or skit it. Does something like this can be done?:

if (mySensorManager.getRegisteredListenerList == null){ registerListener() } else { .. }

回答1:


As far as I know, there is no native way to check if you have already registered listener. I would not worry too much about this check since this check is already done by Android, so if you have already registered listener, Android is not gonna add the same listener twice:

@SuppressWarnings("deprecation")
private boolean registerLegacyListener(int legacyType, int type,
        SensorListener listener, int sensors, int rate)
    {
        if (listener == null) {
            return false;
        }
        boolean result = false;
        // Are we activating this legacy sensor?
        if ((sensors & legacyType) != 0) {
            // if so, find a suitable Sensor
            Sensor sensor = getDefaultSensor(type);
            if (sensor != null) {
                // If we don't already have one, create a LegacyListener
                // to wrap this listener and process the events as
                // they are expected by legacy apps.
                LegacyListener legacyListener = null;
                synchronized (mLegacyListenersMap) {
                    legacyListener = mLegacyListenersMap.get(listener);
                    if (legacyListener == null) {
                        // we didn't find a LegacyListener for this client,
                        // create one, and put it in our list.
                        legacyListener = new LegacyListener(listener);
                        mLegacyListenersMap.put(listener, legacyListener);
                    }
                }
                // register this legacy sensor with this legacy listener
                legacyListener.registerSensor(legacyType);
                // and finally, register the legacy listener with the new apis
                result = registerListener(legacyListener, sensor, rate);
            }
        }
        return result;
    }

So you can call registerListener as many times as you want it will only be added once:)

The same is applicable for unregister logic




回答2:


@addition to Pavel: SensorEventListeners can be registered multiple times. Seen here and in Java Programming for Android Developers. I did register 9 Sensors with Listener delay of SENSOR_DELAY_GAME and run() with THREAD_PRIORITY_BACKGROUND. It works fine. To unregister I did the following in private class readoutRunnable implements Runnable:

@Override
    public void run() {
        try { 
              //process events from locally stored SensorEvent[]
              // array is filled from onSensorChanged()
        } catch (Exception e) {
            // catch e
        } finally {
            sensorManager.unregisterListener(listener);                
        }
    }

This puts lots of events in the onsensorchanged() which is why you should take googles advice to not block the method all the more serious:

Sensor data can change at a high rate, which means the system may call the onSensorChanged(SensorEvent) method quite often. As a best practice, you should do as little as possible within the onSensorChanged(SensorEvent) method so you don't block it. If your application requires you to do any data filtering or reduction of sensor data, you should perform that work outside of the onSensorChanged(SensorEvent) method.



来源:https://stackoverflow.com/questions/14269925/how-to-know-if-my-sensormanager-has-a-registered-sensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!