Работа с акселерометром
- package com.mycompany.myapp;
- import android.app.*;
- import android.hardware.*;
- import android.os.*;
- import android.util.*;
- import android.widget.*;
- public class MainActivity extends Activity implements SensorEventListener {
- public SensorManager sm;
- public TextView text;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- text = (TextView)findViewById(R.id.text);
- sm = (SensorManager)getSystemService(SENSOR_SERVICE);
- sm.registerListener(this, sm.getDefaultSensor(
- Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
- }
- public void onSensorChanged(SensorEvent p1) {
- float[] v = p1.values;
- float x = v[0];
- float y = v[1];
- float z = v[2];
- text.setText("X="+x+"\nY="+y+"\nZ="+z);
- }
- public void onAccuracyChanged(Sensor p1, int p2) { }
- protected void onPause() {
- super.onPause();
- sm.unregisterListener(this);
- }
- protected void onResume() {
- super.onResume();
- sm.registerListener(this, sm.getDefaultSensor(
- Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
- }
- }
Простой пример.