This article describes the method of Android development to obtain sensor data. Share it for your reference, as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; public class Main extends Activity implements SensorEventListener { private TextView tvAccelerometer; private TextView tvMagentic; private TextView tvLight; private TextView tvOrientation; private TextView tvSensors; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); // Obtain SensorManager object SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // Register the acceleration sensor (this, (Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST); // Register a magnetic field sensor (this, (Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_FASTEST); // Register a light sensor (this, (Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_FASTEST); // Register direction sensor (this, (Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST); tvAccelerometer = (TextView) findViewById(); tvMagentic = (TextView) findViewById(); tvLight = (TextView) findViewById(); tvOrientation = (TextView) findViewById(); tvSensors = (TextView) findViewById(); // Get all sensors supported by the current phone List<Sensor> sensors = (Sensor.TYPE_ALL); for (Sensor sensor : sensors) { // Output the name of the current sensor (() + "\n"); } } @Override public void onSensorChanged(SensorEvent event) { // Get the sensor type of the current data being returned through the getType method switch (()) { case Sensor.TYPE_ACCELEROMETER: // Process data sent back by the acceleration sensor String accelerometer = "Acceleration\n" + "X:" + [0] + "\n" + "Y:" + [1] + "\n" + "Z:" + [2] + "\n"; (accelerometer); break; case Sensor.TYPE_LIGHT: // Process data sent back by the light sensor ("brightness:" + [0]); break; case Sensor.TYPE_MAGNETIC_FIELD: // Process data sent back by magnetic field sensor String magentic = "Magnetic Field\n" + "X:" + [0] + "\n" + "Y:" + [1] + "\n" + "Z:" + [2] + "\n"; (magentic); break; case Sensor.TYPE_ORIENTATION: // Process data sent back by the direction sensor String orientation = "Direction\n" + "X:" + [0] + "\n" + "Y:" + [1] + "\n" + "Z:" + [2] + "\n"; (orientation); break; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
PS:All we get here is the sensor to collect data, and it also needs to analyze the data to become useful information.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.