[英]Android Light Sensor to detect significant light change
我正在嘗試使用Sensor.Type_Light或光傳感器來檢測event.values [0]中的更改。
@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]為0,則沒有光,並且隨着光的增加,該值也會增加。 現在我的問題是:如何使用event.values [0]來檢測光線的顯着變化,也就是說,如果我在足夠的光線下激活Sensor(event.values [0]> 15),它應該能夠觸發燈熄滅時發生的事件(event.values [0] <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();
}
}
}
在onSensorChanged()內部,為什么不完全按照自己的意願去做:
if (event.values[0] > 15) {
trigger_some_event();
}
else if (event.values[0] < 3) {
trigger_another_event();
}
您的問題是您只想在發生重大變化時做出反應。
您只需要檢查onChanged方法中的值的解決方案將不會起作用,因為從值“ 3”到值“ 0”的變化不是很大的變化,但是會被檢測到。
您只需要一個變量即可記住上一個燈光事件的值。
在活動范圍內聲明一個變量(不在事件回調中),然后將其命名為“ previousLightValue”
那么在onSensorChanged方法中,您可以將event.values [0]與previousLightValue變量進行比較,並且如果更改超出您確定的某個閾值,則只能“反應”。
您還可以檢查自上次測量以來該值是下降還是上升,告訴您現在的光線是變亮還是變暗。
只需記住,需要先初始化“ previousLightValue”變量,然后才能將其用於比較。 因此,檢測到onSensorChanged事件的第一次發生,並且僅將值存儲在“ previousLightValue”中,而不進行檢查。 從那時起,您一切順利。
=> 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.