Android sensor some phone sensing effects

Source: Internet
Author: User

1.Sensor Type
Gravity sensor/Accelerometer (g-sensor)
Light Sensing (light-sensor)
Temperature sensing
Direction sensing
Magnetic field
Near Sex


2. How to implement sensor programming
A. Get system service (Sensor_service) returns a Sensormanager object
Sensormanager = (Sensormanager) getsystemseriver (Sensor_service);
B. Obtaining an object of the appropriate sensor type through the Sensormanager object
Sensorobject = Sensormanager.getdefaultsensor (sensor Type);
C. Declare a Sensoreventlistener object to listen for the sensor event and overload the Onsensorchanged method
Sensoreventlistener Sensorlistener = new Sensoreventlistener () {
};
D. Register the appropriate Sensorservice
Sensormanager.registerlistener (Sensorlistener, Sensorobject, Sensor TYPE);
E. Destruction of the corresponding Sensorservice
Sensormanager.unregisterlistener (Sensorlistener, sensorobject);

The F:sensorlistener interface is the center of the sensor application. It consists of two required methods:
The onsensorchanged (int sensor,float values[]) method is called when the sensor value changes.
This method is called only for sensors that are monitored by this application (see below for more information). The parameters of the method include: An integer indicating the change of the sensor; a floating-point value array that represents the sensor data itself. Some sensors provide only one data value, while others provide three floating-point values. Both the direction and accelerometer sensors provide three data values.
The onaccuracychanged (int sensor,int accuracy) method is called when the accuracy of the sensor changes. Parameters include two integers: one for the sensor and another for the sensor's new exact value.


3. About G-sensor
The type of Android accelerometer sensor is Sensor.type_accelerometer
Returns the accelerometer value via Android.hardware.SensorEvent.
The unit of the acceleration sensor return value is the unit of acceleration m/s^2 (meters per two seconds), and the values in three directions are
Values[0]: X-axis direction acceleration
VALUES[1]: Y-axis direction acceleration
VALUES[2]: Z-axis direction acceleration
The x, y, z direction is defined as the origin of the reference frame at the right bottom of the cell phone that is placed horizontally.

The x direction is the horizontal direction of the phone, right
The y direction is the horizontal vertical direction of the cell phone, the front is positive
The y direction is the cell phone vertical direction, the direction of the sky is positive, the direction of the Earth is negative
It should be noted that due to the Earth's intrinsic gravitational acceleration g (value 9.8 m/s^2),
So the actual acceleration value in reality should be the Z-direction return value-9.8 m/s^2.
For example, if you throw your phone up with a 2 m/s^2 acceleration, the return value in the z direction should be 11.8 m/s^2.
Conversely, if the mobile phone with 2 m/s^2 acceleration Falls, the z-direction of the return value should be 7.8 m/s^2.
The x, y direction does not have the above limitation.


I wrote myself a similar shake effect, the code is as follows,

Import Android.app.activity;import Android.app.service;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.sensormanager;import Android.os.Bundle;import Android.os.vibrator;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.textview;public class Sensor extends Activity implements Sensoreventlistener{private TextView TV =null;private button button = null;private sensormanager sensormanager =null; Private Vibrator Vibrator = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_sensor); TV = (TextView) Findviewbyid (r.id.textview1); button = (Button) Findviewbyid (R.id.button1), Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick ( View v) {//TODO auto-generated method Stubtv.settext ("Text Recovery");}); Sensormanager = (Sensormanager) getsystemservice (sensor_SERVICE); vibrator = (Vibrator) getsystemservice (Service.vibrator_service);} @Overrideprotected void Onresume () {super.onresume (); Sensormanager.registerlistener (This, Sensormanager.getdefaultsensor (Android.hardware.Sensor.TYPE_ACCELEROMETER), sensormanager.sensor_delay_normal);} @Overrideprotected void OnPause () {super.onpause (); Sensormanager.unregisterlistener (this);}          @Overridepublic void Onsensorchanged (Sensorevent event) {int sensortype = Event.sensor.getType ();          Values[0]:x axis, values[1]:y axis, values[2]:z axis float[] values = event.values; if (SensorType = = Android.hardware.Sensor.TYPE_ACCELEROMETER) {if (Math.Abs (values[0]) >17| | Math.Abs (values[1]) >17| |                  Math.Abs (values[2]) >17) {LOG.D ("sensor", "============ values[0] =" + values[0]);                  LOG.D ("sensor", "============ values[1] =" + values[1]);                  LOG.D ("sensor", "============ values[2] =" + values[2]); Tv.settext ("Shake a Shake"+ "\ n" +values[0]+ "\ n" +values[1]+ "\ n" +values[2]);              Shake the phone, and then accompanied by vibration tips ~ ~ vibrator.vibrate (500);  }}} @Overridepublic void Onaccuracychanged (android.hardware.Sensor Sensor, int accuracy) {//TODO auto-generated Method stub}}




Android sensor some phone sensing effects

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.