简体   繁体   中英

Light sensor issue

The code mentioned below works perfectly on samasung galaxy s(GB) but i didnt worked on SE neo v(ics) and tipo(ics). it is a sony issue or a ics issue.when started app starts and immediately display the toast "No Light Sensor! quit-".

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class LightsenseActivity extends Activity {

 ProgressBar lightMeter;
 TextView textMax, textReading;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lightMeter = (ProgressBar)findViewById(R.id.lightmeter);
        textMax = (TextView)findViewById(R.id.max);
        textReading = (TextView)findViewById(R.id.reading);

        SensorManager sensorManager 
        = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        Sensor lightSensor 
        = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        if (lightSensor == null){
        Toast.makeText(LightsenseActivity.this, 

           "No Light Sensor! quit-", 
           Toast.LENGTH_SHORT).show();
        }else{
       float max =  lightSensor.getMaximumRange();

         lightMeter.setMax((int)max);
         textMax.setText("Max Reading: " + String.valueOf(max));

         sensorManager.registerListener(lightSensorEventListener, 
           lightSensor, 
           SensorManager.SENSOR_DELAY_NORMAL);

        }
    }

    SensorEventListener lightSensorEventListener
    = new SensorEventListener(){

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onSensorChanged(SensorEvent event) {
   // TODO Auto-generated method stub
   //if(event.sensor.getType()==Sensor.TYPE_LIGHT){

    float currentReading = event.values[0];
    { lightMeter.setProgress((int)currentReading);
    textReading.setText("Current Reading: " + String.valueOf(currentReading));
   }
  }

    };
}

The only sensors listed for the SE Neo V on gsmarena (http://www.gsmarena.com/sony_ericsson_xperia_neo_v-4122.php) and the Sony Xperia Tipo (http://www.gsmarena.com/sony_xperia_tipo-4718.php) are accelerometer, proximity, and compass.

A light sensor is by no means a required sensor since all it is generally used for is to adjust screen brightness. From the Android 4.0 compatibility document:

7.3.7. Photometer Device implementations MAY include a photometer (ie ambient light sensor.)

Your app is going to have to handle the case where there is no light sensor or you should add a <uses-feature> for android.hardware.sensor.light to your AndroidManifest.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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