Android _ Android sensor development (acceleration sensor, magnetic field sensor, light sensor, direction sensor)

Source: Internet
Author: User
import
java.util.List;
import
android.app.Activity;
import
android.hardware.Sensor;
import
android.hardware.SensorEvent;
import
android.hardware.SensorEventListener;
import
android.hardware.SensorManager;
import
android.os.Bundle;
import
android.widget.TextView;
 
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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Obtain the sensormanager object
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
 
        // Register an acceleration sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);
 
        // Register the Magnetic Field Sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_FASTEST);
 
        // Register the light sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_FASTEST);
 
        // Register the direction Sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_FASTEST);
 
        tvAccelerometer = (TextView) findViewById(R.id.tvAccelerometer);
        tvMagentic = (TextView) findViewById(R.id.tvMagentic);
        tvLight = (TextView) findViewById(R.id.tvLight);
        tvOrientation = (TextView) findViewById(R.id.tvOrientation);
        tvSensors = (TextView) findViewById(R.id.tvSensors);
 
        // Obtain all the sensors supported by the current mobile phone
        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
        for
(Sensor sensor : sensors) {
            // Output the name of the Current Sensor
            tvSensors.append(sensor.getName() +
"\n");
        }
    }
 
    @Override
    public
void onSensorChanged(SensorEvent event) {
        // Use the GetType method to obtain the sensor type of the currently returned data
        switch
(event.sensor.getType()) {
        case
Sensor.TYPE_ACCELEROMETER: // Process the data returned by the accelerometer
            String accelerometer =
"Acceleration \ n" +
"X:" + event.values[0] +
"\n"
                    +
"Y:" + event.values[1] +
"\n" + "Z:"
+ event.values[2]
                    +
"\n";
            tvAccelerometer.setText(accelerometer);
            break;
        case
Sensor.TYPE_LIGHT: // Process the data returned by the light sensor
            tvLight.setText("Brightness :"
+ event.values[0]);
            break;
        case
Sensor.TYPE_MAGNETIC_FIELD: // Process the data returned by the Magnetic Field Sensor
            String magentic =
"Magnetic field \ n" +
"X:" + event.values[0] +
"\n" + "Y:"
                    + event.values[1] +
"\n" + "Z:"
+ event.values[2] +
"\n";
            tvMagentic.setText(magentic);
            break;
        case
Sensor.TYPE_ORIENTATION: // Process the data returned from the sensor.
            String orientation =
"Direction \ n" +
"X:" + event.values[0] +
"\n" + "Y:"
                    + event.values[1] +
"\n" + "Z:"
+ event.values[2] +
"\n";
            tvOrientation.setText(orientation);
            break;
        }
    }
 
    @Override
    public
void onAccuracyChanged(Sensor sensor,
int accuracy) {
    }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.