首页 > 移动平台 > 详细

Android 传感器简记

时间:2018-03-16 21:14:08      阅读:260      评论:0      收藏:0      [点我收藏+]

显示所有传感器

sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

List<Sensor> allSensors = sm.getSensorList(Sensor.TYPE_ALL);
StringBuilder sb = new StringBuilder();

sb.append("此手机有" + allSensors.size() + "个传感器,分别有:\n\n");
for(Sensor s:allSensors){
     switch (s.getType()){
        case Sensor.TYPE_ACCELEROMETER:
            sb.append(s.getType() + " 加速度传感器(Accelerometer sensor)" + "\n");
            break;
         case Sensor.TYPE_GYROSCOPE:
            sb.append(s.getType() + " 陀螺仪传感器(Gyroscope sensor)" + "\n");
            break;
         case Sensor.TYPE_LIGHT:
            sb.append(s.getType() + " 光线传感器(Light sensor)" + "\n");
            break;
         case Sensor.TYPE_MAGNETIC_FIELD:
            sb.append(s.getType() + " 磁场传感器(Magnetic field sensor)" + "\n");
            break;
         case Sensor.TYPE_ORIENTATION:
             sb.append(s.getType() + " 方向传感器(Orientation sensor)" + "\n");
             break;
         case Sensor.TYPE_PRESSURE:
             sb.append(s.getType() + " 气压传感器(Pressure sensor)" + "\n");
              break;
         case Sensor.TYPE_PROXIMITY:
              sb.append(s.getType() + " 距离传感器(Proximity sensor)" + "\n");
              break;
          case Sensor.TYPE_TEMPERATURE:
              sb.append(s.getType() + " 温度传感器(Temperature sensor)" + "\n");
              break;
          default:
              sb.append(s.getType() + " 其他传感器" + "\n");
              break;
   }
sb.append(" 设备名称:" + s.getName() + "\n 设备版本:" + s.getVersion()
       +"\n 设备精度"+ s.getResolution()+"\n 设备最大范围"+s.getMaximumRange()
        +"\n 设备耗电量"+s.getPower()+"\n 供应商:" +s.getVendor() + "\n\n");
}
txt_show.setText(sb.toString());

使用简例

陀螺仪

public class OritionSensorActivity extends AppCompatActivity implements SensorEventListener 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_oritionsensor);

    sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensorOrientation = sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    sManager.registerListener(this, mSensorOrientation, SensorManager.SENSOR_DELAY_UI);

}

@Override
public void onSensorChanged(SensorEvent event) {
    tv_value1.setText("方位角:" + (float) (Math.round(event.values[0] * 100)) / 100);
    tv_value2.setText("倾斜角:" + (float) (Math.round(event.values[1] * 100)) / 100);
    tv_value3.setText("滚动角:" + (float) (Math.round(event.values[2] * 100)) / 100);
}

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

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (sManager != null) {
      sManager.unregisterListener(this);
   }
}

光线传感器

public class LightSensorActivity extends AppCompatActivity implements SensorEventListener
SensorManager sensorManager ;
Sensor sensor ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lightsensor);

        find();
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        sensorManager.registerListener(this,sensor,sensorManager.SENSOR_DELAY_FASTEST);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        tv_light.setText(sensorEvent.values[0]+"");
        sb_light.setClickable(false);
        sb_light.setProgress((int) sensorEvent.values[0],true);
    }

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

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sensorManager != null) {
            sensorManager.unregisterListener(this);
        }
    }

Android 传感器简记

原文:https://www.cnblogs.com/Mr-quin/p/8585133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!