Android 传感器简记

Posted 超界:亿万年的星光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 传感器简记相关的知识,希望对你有一定的参考价值。

显示所有传感器

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 传感器简记的主要内容,如果未能解决你的问题,请参考以下文章

android系统又一次刷ROM简记

简记Ubuntu下载 Android源码

Android:正确停止异步任务

Android代码片段

Android课程---Android Studio使用小技巧:提取方法代码片段

Android 实用代码片段