用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

Android传感器(加速度传感器,磁场传感器,光线传感器,方向传感器)

2012-08-27 作者: 神马举报

[android]代码库

package mobile.android.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 Main extends Activity implements SensorEventListener {
	private TextView tvAccelerometer;
	private TextView tvMagentic;
	private TextView tvLight;
	private TextView tvOrientation;
	private TextView tvSensors;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获得SensorManager对象
		SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

		// 注册加速度传感器
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_FASTEST);

		// 注册磁场传感器
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
				SensorManager.SENSOR_DELAY_FASTEST);

		// 注册光线传感器
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
				SensorManager.SENSOR_DELAY_FASTEST);

		// 注册方向传感器
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				SensorManager.SENSOR_DELAY_FASTEST);

		tvAccelerometer = (TextView) findViewById(R.id.tvAccelerometer);
		tvMagentic = (TextView) findViewById(R.id.tvMagentic);
		tvLight = (TextView) findViewById(R.id.tvLight);
		tvOrientation = (TextView) findViewById(R.id.tvOrientation);
		tvSensors = (TextView) findViewById(R.id.tvSensors);

		// 获得当前手机支持的所有传感器
		List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
		for (Sensor sensor : sensors) {
			// 输出当前传感器的名称
			tvSensors.append(sensor.getName() + "\n");
		}
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// 通过getType方法获得当前传回数据的传感器类型
		switch (event.sensor.getType()) {
		case Sensor.TYPE_ACCELEROMETER: // 处理加速度传感器传回的数据
			String accelerometer = "加速度\n" + "X:" + event.values[0] + "\n"
					+ "Y:" + event.values[1] + "\n" + "Z:" + event.values[2]
					+ "\n";
			tvAccelerometer.setText(accelerometer);
			break;
		case Sensor.TYPE_LIGHT: // 处理光线传感器传回的数据
			tvLight.setText("亮度:" + event.values[0]);
			break;
		case Sensor.TYPE_MAGNETIC_FIELD: // 处理磁场传感器传回的数据
			String magentic = "磁场\n" + "X:" + event.values[0] + "\n" + "Y:"
					+ event.values[1] + "\n" + "Z:" + event.values[2] + "\n";
			tvMagentic.setText(magentic);
			break;
		case Sensor.TYPE_ORIENTATION: // 处理方向传感器传回的数据
			String orientation = "方向\n" + "X:" + event.values[0] + "\n" + "Y:"
					+ event.values[1] + "\n" + "Z:" + event.values[2] + "\n";
			tvOrientation.setText(orientation);
			break;
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}
}


网友评论    (发表评论)

共4 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...