装好Adroid SDK后,在sample的指导下,写了几个简单的类似hello world的程序,在这里介绍一下所写的在android下调用传感器的程序。
Android中支持的几种传感器:
Sensor.TYPE_ACCELEROMETER:加速度传感器。
Sensor.TYPE_GYROSCOPE:陀螺仪传感器。
Sensor.TYPE_LIGHT:亮度传感器。
Sensor.TYPE_MAGNETIC_FIELD:地磁传感器。
Sensor.TYPE_ORIENTATION:方向传感器。
Sensor.TYPE_PRESSURE:压力传感器。
Sensor.TYPE_PROXIMITY:近程传感器。
Sensor.TYPE_TEMPERATURE:温度传感器。
使用传感器最关键的一些知识是:SensorManager是所有传感器的一个综合管理类,包括了传感器的种类、采样率、精准度等。我们可以通过getSystemService方法来取得一个SensorManager对象。使用传感器时,需要通过registerListener函数注册传感器,使用完后需要通过unregisterListener取消注册。
百闻不如一见,还是直接讲代码:
新建一个Sensors的工程,创建一个Sensors.java,内容如下:
package me.sigma.sensors;
import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Sensors extends Activity {
TextView myTextView1;//t
//gen
TextView myTextView2;//x
TextView myTextView3;//y
TextView myTextView4;//z
//acc
TextView myTextView5;//x
TextView myTextView6;//y
TextView myTextView7;//z
//ori
TextView myTextView8;//x
TextView myTextView9;//y
TextView myTextView10;//z
//Light
TextView myTextView11;//z
SensorManager mySensorManager;//
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView1 = (TextView) findViewById(R.id.myTextView1);
myTextView2 = (TextView) findViewById(R.id.myTextView2);
myTextView3 = (TextView) findViewById(R.id.myTextView3);
myTextView4 = (TextView) findViewById(R.id.myTextView4);
myTextView5 = (TextView) findViewById(R.id.myTextView5);
myTextView6 = (TextView) findViewById(R.id.myTextView6);
myTextView7 = (TextView) findViewById(R.id.myTextView7);
myTextView8 = (TextView) findViewById(R.id.myTextView8);
myTextView9 = (TextView) findViewById(R.id.myTextView9);
myTextView10 = (TextView) findViewById(R.id.myTextView10);
myTextView11 = (TextView) findViewById(R.id.myTextView11);
mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
private SensorListener mySensorListener = new SensorListener(){
@Override
public void onAccuracyChanged(int sensor, int accuracy) {}
@Override
public void onSensorChanged(int sensor, float[] values) {
if(sensor == SensorManager.SENSOR_TEMPERATURE){
myTextView1.setText("Current Temprature:"+values[0]);
}else if(sensor == SensorManager.SENSOR_MAGNETIC_FIELD){
myTextView2.setText("Current Magnetic x:"+values[0]);
myTextView3.setText("Current Magnetic y:"+values[1]);
myTextView4.setText("Current Magnetic z:"+values[2]);
}else if(sensor == SensorManager.SENSOR_ACCELEROMETER){
myTextView5.setText("Current Accelero x:"+values[0]);
myTextView6.setText("Current Accelero y:"+values[1]);
myTextView7.setText("Current Accelero z:"+values[2]);
}else if(sensor == SensorManager.SENSOR_ORIENTATION){
myTextView8.setText("Current Oraenttation x:"+values[0]);
myTextView9.setText("Current Oraenttation y:"+values[1]);
myTextView10.setText("Current Oraenttation z:"+values[2]);
}else if(sensor == SensorManager.SENSOR_LIGHT){
myTextView11.setText("Current Oraenttation x:"+values[0]);
}
}
};
@Override
protected void onResume() {
mySensorManager.registerListener(
mySensorListener,
SensorManager.SENSOR_TEMPERATURE |
SensorManager.SENSOR_MAGNETIC_FIELD |
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_LIGHT |
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_UI
);
super.onResume();
}
@Override
protected void onPause() {
mySensorManager.unregisterListener(mySensorListener);
super.onPause();
}
}
更改res/layout/下面的main.xml,为如下内容:
更改res/values/strings.xml为如下内容:
templator! templator Sigma Sensors Current Temprature: Current Magnetic x: Current Magnetic y: Current Magnetic z: Current Accelero x: Current Accelero y: Current Accelero z: Current Oraenttation x: Current Oraenttation y: Current Oraenttation z: Current Light:
Comments (6)
学习,收藏了~
你搞Android做啥阿?
Android黑飘过。。。
方法有问题。
1.yourActivity implements SensorEventListener
2.mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_FASTEST);
3. @Override
public void onSensorChanged(SensorEvent event) {
// Log.i(TAG, "onSensorChanged()...");
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
Log.i(TAG, "***lightValue: "+event.values[0]);
}
}
应该这样做。。。。
@yihongyuelan
谢谢指正!
Hi there would you mind stating which blog platform you're working with? I'm looking to start my own blog soon but I'm having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I'm looking for something unique. P.S My apologies for being off-topic but I had to ask!