This example describes Android's ability to create an Android dynamometer using the sensor sensor to refresh the UI in a thread. Share to everyone for your reference, specific as follows:
In the previous article, "Android based on sensor sensors to obtain gravitational induction acceleration" we introduced the basic knowledge of sensor and an example of using the accelerometer to obtain data.
One question was mentioned earlier, that is, the sensor refresh rate is too fast, if we want to do a UI, according to the direction of the data to draw a moving arrow, then it will be too frequent to refresh the drawing interface, occupy a lot of resources, experience will be poor, "Android 2 Advanced Programming" An example of a dynamometer in this case inadvertently gives us a solution for refreshing the UI in this situation, so we know how to prevent the sensor from refreshing too frequently in the interface.
The following is your own modified code, for your reference
* * * @author Octobershiner * SE. HIT * This is an example of "Android 2 advanced Programming," the use of sensors is common, but a good way to use sensors to refresh the UI is to learn * I've added some comments and OnPause methods * An example of refreshing the UI in a demo sensor thread
The application of force-measuring device * * * * Package uni.sensor;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import android.app.Activity;
Import Android.content.Context;
Import Android.hardware.Sensor;
Import android.hardware.SensorEvent;
Import Android.hardware.SensorEventListener;
Import Android.hardware.SensorManager;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Forceometeractivity extends activity{Sensormanager Sensormanager;
TextView Accelerationtextview;
TextView Maxaccelerationtextview;
float currentacceleration = 0;
float maxacceleration = 0; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate (s
Avedinstancestate);
Setcontentview (R.layout.main); Get two text display fields Accelerationtextview = (TextView) Findviewbyid (R. id.acceleration);
Maxaccelerationtextview = (TextView) Findviewbyid (r.id.maxacceleration);
Obtain SENSOR service, select acceleration Sensor Sensormanager = (Sensormanager) getsystemservice (Context.sensor_service);
Sensor accelerometer = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);
Registered Event Sensormanager.registerlistener (Sensoreventlistener, accelerometer, sensormanager.sensor_delay_fastest);
Timer Updatetimer = new Timer ("Gforceupdate");
Updatetimer.scheduleatfixedrate (New TimerTask () {public void run () {Updategui ();
}, 0, 100);
///Add new method, when exiting activity, turn off listener public void OnPause () {Sensormanager.unregisterlistener (Sensoreventlistener);
Super.onpause (); Private final Sensoreventlistener Sensoreventlistener = the value of gravitational acceleration set by the new Sensoreventlistener () {//system, the device is under the condition of horizontal static to withstand this
Pressure, so the acceleration value of the default y-axis direction is standard_gravity double calibration = sensormanager.standard_gravity; public void onaccuracychanged (Sensor Sensor, int accuracy) {} public void ONsensorchanged (Sensorevent event) {Double x = event.values[0];
Double y = event.values[1];
Double z = event.values[2];
Computes three-direction acceleration double A = Math.Round (Math.sqrt (Math.pow (x, 2) + Math.pow (Y, 2) + Math.pow (z, 2));
Eliminate the original gravity-induced pressure currentacceleration = Math.Abs ((float) (a-calibration));
if (Currentacceleration > maxacceleration) maxacceleration = currentacceleration;
}
}; private void Updategui () {/* * Recommended Refresh UI Method * Activity.runonuithread (Runnable) * Update UI in new thread * Run Nable is an interface that requires you to implement the Run method, which is timertask to implement the Run method * */Runonuithread (new Runnable () {public void run ()
{String CURRENTG = currentacceleration/sensormanager.standard_gravity + "Gs";
Accelerationtextview.settext (CURRENTG);
Accelerationtextview.invalidate ();
String maxg = maxacceleration/sensormanager.standard_gravity + "Gs";
Maxaccelerationtextview.settext (MAXG);
Maxaccelerationtextview.invalidate ();
} });
}
}
Thread knowledge and I just as insufficient students, we learn thread together, later will update the relevant learning experience, and share with you
Forgot, there's 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>
I hope this article will help you with the Android program.