• Android传感器使用


    package com.king.android.speical;
    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 com.king.android.R;
    /**
     * 描述:传感器
     * 作者:Andy.Liu
     * 时间: 2012-7-16  上午07:22:19
     **/
    public class SensorActivity extends Activity implements SensorEventListener {
    private boolean mRegisterSensor = false;
    private SensorManager mSensorManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }
    @Override
    protected void onResume() {
    super.onResume();
    List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
    if(sensors.size()>0){
    Sensor sensor = sensors.get(0);
    //注册Sensor ,接收频率。
    mRegisterSensor = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
    }
    @Override
    protected void onPause() {
    if(mRegisterSensor){
    mSensorManager.unregisterListener(this);
    mRegisterSensor = false;
    }
    super.onPause();
    }
    //当精确值发生改变时、
    //Accuracy精确值
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    //处理精确值改变
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){
    float x = event.values[SensorManager.DATA_X];
    float y = event.values[SensorManager.DATA_Y];
    float z = event.values[SensorManager.DATA_Z];
    }
    }
    }
    /**
     SensorManager中的其它方法
     getDefaultSensor:得到默认的传感器对象
     getInclination:得到地磁传感器倾斜的弧度值
     getOrientation:得到设备的旋转方向
     getSensorList:得到指定传感器列表。
    **/
  • 相关阅读:
    greenDAO缓存遇到的大坑的解决方法
    hdoj-1004-Let the Balloon Rise(map排序)
    hdu-2673-shǎ崽 OrOrOrOrz(水题)
    hdoj-1106-排序(stringstream)
    CodeForces
    hdoj-1027-Ignatius and the Princess II(逆康拓展开)
    Alex and broken contest CodeForces
    hdoj-1715-大菲波数(大斐波那契数列)
    nyoj-155-求高精度幂(java大数)
    nyoj-655-光棍的yy(大斐波那契数列)
  • 原文地址:https://www.cnblogs.com/liuzenglong/p/2592952.html
Copyright © 2020-2023  润新知