我是新来的传感器Android和现在有点糊涂了使用。我需要执行一些操作只是,如果有一个显著光线的变化,例如:光线是在一个黑暗的房间打开。我有一个pretty的简单的默认实现为止。我怎么能告诉系统我只想反应一个显著光线的变化?
公共类MainActivity扩展活动实现SensorEventListener {
私人的SensorManager mSensorManager;
私人传感器mLight;
私人RelativeLayout的mLayout;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
mLayout =(RelativeLayout的)findViewById(R.id.mLayout);
mLayout.setKeepScreenOn(真正的);
}
@覆盖
保护无效onResume(){
mSensorManager.registerListener(这一点,mLight,
SensorManager.SENSOR_DELAY_FASTEST);
super.onResume();
}
@覆盖
保护无效的onPause(){
mSensorManager.unregisterListener(本);
super.onPause();
}
@覆盖
公共无效onAccuracyChanged(传感器传感器,诠释精度){
如果(sensor.getType()== Sensor.TYPE_LIGHT){
// 去做
}
}
@覆盖
公共无效onSensorChanged(SensorEvent事件){
如果(event.sensor.getType()== Sensor.TYPE_LIGHT){
// 去做
}
}
}
解决方案
试试下面的code: -
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.app.Activity;
进口android.widget.TextView;
公共类MainActivity延伸活动{
TextView的textLIGHT_available,textLIGHT_reading;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
textLIGHT_available
=(TextView中)findViewById(R.id.LIGHT_available);
textLIGHT_reading
=(TextView中)findViewById(R.id.LIGHT_reading);
的SensorManager mySensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
传感器LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
如果(LightSensor!= NULL){
textLIGHT_available.setText(Sensor.TYPE_LIGHT可用);
mySensorManager.registerListener(
LightSensorListener,
LightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}其他{
textLIGHT_available.setText(Sensor.TYPE_LIGHT不可用);
}
}
私人最终SensorEventListener LightSensorListener
=新SensorEventListener(){
@覆盖
公共无效onAccuracyChanged(传感器传感器,诠释精度){
// TODO自动生成方法存根
}
@覆盖
公共无效onSensorChanged(SensorEvent事件){
如果(event.sensor.getType()== Sensor.TYPE_LIGHT){
textLIGHT_reading.setText(LIGHT:+ event.values [0]);
}
}
};
}
I'm new to sensor use in android and now a bit confused. I need to perform some actions only if there's a significant light change, e.g. the light was turned on in a dark room. I have a pretty simple default implementation so far. How can I tell the system I only want to react to a significant light change?
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
private RelativeLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
mLayout = (RelativeLayout) findViewById(R.id.mLayout);
mLayout.setKeepScreenOn(true);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mLight,
SensorManager.SENSOR_DELAY_FASTEST);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_LIGHT) {
// TODO
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
// TODO
}
}
}
解决方案
try the below code :-
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textLIGHT_available, textLIGHT_reading;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLIGHT_available
= (TextView)findViewById(R.id.LIGHT_available);
textLIGHT_reading
= (TextView)findViewById(R.id.LIGHT_reading);
SensorManager mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if(LightSensor != null){
textLIGHT_available.setText("Sensor.TYPE_LIGHT Available");
mySensorManager.registerListener(
LightSensorListener,
LightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}else{
textLIGHT_available.setText("Sensor.TYPE_LIGHT NOT Available");
}
}
private final SensorEventListener LightSensorListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_LIGHT){
textLIGHT_reading.setText("LIGHT: " + event.values[0]);
}
}
};
}