Android Study Notes (9): gravity Sensor

Source: Internet
Author: User

In Android, all sensors must be accessed through SensorMannager,

Call the getSystemService (SENSOR_SERVICE) method to obtain the sensor management object of the mobile phone,

OnSensorChanged (SensorEvent se) is called when the sensor value is changed,

To use a Sensor, you must call the registerListener (SensorEventListener listener, sensor Sensor, int rate) method for registration,

The sensor has three float values: x, y, and z. The value ranges from-10 to 10,


X = 0, y = 0, z = 10


When the mobile phone screen is placed horizontally down (z axis toward the ground), x = 0, y = 0, z =-10


X = 10, y = 0, z = 0


When the mobile phone is vertical (y axis to day), x = 0, y = 10, z = 0


Code:
PS: There is a TextView in the XML file to display the value of the gravity sensor,


[Java] public class TestActivity extends Activity {
TextView textView;
Float x, y, z;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. textView );
// Obtain the gravity sensor hardware controller
SensorManager sm = (SensorManager) this. getSystemService (SENSOR_SERVICE );
Sensor sensor = sm. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
// Add a gravity sensing listener and implement the method,
SensorEventListener sel = new SensorEventListener (){
Public void onSensorChanged (SensorEvent se ){
X = se. values [SensorManager. DATA_X];
Y = se. values [SensorManager. DATA_Y];
Z = se. values [SensorManager. DATA_Z];
TextView. setText ("x =" + (int) x + "y =" + (int) y + "z =" + (int) z );
}
Public void onAccuracyChanged (Sensor arg0, int arg1 ){
}
};
// Register Listener. SENSOR_DELAY_GAME indicates the detection accuracy,
Sm. registerListener (sel, sensor, SensorManager. SENSOR_DELAY_GAME );
}
Public class TestActivity extends Activity {
TextView textView;
Float x, y, z;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. textView );
// Obtain the gravity sensor hardware controller
SensorManager sm = (SensorManager) this. getSystemService (SENSOR_SERVICE );
Sensor sensor = sm. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
// Add a gravity sensing listener and implement the method,
SensorEventListener sel = new SensorEventListener (){
Public void onSensorChanged (SensorEvent se ){
X = se. values [SensorManager. DATA_X];
Y = se. values [SensorManager. DATA_Y];
Z = se. values [SensorManager. DATA_Z];
TextView. setText ("x =" + (int) x + "y =" + (int) y + "z =" + (int) z );
}
Public void onAccuracyChanged (Sensor arg0, int arg1 ){
}
};
// Register Listener. SENSOR_DELAY_GAME indicates the detection accuracy,
Sm. registerListener (sel, sensor, SensorManager. SENSOR_DELAY_GAME );
}
Of course, you can also use the SensorEventListener interface,

Sensors cannot be tested in the simulator. The following is the test result of a real machine,




From Hu HU's column

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.