SoFunction
Updated on 2025-04-06

Android uses Sensor sensor to realize the function of refreshing UI in thread to create an Android dynamometer

This article describes the function of Android using Sensor sensor to create an Android dynamometer in thread refresh UI. Share it for your reference, as follows:

The previous article "Method for obtaining gravity-induced acceleration based on Sensor sensor on Android》We introduce the basic knowledge of sensors and an example of using the acceleration sensor to obtain data.

A problem mentioned earlier is that the sensor refresh frequency is too fast. If we want to make a UI, we need to draw moving arrows one by one according to the direction data, then we need to refresh the drawing interface too frequently, occupy a lot of resources, and have poor experience. An example demonstrating the force tester in "Android 2 Advanced Programming" accidentally provides us with a solution to refresh the UI in this case. Now we know how to prevent the sensor from refreshing too frequently in the interface.

Below is the code you modified for your reference

/*
  * @author octobershiner
  * 2011 07 27
  *
  * This is an example in "Android 2 Advanced Programming". The use of sensors is very common, but it introduces a good way to refresh the UI by using sensors. It is worth learning.
  * I added some comments and onPause method
  * An example of a demonstration sensor refreshing the UI in thread. Application of a Strength Tester
  * */ 
package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
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 
  (savedInstanceState); 
  setContentView(); 
  //Get two text display fields  accelerationTextView = (TextView)findViewById(); 
  maxAccelerationTextView = (TextView)findViewById(); 
  //Get sensor service and select acceleration sensor  sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
  Sensor accelerometer = (Sensor.TYPE_ACCELEROMETER); 
  //Registering Event  (sensorEventListener, 
  accelerometer, 
  SensorManager.SENSOR_DELAY_FASTEST); 
  Timer updateTimer = new Timer("gForceUpdate"); 
  (new TimerTask() { 
  public void run() { 
  updateGUI(); 
  } 
  }, 0, 100); 
 } 
 //Added new method, close the listener when exiting the activity public void onPause(){ 
  (sensorEventListener); 
  (); 
 } 
 private final SensorEventListener sensorEventListener = new SensorEventListener() { 
  //The system sets the standard value of gravity acceleration. The device will bear this pressure when it is stationary horizontally. Therefore, the default acceleration value in the 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 = [0]; 
  double y = [1]; 
  double z = [2]; 
  //Calculate the acceleration in three directions  double a = (((x, 2) + 
  (y, 2) + 
  (z, 2))); 
  //Remove the pressure caused by the original gravity  currentAcceleration = ((float)(a-calibration)); 
  if (currentAcceleration > maxAcceleration) 
  maxAcceleration = currentAcceleration; 
  } 
  }; 
  private void updateGUI() { 
   /*
     * Recommended method to refresh the UI
     * (Runnable)
     * Update the UI in a new thread
     * Runnable is an interface that requires you to implement the run method. The TimerTask above implements this interface and also requires the run method.
     * */ 
   runOnUiThread(new Runnable() { 
   public void run() { 
   String currentG = currentAcceleration/SensorManager.STANDARD_GRAVITY 
   + "Gs"; 
   (currentG); 
   (); 
   String maxG = maxAcceleration/SensorManager.STANDARD_GRAVITY + "Gs"; 
   (maxG); 
   (); 
   } 
   }); 
   } 
}

Students who have the same lack of threading knowledge as me, let’s learn about threading together. We will update our relevant learning experiences in the future and share them with you.

Forgot, there are still files

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:andro 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView android: 
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: 
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 be helpful to everyone's Android programming design.