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:
- public class Lightdemoactivity extends Activity implements sensoreventlistener{
- /** called when the activity is first created. */
- Sensormanager Sensormanager = null;//Sensor Manager Reference
- Sensor Lightsensor = null;//Light sensor reference
- References to individual text
- TextView accuracy_view= null;
- TextView value_0 = null;
- TextView value_1 = null;
- TextView value_2 = null;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Get the Sensor Manager instance
- Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);
- Obtaining an instance of a light sensor
- Lightsensor = Sensormanager.getdefaultsensor (sensor.type_light);
- Get each TextView
- Accuracy_view = (TextView) Findviewbyid (r.id.accuracy);
- Value_0 = (TextView) Findviewbyid (R.ID.VALUE_0);
- Value_1 = (TextView) Findviewbyid (r.id.value_1);
- value_2 = (TextView) Findviewbyid (r.id.value_2);
- }
- @Override
- protected void OnPause () {
- TODO auto-generated Method Stub
- Super.onpause ();
- Cancellation
- Sensormanager.unregisterlistener (this, lightsensor);
- }
- @Override
- protected void Onresume () {
- TODO auto-generated Method Stub
- Super.onresume ();
- Monitor the Sensor Manager register
- Sensormanager.registerlistener (This,lightsensor, sensormanager.sensor_delay_normal);
- }
- @Override
- public void onaccuracychanged (sensor sensor, int accuracy) {
- TODO auto-generated Method Stub
- if (sensor.gettype () = = Sensor.type_light) {
- Set the value of accuracy to be displayed on the screen
- Accuracy_view.settext ("Accuracy:" +accuracy);
- }
- }
- @Override
- public void Onsensorchanged (Sensorevent event) {
- TODO auto-generated Method Stub
- if (event.sensor.getType () = = Sensor.type_light) {
- Display values to the screen
- Float[] values = event.values;
- Value_0.settext ("value[0]:" +values[0]);
- Value_1.settext ("value[1]:" +values[1]);
- Value_2.settext ("value[2]:" +values[2]);
- }
- }
- }
Copy Code
Let's take a look at the explanatory code:
Java code:
- The following explanation code:
- Get the Sensor Manager instance
- Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);
- Obtaining an instance of a light sensor
- Lightsensor = Sensormanager.getdefaultsensor (sensor.type_light);
- The purpose of this code is to obtain the Sensor Manager reference and obtain the light sensor application.
- protected void Onresume () {
- TODO auto-generated Method Stub
- Super.onresume ();
- Monitor the Sensor Manager register
- Sensormanager
- . Registerlistener (This,lightsensor, sensormanager.sensor_delay_normal);
- }
- The role of this code in the resume when the registration of monitoring, to open the light sense
- protected void OnPause () {
- TODO auto-generated Method Stub
- Super.onpause ();
- Cancellation
- Sensormanager.unregisterlistener (this, lightsensor);
- }
- 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.
- if (event.sensor.getType () = = Sensor.type_light)
- {
- Display values to the screen
- Float[] values = event.values;
- Value_0.settext ("value[0]:" +values[0]);
- Value_1.settext ("value[1]:" +values[1]);
- Value_2.settext ("value[2]:" +values[2]);
- }
- }
Copy Code
Android Deep Analytic Light sensor (ii)