如何知道我的SensorManager是否已注册Sensor [英] How to know if my SensorManager has a registered Sensor

查看:457
本文介绍了如何知道我的SensorManager是否已注册Sensor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Android应用程序使用传感器.我用一行代码注册传感器:

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);

到目前为止,它仍然有效,但是当应用程序恢复时,我需要再次注册它.我需要知道我的sensorManager是否具有注册的侦听器,以便我可以再次注册或取消注册.这样可以做吗?:

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 { .. }

推荐答案

据我所知,没有原生方法可以检查您是否已经注册了侦听器.我不会为此检查担心,因为该检查已经由Android完成,因此,如果您已经注册了侦听器,则Android不会两次添加相同的侦听器:

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;
    }

因此您可以根据需要多次调用registerListener,而只会被添加一次:)

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

同样适用于注销逻辑

这篇关于如何知道我的SensorManager是否已注册Sensor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆