Android development-sensor Introduction

Source: Internet
Author: User

To obtain a Sensor from a mobile phone, obtain the SensorManager object and execute the getDefaultSensor () method of the SensorManager object to obtain the Sensor Object. Here, the getdefasensensor () method must pass a parameter, specify a specific Sensor type to obtain various attributes of the Sensor object. The following code is used to obtain the Sensor in the mobile phone: [java] Log. d ("BruceZhang", "The following are the sensors in your mobile phone:"); List <Sensor> sensors = sensorManager. getSensorList (Sensor. TYPE_ALL); Log. d ("BruceZhang", "--->"); for (Sensor sensor: sensors) {Log. d ("BruceZhang", sensor. getName ();} // use the getdefasensensor method to obtain a specific Sensor/sensor Sensor = sensorManager. getdefasensensor (Sensor. TYPE_GRAVITY); after knowing how to obtain the sensor, we need to use the program to obtain the sensor value. The following example code is provided: [java] SensorManager sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); Sensor sensor = sensorManager. getdefasensensor (Sensor. TYPE_LIGHT); sensorManager. registerListener (new SensorEventListener () {/** SensorEvent event * once the sensor detects a change in external conditions, the corresponding data will be passed to this object * This method is generally put in onCreate, and you do not need to manually obtain */@ Override public void onSensorChanged (SensorEvent event) {// TODO Auto-generated method stub float auc = event. accuracy; // The first element here represents the light intensity float lux = event. values [0]; Log. d ("BruceZhang", auc + ""); Log. d ("BruceZhang", lux + "") ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// TODO Auto-generated method stub}, sensor, sensorManager. SENSOR_DELAY_NORMAL); // SensorManager. SENSOR_DELAY_NORMAL // This parameter sets the sensor sampling rate} the sensor has a series of attributes: 1. Sensor sampling rate a) SENSOR_DELAY_NOMAL (200000 microseconds) B) SENSOR_DELAY_UI (60000 microseconds) c) SENSOR_DELAY_GAME (20000 microseconds) d) SENSOR_DELAY_FASTEST (0 microseconds) 2. Sensor attribute a) sensor name B) sensor manufacturer c) sensor power d) the resulotion method of the sensor to obtain these attributes is also very simple. The Code is as follows: [java] SensorManager sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); Sensor sensor = sensorManager. getdefasensensor (Sensor. TYPE_LIGHT); sensorManager. registerListener (new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent event) {// TODO Auto-generated method stub // Sensor Name String sensorName = event. sensor. getName (); // sensor manufacturer name String sensorVendor = event. sensor. getVendor (); // sensor resolution. The minimum accuracy that the sensor can recognize is float sensorRevolution = event. sensor. getResolution (); // sensor power float sensorPower = event. sensor. getPower (); // how long is the sensor's data acquisition time double timeStamp = event. timestamp; // print the data Log. d ("BruceZhang", sensorName + ""); Log. d ("BruceZhang", sensorVendor + ""); Log. d ("BruceZhang", sensorRevolution + ""); Log. d ("BruceZhang", sensorPower + ""); Log. d ("BruceZhang", timeStamp + "") ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// TODO Auto-generated method stub }}, sensor, sensorManager. SENSOR_DELAY_NORMAL);} The following example realizes the synthesis of the above content: the complete code of the instance is as follows: [java]/** the sensor will pass the value to the user through the sensor event, our applications are passive, waiting for the sensor to pass data to the SensorEvent object */public class MainActivity extends Activity {private Button button; private Button button2; private Button button3; private SensorManager sensorManager; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the SensorManager object sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); button = (Button) findViewById (R. id. sensorButton); button2 = (Button) findViewById (R. id. lightButton); button3 = (Button) findViewById (R. id. lightButton2); button3.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub SensorManager sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); Sensor sensor = sensorManager. getdefasensensor (Sensor. TYPE_LIGHT); sensorManager. registerListener (new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent event) {// TODO Auto-generated method stub // Sensor Name String sensorName = event. sensor. getName (); // sensor manufacturer name String sensorVendor = event. sensor. getVendor (); // sensor resolution. The minimum accuracy that the sensor can recognize is float sensorRevolution = event. sensor. getResolution (); // sensor power float sensorPower = event. sensor. getPower (); // how long is the sensor's data acquisition time double timeStamp = event. timestamp; // print the data Log. d ("BruceZhang", sensorName + ""); Log. d ("BruceZhang", sensorVendor + ""); Log. d ("BruceZhang", sensorRevolution + ""); Log. d ("BruceZhang", sensorPower + ""); Log. d ("BruceZhang", timeStamp + "") ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// TODO Auto-generated method stub }}, sensor, sensorManager. SENSOR_DELAY_NORMAL) ;}}); button2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub SensorManager sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE); Sensor sensor = sensorManager. getdefasensensor (Sensor. TYPE_LIGHT); sensorManager. registerListener (new SensorEventListener () {/** SensorEvent event * once the sensor detects a change in external conditions, the corresponding data will be passed to this object * This method is generally put in onCreate, and you do not need to manually obtain */@ Override public void onSensorChanged (SensorEvent event) {// TODO Auto-generated method stub float auc = event. accuracy; // The first element here represents the light intensity float lux = event. values [0]; Log. d ("BruceZhang", auc + ""); Log. d ("BruceZhang", lux + "") ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// TODO Auto-generated method stub}, sensor, sensorManager. SENSOR_DELAY_NORMAL); // SensorManager. SENSOR_DELAY_NORMAL // This parameter sets the sensor's sampling rate}); button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub Log. d ("BruceZhang", "The following are the sensors in your mobile phone:"); List <Sensor> sensors = sensorManager. getSensorList (Sensor. TYPE_ALL); Log. d ("BruceZhang", "--->"); for (Sensor sensor: sensors) {Log. d ("BruceZhang", sensor. getName ();} // use the getdefasensensor method to obtain a specific Sensor // sensor = sensorManager. getdefasensensor (Sensor. TYPE_GRAVITY) ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}}

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.