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(); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。