扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
package uni.sensor; import java.util.Iterator; import java.util.List; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; public class SensorDemoActivity extends Activity { //设置LOG标签 private static final String TAG = "sensor"; private SensorManager sm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建一个SensorManager来获取系统的传感器服务 sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); //选取加速度感应器 int sensorType = Sensor.TYPE_ACCELEROMETER; sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL); } final SensorEventListener myAccelerometerListener = new SensorEventListener(){ //复写onSensorChanged方法 public void onSensorChanged(SensorEvent sensorEvent){ if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ Log.i(TAG,"onSensorChanged"); //图解中已经解释三个值的含义 float X_lateral = sensorEvent.values[0]; float Y_longitudinal = sensorEvent.values[1]; float Z_vertical = sensorEvent.values[2]; Log.i(TAG,"\n heading "+X_lateral); Log.i(TAG,"\n pitch "+Y_longitudinal); Log.i(TAG,"\n roll "+Z_vertical); } } //复写onAccuracyChanged方法 public void onAccuracyChanged(Sensor sensor , int accuracy){ Log.i(TAG, "onAccuracyChanged"); } }; public void onPause(){ sm.unregisterListener(myAccelerometerListener); super.onPause(); } }
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流