Advanced two Android Gravity sensor (ii)

Source: Internet
Author: User

It's a long way to go, not just to survive but to love.


This content: Android Gravity sensor


First,Sensormannager sensor Management Objects

1. Get Sensormannager

All the sensors in the phone need to be accessed via Sensormannager, and the Getsystemservice (Sensor_service) method can be used to get the sensor management object of the current phone.

SM = (Sensormanager) getsystemservice (Sensor_service);


2. Obtainsensor target types of values (getsensorlist () method to obtain )

list<sensor> sensors = sm.getsensorlist (sensor.type_temperature);  

3, To achieve the sensor sensors status monitoring function < Span style= "Color:rgb (51,51,51)" >

public void onaccuracychanged (Senso Sensor,int accuracy);  
public void onsensorchanged ( Sensorevent event);  

public void onsensorchanged (Sensorevent e) {x = E.values[sensormanager.data_x];y = E.values[sensormanager.data_y];z = E.VALUES[SENSORMANAGER.DATA_Z];}

4, Registered Sensorlistener
<pre name= "code" class= "Java" >sm.regesterlistener (Sensoreventlistener Listener, sensor sensor, int rate);  
The first parameter: Monitor the Sensor event, the second parameter is the value of the sensor target type, and the third parameter is the precision density of the delay (detection) time. The precision parameters of the delay time are as follows:
parameters
delay time
s Ensormanager.sensor_delay_fastest
0ms
sensormanager.sensor_delay_game
20ms
sensormanager.sensor_delay_ui
60ms
sensormanager.sensor_delay_normal
"/ span>
200ms
since the service of sensor sensors is related to the consumption of battery parameters, and also affects the efficiency of the processing, so the balance between the consumption of battery and processing efficiency, set the sensor detection delay time is an important learning, The appropriate settings need to be made according to the needs of the application system. The hardware detection components of sensor sensing sensors are provided by different vendors. You can use the sensor's Getvendor (), sensor () GetName () and sensor's getveesrion () method to obtain the manufacturer's name, product, and version.

5. Cancellation of registrationSm.unregisterlistener (Sensoreventlistener listener)


Here is a gravity sensing application

Here is the Res/layout/activity_main.xml layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "Com.example.test1.mainactivity$placeholderfragment" >    <textview        android:id= "@ +id/tv "        android:layout_width=" match_parent "        android:layout_height=" wrap_content "        android:textsize= "30DP"/></relativelayout>

here is the Mainactivity.java main interface file:

public class Mainactivity extends Activity {private TextView tv;private sensormanager sm; Sensor sensor;private float x, y, z; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv); sm = ( Sensormanager) Getsystemservice (sensor_service); SENSOR = Sm.getdefaultsensor (Sensor.type_accelerometer); Sensoreventlistener listener = new Sensoreventlistener () {@Overridepublic void onsensorchanged (Sensorevent e) {x = E.valu Es[sensormanager.data_x];y = E.values[sensormanager.data_y];z = E.values[sensormanager.data_z];settitle ("x=" + (int) x + "," + "y=" + (int) y + "," + "z=" + (int) z); Tv.settext ("x=" + (int) x + "," + "y=" + (int) y + "," + "z=" + (int) z) ;} @Overridepublic void onaccuracychanged (Sensor arg0, int arg1) {}};        Sm.registerlistener (listener, sensor, sensormanager.sensor_delay_game); }}



This is where we go, take your time and enjoy it


Advanced two Android Gravity sensor (ii)

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.