Use of Android sensors (gravieysensor)

Source: Internet
Author: User

Here is an example of a gravity sensor to illustrate

First step: Create a Sensormanager object to manage or retrieve the sensor

this. Getsystemservice (Context.sensor_service);

The second step: Obtain the corresponding sensor object according to sensor enumeration

Sensor sr = sm.getdefaultsensor (sensor.type_gravity);

The third step, register an event for the sensor, there are many ways to allow Activi to inherit the Sensoreventlistener interface, or to use the inner class directly, it is recommended to use internal classes:

Sm.registerlistener (new  Sensoreventlistener () {            @Override            publicvoid onsensorchanged (Sensorevent event) {            }            @Override            publicvoid int accuracy) {            }        }, Sr, sensormanager.sensor_delay_normal);

Wherein, the third parameter, Sensor_delay_normal represents the sensor reaction speed, here sensor_delay_game,sensor_delay_fast and so on.

Fourth step: Sensorevent objects in the values[] array to save the sensor specific values, for different sensor objects, values[] have different content, the length is not the same. For the gravity sensor, where values have a value of three, representing their gravitational acceleration in the x, Y, z direction, we simply set the layout in the XML file:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_gravity"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.xdwang.myapplication.Gravity"android:orientation= "vertical">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/x"/>    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/y"/>    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/z"/></LinearLayout>

Then set the values for the three Txtview in onsensorchanged:

 Sm.registerlistener (new   Sensoreventlistener () {@Override  public  void   onsensorchanged (Sensorevent event) {Txtx.settext ( "Gravity in X-direction                Acceleration: "+ event.values[0] +" G/MS2 ");                Txty.settext (The gravitational acceleration in the  y direction is: "+ event.values[1] +" G/MS2 ");            Txtz.settext (The gravitational acceleration of the  "Z-direction is:" + event.values[2] + "G/MS2" ); } @Override  public  void onaccuracychanged (sensor sensor, int   

In this way, we can get the data of the gravity sensor in real time, how to use the data, depends on your usefulness.

Finally, enclose all the code:

ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classGravityextendsappcompatactivity {TextView txtx=NULL; TextView txty=NULL; TextView Txtz=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_gravity); Sensormanager SM= (Sensormanager) This. Getsystemservice (Context.sensor_service); Sensor SR=sm.getdefaultsensor (sensor.type_gravity); TXTX=(TextView) Findviewbyid (r.id.x); Txty=(TextView) Findviewbyid (R.ID.Y); Txtz=(TextView) Findviewbyid (R.ID.Z); Sm.registerlistener (NewSensoreventlistener () {@Override Public voidonsensorchanged (Sensorevent event) {Txtx.settext ("X-direction of the gravitational acceleration:" + event.values[0] + "G/MS2"); Txty.settext ("The gravitational acceleration in the y direction is:" + event.values[1] + "G/MS2"); Txtz.settext ("The gravitational acceleration in the z direction is:" + event.values[2] + "G/MS2"); } @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy)    {}}, Sr, Sensormanager.sensor_delay_normal); }}

Use of Android sensors (gravieysensor)

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.