Android--seneor (get data value flow and monitor multiple sensor simultaneously)
Source: Internet
Author: User
<span id="Label3"></p><p><p>Recently contacted with Android sensor programming, Android provides sensors, Sensormanager and Senseoreventmanager to obtain sensor data and take some appropriate Action.</p></p><p><p><br></p></p><p><p>First step: get the sensor values</p></p><p><p>1, first through the service to obtain Sensormanager: (getsystemservice) This.getsystemservice (context.sensor_service). Note This is the this of the main activity, and if it is called elsewhere, the main activity needs to be passed.</p></p><p><p>2. (optional) You can get a list of the specific sensor and all of the Sensor's on the phone via sensormanager. Can produce the information about the sensor such as manufacturers, models and so On.</p></p><p><p>3, through the Sensormanager register monitoring Events. <span style="font-weight:bold">boolean Registerlistener (</span>sensoreventlistener<span style="font-weight:bold"> listener, </span>sensor<span style="font-weight:bold"> sensor, </span> <span style="font-weight:bold">int rate). </span>you can write a class to implement the Sensoreventlistener interface, you can let the main activity implement its interface, you can also new a sensoreventlistener. The second one is for which sensor is registered for the Listener. The third one is the sampling delay (subtlety).</p></p><p><p>4, realize the Sensoreventlistener in two methods. public void onsensorchanged (sensorevent event), public void onaccuracychanged (sensor sensor, int accuracy);</p></p><p><p>The current value of the sensor can be obtained by the event of the first Method.</p></p><p><p>Step two: Display the values on the interface</p></p><p><p>I used to write a class (mysensoreventlistener) to implement the listener Interface.</p></p><p><p>In a Non-ui thread to control the ui, the handler is Used. The context of the main activity and the interface view (or the control directly displayed) need to be passed in when initializing the Mysensoreventlistener. In Mysensoreventlistener classes, you need to use the new Handler (context.getmainlooper ()) when initializing Handler. You can then use handler to send messages after you have obtained the Data.</p></p><p><p>The third step: realize the service end control start and end the data collection. At the same time, the collected data needs to be uploaded to the Server.</p></p><p><p>Use sockets for Communication. (see Java--socket Communication)</p></p><p><p><br></p></p><p><p>Note: when you need to listen to multiple sensor simultaneously, <span style="color:#ff0000">only one sensormanager is required</span>. Then initialize multiple mysensoreventlistener, register with different mysensoreventlistener and specify different Sensor. At the same time, the public <span style="color:#ff0000">void onsensorchanged (sensorevent Event) in Mysensoreventlistener uses Event.sensor.getType () to determine</span> Which sensor data is currently in order to make a different operation.</p></p><p><p></p></p><p><p>Android--seneor (get data value flow and monitor multiple sensor simultaneously)</p></p></span>
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