简体   繁体   English

检测设备是否具有Android的光线传感器

[英]Detect if the device has a light sensor Android

I programm for a few years for Android and I wonder something : How to detect if the device has a light sensor for an activity ? 我为Android编程了几年,我想知道一个问题:如何检测设备是否具有用于活动的光传感器? If it has a light sensor : the actvity continues its normal course. 如果具有光传感器:动作将继续其正常过程。 If it has not a light sensor : there is a message and we move on to the next activity. 如果没有光传感器,则会显示一条消息,然后我们继续进行下一个活动。

Thank for your help ! 谢谢您帮忙 !

You probably just need to get the default light sensor and see if it is null. 您可能只需要获取默认的光传感器,看看它是否为空。 If it is not, then the sensor is available. 如果不是,则传感器可用。 The could should look something like: 罐子应该看起来像:

boolean is_Sensor_available; //keep the result in here.
Sensor sensor = null;
SensorManager sensormanager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (sensormanager.getDefaultSensor(Sensor.TYPE_LIGHT) != null){
            // Success! That sensor exists.
            is_sensor_available=true;
            sensor= sensormanager.getDefaultSensor(sensortype);
            Log.d(TAG,"sensor is available");
        }
        else {
            // Failure! sensor not available.
            is_sensor_available=false;
            Log.e(TAG,"sensor does not exist");
        }

This page may also be useful to you: http://developer.android.com/guide/topics/sensors/sensors_environment.html 此页面也可能对您有用: http : //developer.android.com/guide/topics/sensors/sensors_environment.html

Add the following to your Manifest. 将以下内容添加到清单中。 It won't allow the application to install if the hardware isn't present. 如果没有硬件,它将不允许安装应用程序。

<uses-feature android:name="android.hardware.sensor.light" android:required="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM