Android Sensor Overview (vi)

Source: Internet
Author: User

Monitoring sensor events

To monitor the original sensor data, you need to implement two callback methods exposed via the Sensoreventlistener Interface: onaccuracychanged () and onsensorchanged (). The Android system calls both methods whenever the following things happen:

1. Change in sensor accuracy:

In such a case, the system calls the Onaccuracychanged () method, which provides the sensor object to which you want to reference the occurrence of the change in precision. Precision is represented by one of the following four state constants:

Sensor_status_accuracy_low

Sensor_status_accuracy_medium

Sensor_status_accuracy_high

Sensor_status_unreliable

2. The sensor reports a new value:

In such a case, the system calls the Onsensorchanged () method, which provides a Sensorevent object. The Sensorevent object includes information about the new sensor data, including the accuracy of the data, the sensor that generated the data, the timestamp when the data was generated, and the new data that the sensor recorded.

The following code shows how to use the Onsensorchanged () method to monitor data from a luminance sensor. This example displays the original sensor data in a TextView:

 Public class sensoractivity extends Activity Implements Sensoreventlistener {
Private Sensormanager Msensormanager ;
Private Sensor Mlight ;
@Override
 Public Final void onCreate (Bundle  savedinstancestate) {
Super . onCreate (savedinstancestate);
Setcontentview(R. Layout . Main );
       msensormanager =   ( sensormanager  )   Getsystemservice   ( context   sensor_service     
= Msensormanager . Getdefaultsensor (Sensor. Type_light );
}
@Override
 Public Final void onaccuracychanged (sensor  sensor,int  accuracy) {
Do something the here if sensor accuracy changes.
}
@Override
 Public Final void onsensorchanged (sensoreventevent) {
The light sensor returns a single value.
Many sensors return 3 values, one for each axis.
float=event. Values [0];
Do something with this sensor value.
}

@Override

protected void Onresume () {

Super.onresume ();

Msensormanager.registerlistener (This, mlight, sensormanager.sensor_delay_normal);

 }

@Override

protected void OnPause () {

super.onpause ();

Msensormanager.unregisterlistener (this);

 }

}

In this example, the default data delay (sensor_delay_normal) is specified when the Registerlistener () method is called. Data latency (or sample rate) controls the interval of time that sensor events are sent to your application via the onsensorchanged () callback method. The default data delay is for monitoring typical screen orientation changes, which use a time delay of 200,000 milliseconds. You can specify additional data delay types, such as sensor_delay_game (20,000 millisecond delay), sensor_delay_ui (60,000 millisecond delay), or sensor_delay_fastest (0 millisecond delay). Android3.0 (API level 11), you can also use an absolute value (in milliseconds) to specify the delay.

The delay you specify is only a suggested delay. This delay can be changed by Android and other applications. As a best practice, you should specify the maximum latency you need, as the system typically uses a smaller delay than you specify (that is, you should choose the lowest sample rate your app needs). Using a large delay reduces the load on the processor and reduces the loss of power at the same time.

There is no public way to infer how often the sensor frame sends sensor events to your application, however, you can calculate the sample rate using the timestamp of the two sensor events before and after. Once you set the sample rate (delay) do not change. Assuming that for some reason a change is required, you must log out and then register the sensor listener again.

In this example, it is also important to focus on using the Onresume () and OnPause () callback methods to register and unregister the sensor listener events. As a best practice, you should always disable the sensor when it is not needed, especially when the activity is suspended. Assuming this is not the case, because some sensors have very large power requirements, they can consume battery power very quickly. When the screen is off, the system does not actively disable the sensor itself.

Android Sensor Overview (vi)

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.