标签:style blog java color 使用 数据
<span style="font-size:12px;">//获取传感器列表 SensorManager mSensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> mSensors=mSensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor sensor : mSensors) { mSensorTv.append(sensor.getName()+"\n"); }</span>
<span style="font-size:12px;">/** * @author linwb */ public class MainActivity extends Activity implements SensorEventListener{ private TextView mSensorTv; private SensorManager mSensorManager; private float[] adjustArgs=new float[3]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSensorTv=(TextView) findViewById(R.id.sensorTv); mSensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); //第三个参数为刷新的频率 //SensorManager.SENSOR_DELAY_FASTEST 最快 //SensorManager.SENSOR_DELAY_GAME 游戏 //SensorManager.SENSOR_DELAY_NORMAL 普通 //SensorManager.SENSOR_DELAY_UI 用户界面 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_UI); } @Override public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: //去除杂质 在此之前先保证重力传感器存在 // float x=adjustArgs[0] * 0.9f + event.values[0] *( 1 - 0.9f); // float y=adjustArgs[1] * 0.9f + event.values[1] *( 1 - 0.9f); // float z=adjustArgs[2] * 0.9f + event.values[2] *( 1 - 0.9f); String reslut=(event.values[0])+" "+(event.values[1]) +" "+(event.values[2]-9.81f); mSensorTv.setText(reslut); break; case Sensor.TYPE_GRAVITY: adjustArgs[0]=event.values[0]; adjustArgs[1]=event.values[1]; adjustArgs[2]=event.values[2]; break; default: break; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }</span>
标签:style blog java color 使用 数据
原文地址:http://blog.csdn.net/qq285016127/article/details/37612869