如何知道我的 Android 设备上是否存在传感器?

发布于 2024-12-29 14:57:50 字数 274 浏览 2 评论 0原文

我想知道我的 Android 设备上是否存在传感器(例如加速度计)。

我正在处理 SensorManager 类。 这是我正在使用的代码:

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),1);

谢谢。

i would like to know if a sensor (for exemple the accellerometer) is present on my Android device.

I am dealing with the SensorManager class.
Here is the code I am using:

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),1);

Thank you.

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

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

发布评论

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

评论(5

暗喜 2025-01-05 14:57:50

看看这里:

http://developer.android.com/reference /android/content/pm/PackageManager.html

如果您认为需要这样做:

PackageManager manager = getPackageManager();
boolean hasAccelerometer = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER);

take a look in here:

http://developer.android.com/reference/android/content/pm/PackageManager.html

if think you need to do that:

PackageManager manager = getPackageManager();
boolean hasAccelerometer = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER);
关于从前 2025-01-05 14:57:50

以下是来自developer.android.com的推荐:http://developer.android.com/ Guide/topics/sensors/sensors_overview.html#sensors-identify

您可以使用 getDefaultSensor() 方法并传入特定传感器的类型常量来确定设备上是否存在特定类型的传感器。如果设备具有多个给定类型的传感器,则必须将其中一个传感器指定为默认传感器。如果给定类型的传感器不存在默认传感器,则该方法调用将返回 null,这意味着设备没有该类型的传感器。

private SensorManager mSensorManager;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
  // Success! There's a magnetometer.
}
else {
  // Failure! No magnetometer.
}

Here is recomendation from developer.android.com: http://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-identify

You can determine whether a specific type of sensor exists on a device by using the getDefaultSensor() method and passing in the type constant for a specific sensor. If a device has more than one sensor of a given type, one of the sensors must be designated as the default sensor. If a default sensor does not exist for a given type of sensor, the method call returns null, which means the device does not have that type of sensor.

private SensorManager mSensorManager;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
  // Success! There's a magnetometer.
}
else {
  // Failure! No magnetometer.
}
2025-01-05 14:57:50

您的第二行可用于此目的:

boolean accelerometer;

accelerometer = sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

if(accelerometer) 
{
.
.
}

Your second line can be used for this:

boolean accelerometer;

accelerometer = sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

if(accelerometer) 
{
.
.
}
原野 2025-01-05 14:57:50

由于我收到 API9 required 错误,因此我使用:

    SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    for (int i = 0; i< deviceSensors.size(); i++) {
        if (deviceSensors.get(i).getType() == Sensor.TYPE_PRESSURE) {
            mHasBarometer = true;
            break;
        }
    }

Since I get a API9 required error, I use instead:

    SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    for (int i = 0; i< deviceSensors.size(); i++) {
        if (deviceSensors.get(i).getType() == Sensor.TYPE_PRESSURE) {
            mHasBarometer = true;
            break;
        }
    }
昨迟人 2025-01-05 14:57:50

我使用以下代码:

        SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
       if( sensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).size() > 0)
       {
           //sensor exist
       }
       else
       {
           //disable feature
       }

I use the following code:

        SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
       if( sensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).size() > 0)
       {
           //sensor exist
       }
       else
       {
           //disable feature
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文