Android 传感器 - 获取接近传感器值的问题以及使用不同手机的问题

发布于 2024-10-18 23:34:47 字数 1186 浏览 7 评论 0原文

过去做过一些 Android 开发,但已经有一段时间了 - 想构建一个使用方向和接近传感器的应用程序。所以...

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ALL) , SensorManager.SENSOR_DELAY_GAME);

所以设置监听器 - 监听所有传感器 - 想知道我应该只听两个吗? (这会节省电池吗?)

Sensor mySensor = event.sensor;
    // if (mySensor.getType() != Sensor.TYPE_ORIENTATION) return;
    if (mySensor.getType() == Sensor.TYPE_ORIENTATION)
    {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        tv.setText("Rotation around X, Azimuth = " + x);
        tv1.setText("Rotation around Y, Pitch = " + y);
        tv2.setText("Rotation around Z, Roll = " + z);
    }
    if (mySensor.getType() == SensorManager.SENSOR_PROXIMITY)
    {

        float p = event.values[0];
        tv3.setText("Proximity = " + p);

    }

然后在 onSensorChanged 中 - 请参阅上面

我的 LG Optimus One 上的内容,我可以让它显示方向传感器的 TextView 中的值(这款手机没有接近传感器),

但在我的 Nexus One 出现了可爱的空白屏幕!?

抱歉问了这么长的问题!但任何帮助都会很好,如果您需要更多信息,请询问, LG 手机是 2.2 Nexus One 是 2.2.2 谢谢,

大卫

Have done some Android Dev in the past but it has been a while - want to build an app that uses Orientation and Proximity Sensors. so...

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ALL) , SensorManager.SENSOR_DELAY_GAME);

So set up listener - listened to all sensors - wonder should I just listen to two? (would this save battery?)

Sensor mySensor = event.sensor;
    // if (mySensor.getType() != Sensor.TYPE_ORIENTATION) return;
    if (mySensor.getType() == Sensor.TYPE_ORIENTATION)
    {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        tv.setText("Rotation around X, Azimuth = " + x);
        tv1.setText("Rotation around Y, Pitch = " + y);
        tv2.setText("Rotation around Z, Roll = " + z);
    }
    if (mySensor.getType() == SensorManager.SENSOR_PROXIMITY)
    {

        float p = event.values[0];
        tv3.setText("Proximity = " + p);

    }

Then in onSensorChanged - see above

on my LG Optimus One I can get it to display the values in the TextViews of the Orientation sensor (this phone doesn't have a proximity sensor)

But running the same code on my Nexus One leads to a lovely blank screen!?

Sorry for the long question! but any help would be good and if you need more infor just ask,
LG phone is 2.2 Nexus One is 2.2.2
Thanks,

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-10-25 23:34:47

您可以对所有传感器使用相同的侦听器。
但如果你真的想监听多个甚至所有传感器,你需要重写这一行:

mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ALL) , SensorManager.SENSOR_DELAY_GAME);

因为 getDefaultSensor(int type) 的实际实现只返回一个传感器,如源代码所示:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    return l.isEmpty() ? null : l.get(0);
}

public List<Sensor> getSensorList(int type) {
    // cache the returned lists the first time
    List<Sensor> list;
    final ArrayList<Sensor> fullList = sFullSensorsList;
    synchronized(fullList) {
        list = sSensorListByType.get(type);
        if (list == null) {
           if (type == Sensor.TYPE_ALL) {
               list = fullList;
           } else {
                list = new ArrayList<Sensor>();
                for (Sensor i : fullList) {
                     if (i.getType() == type)
                        list.add(i);
                }
           }
           list = Collections.unmodifiableList(list);
           sSensorListByType.append(type, list);
       }

    return list;
}

因此,在 Sensor.TYPE_ALL 的情况下,方法 getSensorList(int type) 提供所有可用传感器的(无序)列表和 getDefaultSensor (int type) 仅返回其中的第一个。

由于包含所有传感器的列表是无序的,因此 getDefaultSensor(Sensor.TYPE_ALL) 提供随机传感器。我尝试了一下,首先得到了加速度计,然后又得到了光传感器。由于您只处理侦听器中的方向和接近度变化,因此我怀疑 getDefaultSensor(Sensor.TYPE_ALL) 在您的 Nexus 上提供了不同的传感器。

您应该收到 mSensorManager.getSensorList(Sensor.TYPE_ALL) 可用的所有传感器的列表,并迭代该列表,以便分别注册每个包含的传感器。

关于您关于节省电池的问题 - 我不知道传感器是否永久运行。即使是这种情况,也可以通过减少不必要的处理来节省电池电量。如果您不需要特定传感器提供的信息,请不要注册以接收来自该传感器的消息。这减少了发送到您的应用程序的消息。

You can use the same listener for all sensors.
But if you really want to listen to multiple or even all sensors, you need to rewrite this line:

mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ALL) , SensorManager.SENSOR_DELAY_GAME);

Because the actual implementation of getDefaultSensor(int type) returns only one sensor, as seen in the source code:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    return l.isEmpty() ? null : l.get(0);
}

public List<Sensor> getSensorList(int type) {
    // cache the returned lists the first time
    List<Sensor> list;
    final ArrayList<Sensor> fullList = sFullSensorsList;
    synchronized(fullList) {
        list = sSensorListByType.get(type);
        if (list == null) {
           if (type == Sensor.TYPE_ALL) {
               list = fullList;
           } else {
                list = new ArrayList<Sensor>();
                for (Sensor i : fullList) {
                     if (i.getType() == type)
                        list.add(i);
                }
           }
           list = Collections.unmodifiableList(list);
           sSensorListByType.append(type, list);
       }

    return list;
}

So in case of Sensor.TYPE_ALL the method getSensorList(int type) provides an (unordered) list of all sensors available and getDefaultSensor(int type) returns only the first one of it.

Because the list containing all sensors is unordered, getDefaultSensor(Sensor.TYPE_ALL) supplies a random sensor. I tried it and first got the accelerometer and next time the light sensor. Since you handle only orientation and proximity changes in your listener, I would suspect that getDefaultSensor(Sensor.TYPE_ALL) supplied a different sensor on your Nexus.

You should receive a list of all sensors available with mSensorManager.getSensorList(Sensor.TYPE_ALL) and iterate over the list, in order to register every contained sensor respectively.

Regarding your question about saving battery - I don't know, if the sensors are running permanently. Even if this is the case, one can always save battery power by reducing unnecessary processing. If you do not need the information a specific sensor provides, do not register to receive messages from it. That reduces messages being sent to your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文