Android感應器使用

來源:互聯網
上載者:User

public class SensorTest extends Activity implements SensorEventListener { SensorManager sensorManager = null; //for accelerometer values TextView outputX; TextView outputY; TextView outputZ; //for orientation values TextView outputX2; TextView outputY2; TextView outputZ2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化sensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); setContentView(R.layout.main); //just some textviews, for data output outputX = (TextView) findViewById(R.id.TextView01); outputY = (TextView) findViewById(R.id.TextView02); outputZ = (TextView) findViewById(R.id.TextView03); outputX2 = (TextView) findViewById(R.id.TextView04); outputY2 = (TextView) findViewById(R.id.TextView05); outputZ2 = (TextView) findViewById(R.id.TextView06); }//在onResume中註冊監聽 @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), sensorManager.SENSOR_DELAY_GAME); }//在onStop中登出監聽 @Override protected void onStop() { super.onStop(); sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)); sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION)); }//當感應器狀態發生改變時 public void onSensorChanged(SensorEvent event) { synchronized (this) { switch (event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: outputX.setText("x:"+Float.toString(event.values[0])); outputY.setText("y:"+Float.toString(event.values[1])); outputZ.setText("z:"+Float.toString(event.values[2])); break; case Sensor.TYPE_ORIENTATION: outputX2.setText("x:"+Float.toString(event.values[0])); outputY2.setText("y:"+Float.toString(event.values[1])); outputZ2.setText("z:"+Float.toString(event.values[2])); break; } } }//更精確的改變 @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} }

To sum things up, to access sensor data you have to do the following things:
1. Check sensor availability.
2. Register a listener to a sensorManager.
3. Catch the needed data , from onSensorChanged.
4. Unregister the sensorManager's listener.

另:其他參考資料

1, http://www.ibm.com/developerworks/opensource/library/os-android-sensor/index.html?ca=dgr-lnxw09Android-Sensors&S_TACT=105AGX59&S_CMP=grlnxw09

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.