Android ApiDemos example resolution (90): OS-& gt; Sensors

Source: Internet
Author: User

In the previous example, the Android ApiDemos example resolution (61): Graphics-> Compass and the Android ApiDemos example resolution (79): Graphics-> SensorTest involves Sensors.

This example introduces the Sensors usage supported by Android.

SensorManager is used to manage various sensors in Android devices. The method for obtaining the SensorManager instance object is to use getSystemService:


[Java]
Private SensorManager mSensorManager;
...
MSensorManager = (SensorManager)
GetSystemService (Context. SENSOR_SERVICE );

Private SensorManager mSensorManager;
...
MSensorManager = (SensorManager)
GetSystemService (Context. SENSOR_SERVICE );


SensorListener and SensorEventListener define the callback function of the sensor of the monitoring device. The methods defined by SensorListener and SensorEventListener are similar, mainly because the parameters of the Sensor type are different. SensorListener is an out-of-date interface and should not be reused. In this example, SensorListener is used.

It defines two callback functions:


[Java]
Public void onSensorChanged (int sensor, float [] values ){
MValues = values;
If (mView! = Null ){
MView. invalidate ();
}
}

Public void onAccuracyChanged (int sensor, int accuracy ){
// TODO Auto-generated method stub

}

Public void onSensorChanged (int sensor, float [] values ){
MValues = values;
If (mView! = Null ){
MView. invalidate ();
}
}
 
Public void onAccuracyChanged (int sensor, int accuracy ){
// TODO Auto-generated method stub
 
}
Sensor specifies the Sensor type, whose type is int, while SensorEventListener uses the sensor type. The values array defines the degree of the value direction of the X, Y, and Z sensors. The definitions of X, Y, and Z are different in the horizontal and vertical directions. values [3], values [4], values [5] defines the X, Y, and Z values irrelevant to the screen direction.

Android devices may have multiple sensors with different precision. When the precision is changed, onAccuracyChanged is triggered. The sensor specifies the sensor type, while accuracy is new:

SensorManager. SENSOR_STATUS_ACCURACY_HIGH sensor reports High Precision values
SensorManager. SENSOR_STATUS_ACCURACY_LOW sensor reports low-Precision values
SensorManager. SENSOR_STATUS_ACCURACY_MEDIUM sensor reports average accuracy value
The SensorManager. SENSOR_STATUS_ACCURACY_UNRELIABLE sensor reports unreliable Precision values.
Sensor specifies the sensor type. The Android system may support the following types of sensors:

SENSOR_ACCELEROMETER: an acceleration sensor that detects acceleration in the X, Y, and Z directions (m/s2 ).
SENSOR_ORIENTATION: Orientation sensor, angle along the X, Y, and Z directions.
SENSOR_LIGHT: brightness sensor that detects the current light intensity.
SENSOR_MAGNETIC_FIELD: magnetic field sensor that detects magnetic flux in the X, Y, and Z directions.
SENSOR_PROXIMITY: A location sensor that detects the distance between the current location and the target location.
SENSOR_TEMPERATURE: temperature sensor used to detect the current temperature.
In the latest Android development kit, a new Sensor class is provided to replace the Sensor type originally represented by int. We recommend that you use this Sensor class in new applications.

If you want to specify a Sensor, you can specify it when registerListener. The Sensor is a shared device. For a "good" application, you need to clear the monitoring of the Sensor when no Sensor is needed, the onResume in this response is in the onStop method:


[Java]
@ Override
Protected void onResume (){
Super. onResume ();
MSensorManager. registerListener (mGraphView,
SensorManager. SENSOR_ACCELEROMETER |
SensorManager. SENSOR_MAGNETIC_FIELD |
SensorManager. SENSOR_ORIENTATION,
SensorManager. SENSOR_DELAY_FASTEST );
}

@ Override
Protected void onStop (){
MSensorManager. unregisterListener (mGraphView );
Super. onStop ();
}

@ Override
Protected void onResume (){
Super. onResume ();
MSensorManager. registerListener (mGraphView,
SensorManager. SENSOR_ACCELEROMETER |
SensorManager. SENSOR_MAGNETIC_FIELD |
SensorManager. SENSOR_ORIENTATION,
SensorManager. SENSOR_DELAY_FASTEST );
}
 
@ Override
Protected void onStop (){
MSensorManager. unregisterListener (mGraphView );
Super. onStop ();
}


In addition to the Sensor type to be monitored, the registerListener can also specify the Sensor update frequency. The supported frequencies support the following options:

SENSOR_DELAY_FASTEST: select the frequency as fast as possible.
SENSOR_DELAY_GAME: select the update frequency suitable for game applications.
SENSOR_DELAY_NORMAL: select the default update frequency.
SENSOR_DELAY_UI: select the frequency suitable for UI updates.
In this example, onSensorChanged is defined as follows. SENSOR_ORIENTATION, SENSOR_MAGNETIC_FIELD, and SENSOR_MAGNETIC_FIELD are used for testing by phone:

[Java]
Public void onSensorChanged (int sensor, float [] values ){
Synchronized (this ){
If (mBitmap! = Null ){
Final Canvas canvas = mCanvas;
Final Paint paint = mPaint;
If (sensor = SensorManager. SENSOR_ORIENTATION ){
For (int I = 0; I <3; I ++ ){
MOrientationValues [I] = values [I];
}
} Else {
Float deltaX = mSpeed;
Float newX = mLastX + deltaX;

Int j = (sensor = SensorManager. SENSOR_MAGNETIC_FIELD )? 1: 0;
For (int I = 0; I <3; I ++ ){
Int k = I + j * 3;
Final float v = mYOffset + values [I] * mScale [j];
Paint. setColor (mColors [k]);
Canvas. drawLine (mLastX, mLastValues [k], newX, v, paint );
MLastValues [k] = v;
}
If (sensor = SensorManager. SENSOR_MAGNETIC_FIELD)
MLastX + = mSpeed;
}
Invalidate ();
}
}
}

Public void onSensorChanged (int sensor, float [] values ){
Synchronized (this ){
If (mBitmap! = Null ){
Final Canvas canvas = mCanvas;
Final Paint paint = mPaint;
If (sensor = SensorManager. SENSOR_ORIENTATION ){
For (int I = 0; I <3; I ++ ){
MOrientationValues [I] = values [I];
}
} Else {
Float deltaX = mSpeed;
Float newX = mLastX + deltaX;
 
Int j = (sensor = SensorManager. SENSOR_MAGNETIC_FIELD )? 1: 0;
For (int I = 0; I <3; I ++ ){
Int k = I + j * 3;
Final float v = mYOffset + values [I] * mScale [j];
Paint. setColor (mColors [k]);
Canvas. drawLine (mLastX, mLastValues [k], newX, v, paint );
MLastValues [k] = v;
}
If (sensor = SensorManager. SENSOR_MAGNETIC_FIELD)
MLastX + = mSpeed;
}
Invalidate ();
}
}
}

 
Author: mapdigit
 

 


 

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.