[Based Android] Android sensor introduction (2) refresh the UI in the thread to create an android Dynamometer

Source: Internet
Author: User

PreviousArticleBytes.

One problem we mentioned is that the sensor refresh frequency is too fast. If we need to draw a moving arrow Based on the direction data in a UI, We need to refresh the drawing interface too frequently, it consumes a lot of resources and has poor experience. In Android 2 advanced programming, an example of the provisioning is provided, but a solution for refreshing the UI is provided, now we know how to prevent the sensor from refreshing too frequently on the interface.

The following is a self-modifiedCodeFor your reference

 1   /*   
2 * @ Author octobershiner
3 * 2011 07 27
4 * Se. Hit
5 * This is an example of Android 2 advanced programming. It is common to use sensors. However, it is worth learning how to use sensor applications to refresh the UI.
6 * I added some comments and the onpause method.
7 * An example of how to refresh the UI in a sensor thread: application of a Power Tester
8 * */
9
10 Package Uni. sensor;
11
12 Import Java. util. timer;
13 Import Java. util. timertask;
14
15 Import Android. App. activity;
16 Import Android. content. context;
17 Import Android. Hardware. sensor;
18 Import Android. Hardware. sensorevent;
19 Import Android. Hardware. sensoreventlistener;
20 Import Android. Hardware. sensormanager;
21 Import Android. OS. Bundle;
22 Import Android. widget. textview;
23
24 Public Class Forceometeractivity Extends Activity {
25
26 Sensormanager;
27 Textview accelerationtextview;
28 Textview maxaccelerationtextview;
29
30 Float Currentacceleration = 0;
31 Float Maxacceleration = 0;
32
33 @ Override
34 Protected Void Oncreate (bundle savedinstancestate ){
35 // Todo auto-generated method stub
36 Super . Oncreate (savedinstancestate );
37 Setcontentview (R. layout. Main );
38 // Get two text display fields
39 Accelerationtextview = (textview) findviewbyid (R. Id. Acceleration );
40 Maxaccelerationtextview = (textview) findviewbyid (R. Id. maxacceleration );
41 // Obtain the sensor service and select an acceleration sensor.
42 Sensormanager = (sensormanager) getsystemservice (context. sensor_service );
43 Sensor accelerometer = sensormanager. getdefasensensor (sensor. type_accelerometer );
44 // Register an event
45 Sensormanager. registerlistener (sensoreventlistener,
46 Accelerometer,
47 Sensormanager. sensor_delay_fastest );
48
49 Timer updatetimer = New Timer ("gforceupdate ");
50 Updatetimer. scheduleatfixedrate ( New Timertask (){
51 Public Void Run (){
52 Updategui ();
53 }
54 },0,100 );
55
56
57 }
58
59 // The listener is closed when you exit the activity.
60 Public Void Onpause (){
61 Sensormanager. unregisterlistener (sensoreventlistener );
62 Super . Onpause ();
63 }
64
65
66 Private Final Sensoreventlistener = New Sensoreventlistener (){
67 // The gravity acceleration standard value set by the system. The device is under this pressure when it is horizontally static, so the acceleration value in the Y axis is standard_gravity by default.
68 Double Calibration = sensormanager. standard_gravity;
69
70 Public Void Onaccuracychanged (sensor, Int Accuracy ){}
71
72 Public Void Onsensorchanged (sensorevent event ){
73 Double X = event. Values [0];
74 Double Y = event. Values [1];
75 Double Z = event. Values [2];
76
77 // Calculate the acceleration in three directions
78 Double A = math. Round (math. SQRT (math. Pow (x, 2) +
79 Math. Pow (Y, 2) +
80 Math. Pow (z, 2 )));
81
82 // Eliminate pressure caused by original gravity
83 Currentacceleration = math. Abs (( Float ) (A-calibration ));
84 If (Currentacceleration> maxacceleration)
85 Maxacceleration = currentacceleration;
86 }
87 };
88
89 Private Void Updategui (){
90 /*
91 * A recommended method for refreshing the UI
92 * Activity. runonuithread (runnable)
93 * Update the UI in a new thread.
94 * Runnable is an interface that requires you to implement the run method. The above timertask implements this interface and also requires the run method.
95 * */
96 Runonuithread ( New Runnable (){
97 Public Void Run (){
98 String currentg = currentacceleration/sensormanager. standard_gravity
99 + "GS ";
100 Accelerationtextview. settext (currentg );
101 Accelerationtextview. invalidate ();
102 String maxg = maxacceleration/sensormanager. standard_gravity + "gS ";
103 Maxaccelerationtextview. settext (maxg );
104 Maxaccelerationtextview. invalidate ();
105 }
106 });
107
108 }
109
110
111 }

If the thread knowledge is as insufficient as I do, let's study the thread again. We will update the learning experience in the future and share it with you.

Forgot, and the main. xml file.

 <?  XML version = "1.0" encoding = "UTF-8"  ?>   
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation = "Vertical"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent" >
< Textview Android: ID = "@ + ID/acceleration"
Android: gravity = "Center"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: textstyle = "Bold"
Android: textsize = "32sp"
Android: Text = "Center"
Android: editable = "False"
Android: singleline = "True"
Android: layout_margin = "10px" />

< Textview Android: ID = "@ + ID/maxacceleration"
Android: gravity = "Center"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: textstyle = "Bold"
Android: textsize = "40sp"
Android: Text = "Center"
Android: editable = "False"
Android: singleline = "True"
Android: layout_margin = "10px" />

</ Linearlayout >

Related Article

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.