我正在尝试为我的LG P990开发一个光传感器应用来检测激光(不要问为什么)。
守则如下:
package soma.de.Light;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class LightSensorActivity extends Activity {
SensorManager mySensorManager;
Sensor myLightSensor;
TextView textLightSensorData;
TextView textLightSensor;
Button Start;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textLightSensor = (TextView)findViewById(R.id.tvLight);
textLightSensorData = (TextView)findViewById(R.id.tvAcc);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
myLightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Start = (Button)findViewById(R.id.btStart);
Start.setOnClickListener(btListener);
if (myLightSensor == null){
textLightSensor.setText("No Light Sensor!"); //THIS IS WHAT I GET!
}else{
textLightSensor.setText(myLightSensor.getName() + "\n " + myLightSensor.getResolution() );
mySensorManager.registerListener(lightSensorEventListener,
myLightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
private OnClickListener btListener = new OnClickListener()
{
public void onClick(View v){
myLightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (myLightSensor == null){
textLightSensor.setText("No Light Sensor!");
}else{
textLightSensor.setText(myLightSensor.getName() + "\n " + myLightSensor.getResolution() );
mySensorManager.registerListener(lightSensorEventListener,
myLightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
};
SensorEventListener lightSensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent arg0) {
// TODO Auto-generated method stub
if(arg0.sensor.getType()==Sensor.TYPE_LIGHT){
textLightSensorData.setText("Light Sensor Date:"
+ String.valueOf(arg0.values[0]));
}else if(arg0.sensor.getType()==Sensor.TYPE_PROXIMITY){
textLightSensorData.setText("Test "
+ String.valueOf(arg0.values[0]) +" " + String.valueOf(arg0.values[1]));
}
}};
}
但我只在活动中获得“无光传感器”回报。 LG P990确实有一个。在“隐藏菜单”中,我可以在ALC /接近测试中看到光传感器的值。
我是否必须更改代码中的任何内容才能让它运行?
答案 0 :(得分:1)
不确定getDefaultSensor()
,但其文档说明:
Note that the returned sensor could be a composite sensor,
and its data could be averaged or filtered. If you need to access
the raw sensors, use getSensorList()
我正是这样做的:
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// Check if there is at least one light sensor
if (sm.getSensorList(Sensor.TYPE_LIGHT).size() == 0) {
// No light sensor installed
}
else {
Sensor light = sm.getSensorList(Sensor.TYPE_LIGHT).get(0);
if (!sm.registerListener(this, light, SensorManager.SENSOR_DELAY_GAME)) {
// Couldn't register light sensor listener
}
}
然后在onSensorChanged()
方法中,event.values[0]
为您提供光传感器的值,即0..100范围内的整数,即使我只看到10和100: - )