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 { //設(shè)置LOG標(biāo)簽 private static final String TAG = "sensor"; private SensorManager sm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //創(chuàng)建一個(gè)SensorManager來獲取系統(tǒng)的傳感器服務(wù) sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); //選取加速度感應(yīng)器 int sensorType = Sensor.TYPE_ACCELEROMETER; sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL); } final SensorEventListener myAccelerometerListener = new SensorEventListener(){ //復(fù)寫onSensorChanged方法 public void onSensorChanged(SensorEvent sensorEvent){ if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ Log.i(TAG,"onSensorChanged"); //圖解中已經(jīng)解釋三個(gè)值的含義 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); } } //復(fù)寫onAccuracyChanged方法 public void onAccuracyChanged(Sensor sensor , int accuracy){ Log.i(TAG, "onAccuracyChanged"); } }; public void onPause(){ sm.unregisterListener(myAccelerometerListener); super.onPause(); } }
分享名稱:安卓獲取重力加速度例子
本文鏈接:http://muchs.cn/article12/jpdedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、App設(shè)計(jì)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)