Android Deep Analytic Light sensor (ii)

Source: Internet
Author: User

Light Sensor Demo 1

Having told a whole bunch of theories, the following example shows the use of a light sensor. Why fully show the usage of light sense, I This example is very easy to write, just write the use of light sense necessary code, and then use a few TextView to display the value of light changes to the user interface. Allows the user to see changes in luminosity at any time.




Below are Activity 's Code :


Java code:

  1. public class Lightdemoactivity extends Activity implements sensoreventlistener{
  2. /** called when the activity is first created. */
  3. Sensormanager Sensormanager = null;//Sensor Manager Reference
  4. Sensor Lightsensor = null;//Light sensor reference

  5. References to individual text
  6. TextView accuracy_view= null;
  7. TextView value_0 = null;
  8. TextView value_1 = null;
  9. TextView value_2 = null;

  10. @Override
  11. public void OnCreate (Bundle savedinstancestate) {
  12. Super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.main);

  14. Get the Sensor Manager instance
  15. Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);
  16. Obtaining an instance of a light sensor
  17. Lightsensor = Sensormanager.getdefaultsensor (sensor.type_light);
  18. Get each TextView
  19. Accuracy_view = (TextView) Findviewbyid (r.id.accuracy);
  20. Value_0 = (TextView) Findviewbyid (R.ID.VALUE_0);
  21. Value_1 = (TextView) Findviewbyid (r.id.value_1);
  22. value_2 = (TextView) Findviewbyid (r.id.value_2);
  23. }

  24. @Override
  25. protected void OnPause () {
  26. TODO auto-generated Method Stub
  27. Super.onpause ();
  28. Cancellation
  29. Sensormanager.unregisterlistener (this, lightsensor);
  30. }

  31. @Override
  32. protected void Onresume () {
  33. TODO auto-generated Method Stub
  34. Super.onresume ();
  35. Monitor the Sensor Manager register
  36. Sensormanager.registerlistener (This,lightsensor, sensormanager.sensor_delay_normal);
  37. }

  38. @Override
  39. public void onaccuracychanged (sensor sensor, int accuracy) {
  40. TODO auto-generated Method Stub
  41. if (sensor.gettype () = = Sensor.type_light) {
  42. Set the value of accuracy to be displayed on the screen
  43. Accuracy_view.settext ("Accuracy:" +accuracy);
  44. }

  45. }

  46. @Override
  47. public void Onsensorchanged (Sensorevent event) {
  48. TODO auto-generated Method Stub
  49. if (event.sensor.getType () = = Sensor.type_light) {

  50. Display values to the screen
  51. Float[] values = event.values;
  52. Value_0.settext ("value[0]:" +values[0]);
  53. Value_1.settext ("value[1]:" +values[1]);
  54. Value_2.settext ("value[2]:" +values[2]);
  55. }
  56. }

  57. }
Copy Code
Let's take a look at the explanatory code:

Java code:
  1. The following explanation code:
  2. Get the Sensor Manager instance
  3. Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);
  4. Obtaining an instance of a light sensor
  5. Lightsensor = Sensormanager.getdefaultsensor (sensor.type_light);

  6. The purpose of this code is to obtain the Sensor Manager reference and obtain the light sensor application.
  7. protected void Onresume () {
  8. TODO auto-generated Method Stub
  9. Super.onresume ();
  10. Monitor the Sensor Manager register
  11. Sensormanager
  12. . Registerlistener (This,lightsensor, sensormanager.sensor_delay_normal);
  13. }
  14. The role of this code in the resume when the registration of monitoring, to open the light sense
  15. protected void OnPause () {
  16. TODO auto-generated Method Stub
  17. Super.onpause ();
  18. Cancellation
  19. Sensormanager.unregisterlistener (this, lightsensor);
  20. }
  21. The purpose of this code is to log off the monitor when paused and turn off the light sense. Because the sensor consumes electricity, it should be shut down when not in use.
  22. if (event.sensor.getType () = = Sensor.type_light)
  23. {
  24. Display values to the screen
  25. Float[] values = event.values;
  26. Value_0.settext ("value[0]:" +values[0]);
  27. Value_1.settext ("value[1]:" +values[1]);
  28. Value_2.settext ("value[2]:" +values[2]);
  29. }

  30. }
Copy Code

Android Deep Analytic Light sensor (ii)

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.