繁体   English   中英

光传感器问题

[英]Light sensor issue

下面提到的代码在三星银河s(GB)上可以完美工作,但我在SE neo v(ics)和tipo(ics)上没有工作。 它是sony或ics的问题。当启动应用程序并立即显示吐司“ 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));
   }
  }

    };
}

在gsmarena(http://www.gsmarena.com/sony_ericsson_xperia_neo_v-4122.php)和Sony Xperia Tipo(http://www.gsmarena.com/sony_xperia_tipo-4718.php)上为SE Neo V列出的唯一传感器是加速度计,接近度和指南针。

光传感器绝不是必需的传感器,因为它通常仅用于调整屏幕亮度。 从Android 4.0兼容性文档中:

7.3.7。 光度计设备的实现可能包括光度计(即环境光传感器)。

您的应用将不得不处理没有光传感器的情况,或者您应将android.hardware.sensor.light<uses-feature>添加到AndroidManifest.xml中。

暂无
暂无

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

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