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

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 extendsActivity implementsSensorEventListener {
    privateTextView tvAccelerometer;
    privateTextView tvMagentic;
    privateTextView tvLight;
    privateTextView tvOrientation;
    privateTextView tvSensors;

    @Override
    publicvoid 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
    publicvoid onSensorChanged(SensorEvent event) {
        // 通过getType方法获得当前传回数据的传感器类型
        switch(event.sensor.getType()) {
        caseSensor.TYPE_ACCELEROMETER: // 处理加速度传感器传回的数据
            String accelerometer ="加速度\n" + "X:" + event.values[0] +"\n"
                    +"Y:" + event.values[1] +"\n" + "Z:"+ event.values[2]
                    +"\n";
            tvAccelerometer.setText(accelerometer);
            break;
        caseSensor.TYPE_LIGHT: // 处理光线传感器传回的数据
            tvLight.setText("亮度:"+ event.values[0]);
            break;
        caseSensor.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;
        caseSensor.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
    publicvoid onAccuracyChanged(Sensor sensor,int accuracy) {
    }
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值