我正在尝试使用Sensor.Type_Light或光传感器来检测event.values中的变化。
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()== Sensor.TYPE_LIGHT) {
debugView = (TextView)findViewById(R.id.lightValue);
debugView.setText("Detecting...." +event.values[0]);
}如您所见,这些值显示在一个TextView中。此外,我们知道如果event.values为0,则没有灯光,并且随着灯光的增加,该值也会增加。现在我的问题是:我如何使用这个event.values来检测光线的重大变化,也就是说,如果我在足够的光线(event.values >15)下激活我的传感器,它应该能够在光线关闭(event.values < 3)时触发事件,反之亦然。
提前感谢
我在onSensorChanged中尝试了以下方法,但似乎都不起作用。
if(event.values[0] >= 0 && event.values[0] <5) {
// No Light
if(event.values[0] > 10) {
callForHelp();
}
}
else {
if(event.values[0] > 15) {
// Light On
if(event.values[0] < 5) {
callForHelp();
}
}
}发布于 2015-01-07 17:27:13
在onSensorChanged(),内部,您为什么不完全按照您所说的去做:
if (event.values[0] > 15) {
trigger_some_event();
}
else if (event.values[0] < 3) {
trigger_another_event();
}发布于 2015-04-14 14:52:47
你的问题是,你只想在发生重大变化时做出反应。
简单地检查onChanged方法中的值的解决方案不会像从值"3“更改为值"0”那样工作,这并不是一个重要的更改,但它将被检测到。
您需要的只是一个变量来记住上一个light事件的值。
在活动作用域(不在事件回调中)上声明一个变量,并将其命名为类似于"previousLightValue“的名称
然后,在onSensorChanged方法中,您可以将event.values与previousLightValue变量进行比较,并且只有在更改超过您决定的某个阈值时才会"react“。
您还可以检查该值自上次测量以来是下降还是上升,从而告诉您灯光现在是更亮还是更暗。
请记住,您需要先初始化"previousLightValue“变量,然后才能使用它进行比较。因此,检测第一次出现的onSensorChanged事件,并且只将值存储在"previousLightValue“中,而不进行检查。从那时起,你就可以走了。
发布于 2016-06-25 04:34:35
用于在屏幕上显示图像的=> Xml文件布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgWallpaper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
android:src="@drawable/ic_img" />
</RelativeLayout>=>活动代码
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.ImageView;
public class WallpaperChangeByLight extends Activity {
SensorManager sensorManager = null;
Sensor light = null;
float sensorValue = -1;
ImageView imgView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
imgView = (ImageView) findViewById(R.id.imgWallpaper);
sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
light = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
if (event.values[0] < 3 || event.values[0] > 15) {
sensorValue = event.values[0];
}
if (sensorValue < 3) {
imgView.setImageDrawable(getDrawable(R.drawable.ic_nolight));
} else {
imgView.setImageDrawable(getDrawable(R.drawable.ic_light));
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, light, SensorManager.SENSOR_DELAY_NORMAL);
}
}不要忘记放置可绘制的图像(ic_nolight.png、ic_light.png)
https://stackoverflow.com/questions/27815858
复制相似问题