Android Light Sensor
下面这段话都能看懂吧~
Here I am explaining you how to use Light Sensor of Android.
1. Implement SensorEventListener
2. Create variable for SensorManager and Sensor
3. Get Object of SensorManager using system service
4. Get Object of Light Sensor from SensorManager
5. Implement onAccuracyChanged and onSensorChanged method
[java] package com.test;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if(sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "Accuracy :" + accuracy);
}
}
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
}
}
}
package com.test;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if(sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "Accuracy :" + accuracy);
}
}
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_LIGHT){
Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
}
}
}
所有传感器:
[java] package androidegg.stu.sensor;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidEggSensorGetValueActivity extends Activity implements SensorEventListener{ //...①
/** Called when the activity is first created. */
private SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SensorManager的接口取得
sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
}
@Override
protected void onResume(){ //...②
super.onResume();
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
//sensor1
for (Sensor s : sensors){
sensorManager.registerListener(this,s,SensorManager.SENSOR_DELAY_NORMAL);
}
}
//
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent e) { //...③
// TODO Auto-generated method stub
switch(e.sensor.getType()){
//加速度的值表示
case Sensor.TYPE_ACCELEROMETER:{
TextView x = (TextView)findViewById(R.id.x);
x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
&nb
补充:移动开发 , Android ,