Nachding
Nachding

Reputation: 465

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 ? 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 !

Upvotes: 0

Views: 1542

Answers (2)

jradich1234
jradich1234

Reputation: 1425

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" />

Upvotes: 0

Shervin
Shervin

Reputation: 409

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

Upvotes: 2

Related Questions