简体   繁体   English

如何获得光传感器的价值

[英]How to get light sensor value

I have following code uses light sensor but not able to display value of event.value[0]...it displays toast only with message "On SensorChanged" also text is not set to tv where i goes wrong.. 我下面的代码使用光传感器,但无法显示event.value [0]的值...它仅显示消息“ On SensorChanged”的吐司,并且文本未设置为我出错的电视。

here is code 这是代码

    public void onSensorChanged(SensorEvent event) 
    {
    if( event.sensor.getType() == Sensor.TYPE_LIGHT)
    {
        tv.setText("value: " + event.values[0] + " lux" );   
        Toast.makeText(getApplicationContext(),"On SensorChanged"+ event.values[0],Toast.LENGTH_SHORT).show();
        Log.d("Sensor Changed", "onSensor Change :" + brightness);
    }

}

Galaxy S does in fact have light-sensor hardware. 实际上,Galaxy S 确实具有光传感器硬件。
The proximity sensing is done by the light sensor device. 接近感测由光传感器装置完成。 More on this here . 更多关于这里

To ensure that you are activating and using the light sensor properly, 为确保您正确激活并使用光传感器,
verify that getSensorList(TYPE_LIGHT) returns a valid light sensor. 验证getSensorList(TYPE_LIGHT)返回有效的光传感器。

Thereafter proceed as shown in this link . 此后,如此链接所示继续。

I dont think that is contains light sensors to that why it is returning nothing,your code seems fine otherwise, 我不认为它包含光传感器,为什么它什么也不返回,否则您的代码看起来还不错,

http://www.gsmarena.com/samsung-phones-9.php http://www.gsmarena.com/samsung-phones-9.php

Follow this link to check the model you are testing on and see in features and then sensors 单击此链接检查您要测试的模型,并在特征和传感器中查看

Now i get sensor value. 现在我得到传感器值。 But the problem is how to assign that to System.SCREEN_BRIGHTNESS. 但是问题是如何将其分配给System.SCREEN_BRIGHTNESS。 i use following code but not able to assign it. 我使用以下代码,但无法分配它。 It only works when automatic is enable in brightness setting. 仅在亮度设置中启用了自动时才起作用。

following is the code 以下是代码

public void onSensorChanged(SensorEvent event) 
{
    if( event.sensor.getType() == Sensor.TYPE_LIGHT)
    {
        float currentLux = event.values[0];
        brightness=(int)event.values[0];
        brightbar.setProgress(brightness);
        tv2=(TextView)findViewById(R.id.textView2);
        tv2.setText("value:"+ currentLux+ " lux" );   
        System.putFloat(cResolver, System.SCREEN_BRIGHTNESS_MODE,currentLux);
        android.view.WindowManager.LayoutParams layoutpars = window.getAttributes();
        layoutpars.screenBrightness = currentLux;  
        window.setAttributes(layoutpars); 
        Log.d("Sensor Changed", "onSensor Change :" + currentLux);
    }

}

Have you registered the listener. 您是否已注册听众。 There are three parts for using the sensors 使用传感器分为三个部分

  1. Create SensorManager and Sensors 创建SensorManager和传感器

     public class MainActivity extends AppCompatActivity implements SensorEventListener { private SensorManager mSensorManager = null; private Sensor mLightSensor = null; ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); } 
  2. Register the sensor you are listening to 注册您正在收听的传感器

     @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mLightSensor, SensorManager.SENSOR_DELAY_NORMAL); } 
  3. Implement the SensorEventListener methods from part 1. 在第1部分中实现SensorEventListener方法。

     @Override public void onSensorChanged(SensorEvent sensorEvent) { if( sensorEvent.sensor.getType() == Sensor.TYPE_LIGHT) { Log.d("SensorTest", ""+ sensorEvent.values[0]); } } @Override public void onAccuracyChanged(Sensor sensor, int i) { } 

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

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