Android supports multiple sensors. Some sensors have been used in Android frameworks and most sensors are used in applications.
1. Supported sensor types in Android:
Sensor |
Name in Java |
Local interface Name |
Value |
Acceleration |
Type_accelerometer |
Sensor_type_accelerometer |
1 |
Magnetic Field |
Type_magnetic_field |
Sensor_type_magnetic_field |
2 |
Direction |
Type_orientation |
Sensor_type_orientation |
3 |
Gyroscope |
Type_gyroscope |
Sensor_type_gyroscope |
4 |
Light (brightness) |
Type_light |
Sensor_type_light |
5 |
Pressure |
Type_pressure |
Sensor_type_pressure |
6 |
Temperature |
Type_temperature |
Sensor_type_temperature |
7 |
Approaching |
Type_proximity |
Sensor_type_proximity |
8 |
Ii. Android
System code distribution:
1) sensor system Java code
Code path: Framework/base/CORE/Java/Android/hardware
The directory contains the camera
And sensor. The content of sensor is sensor *. java.
File.
2) JNI part of the Sensor System
Code path: Framework/base/CORE/JNI/android_hardware_sensormanager.cpp
This section provides Android. Hardware. sensor. Manager
Class.
3) interfaces implemented at the hardware layer of the Sensor System
Header file path: Hardware/libhardware/include/hardware/sensors. h
The Hardware Abstraction Layer of the sensor system needs to be implemented by each system according to the interfaces defined in sensors. h.
The sensor part also contains the driver and Hardware Abstraction Layer of the underlying part, as well as the call department of the sensor on the upper layer.
3. Android sensor source code parsing:
The main sensor file in Android is:
Sensor. Java Single Sensor description file
Sensorevent. Java sensor system time class
Sensoreventlistener. Java sensor listening event (an interface)
Sensorlistener. Java sensor listener (Interface)
Core Management of sensormanager. Java Sensors
Sensor. Java defines some types of sensor constants, such as public static final type_magnetic_field = 2;. For specific parameters, refer to the sensor type (figure 1)
Sensormanager. Java
Public sensor getdefasensensor (INT type) {Get the Default Sensor}
Public list <Sensor> getsensorlist (INT type) {retrieve sensor list}
Public Boolean registerlistener (sensorlistener listener, int sensors ){
Return registerlistener (listener, sensors, sensor_delay_normal );
} // Register the listener event
Public void unregisterlistener (sensorlistener listener, int sensors) {deregister a listener event}
Time relationship: the source code is not mentioned one by one. You can check the source code yourself. If no source code exists, send me an email and I will send this code to everyone, let's take a simple demo for everyone to know. It's like this piece of code can be found on an IBM website!
4. Program code
1) sens?ti=. java code package COM. sensor; import android. app. activity; import android. hardware. sensoreventlistener; import android. hardware. sensorlistener; import android. hardware. sensormanager; import android. OS. bundle; import android. util. log; import android. widget. textview; public class sens+ti}extends activity implements sensorlistener {final string tag = "sens?ti="; sensormanager Sm = NULL; textview xviewa = NULL; textview yviewa = NULL; textview zviewa = NULL; textview xviewo = NULL; textview yviewo = NULL; textview zviewo = NULL;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Sm = (sensormanager) getsystemservice (sensor_service); xviewa = (textview) findviewbyid (R. id. xbox); yviewa = (textview) findviewbyid (R. id. ybox); zviewa = (textview) findviewbyid (R. id. zbox); xviewo = (textview) findviewbyid (R. id. xboxo); yviewo = (textview) findviewbyid (R. id. yboxo); zviewo = (textview) findviewbyid (R. id. zboxo) ;}@ overridepublic void onaccuracychanged (INT sensor, int accuracy) {// todo auto-generated method stub log. D (TAG, "onaccuracychanged:" + sensor + ", accuracy:" + accuracy) ;}@ overridepublic void onsensorchanged (INT sensor, float [] values) {// todo auto-generated method stub synchronized (this) {log. D (TAG, "onsensorchanged:" + sensor + ", X:" + values [0] + ", Y:" + values [1] + ", Z: "+ values [2]); If (sensor = sensormanager. sensor_orientation) {xviewo. settext ("orientation X:" + values [0]); yviewo. settext ("orientation Y:" + values [1]); zviewo. settext ("orientation Z:" + values [2]);} If (sensor = sensormanager. sensor_accelerometer) {xviewa. settext ("accel X:" + values [0]); yviewa. settext ("accel Y:" + values [1]); zviewa. settext ("accel Z:" + values [2]) ;}}@ overrideprotected void onresume () {super. onresume (); SM. registerlistener (this, sensormanager. sensor_orientation | sensormanager. sensor_accelerometer, sensormanager. sensor_delay_normal) ;}@ overrideprotected void onstop () {SM. unregisterlistener (this); super. onstop ();}
2) Main. xml layout file (simple textview) <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" androidrientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "accelerometer"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "x value" Android: id = "@ + ID/Xbox"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Y value" Android: id = "@ + ID/ybox"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "z value" Android: id = "@ + ID/zbox"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "orientation"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "x value" Android: Id = "@ + ID/xboxo"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Y value" Android: Id = "@ + ID/yboxo"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "z value" Android: Id = "@ + ID/zboxo"/> </linearlayout>
5. When developing and testing the sensor in the simulator, you must install a sensor plug-in to see the effect. Some mobile phone hardware drivers may not support sensor, however, mobile phones of popular brands are generally supported!
Take the time to sort out the tutorials for the first time. If there is something bad, don't tell me!