Android USES Sensor sensors to refresh UI and create android dynamometer in a thread

  • 2020-11-25 07:31:52
  • OfStack

This article shows how Android USES Sensor sensors to refresh UI and create android dynamometer in a thread. To share for your reference, the details are as follows:

In the previous article, "Android's Method for Obtaining Accelerometer based on Sensor Sensor", we introduced the basic knowledge of sensor and an example of using the accelerometer to obtain data.

Mentioned a problem, that is, sensor refresh rate is too fast, if we want to make a UI, need according to the direction of data 1 a moving arrow, then too frequent refresh the map interface, takes up a lot of resources, experience will also is very poor, the android 2 advanced programming, a demonstration example of force-measuring device, but inadvertently provide us with a kind of refresh UI solution for this case, we will know how to prevent the sensor too frequent refresh in the interface.

Below is the code that oneself change, for everybody reference


/* 
 * @author octobershiner 
 * 2011 07 27 
 * SE.HIT 
 *  This is the Android 2  Advanced Programming 1 For example, the use of sensors is very common, but it is introduced 1 How to refresh an application that USES sensors UI A good way to learn  
 *  I added 1 Some comments and onPause methods  
 * 1 The demo sensor is refreshed in the thread UI An example of   Application of dynamometer  
 * */ 
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(savedInstanceState); 
  setContentView(R.layout.main); 
  // Gets two text display fields  
  accelerationTextView = (TextView)findViewById(R.id.acceleration); 
  maxAccelerationTextView = (TextView)findViewById(R.id.maxAcceleration); 
  // To obtain sensor Service, select accelerometer  
  sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
  Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
  // Register 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, exit activity ", close the listener  
 public void onPause(){ 
  sensorManager.unregisterListener(sensorEventListener); 
  super.onPause(); 
 } 
 private final SensorEventListener sensorEventListener = new SensorEventListener() { 
  // The standard value of gravity acceleration set by the system, the device will bear this pressure in the case of horizontal rest, so default Y The acceleration in the axial direction is zero 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]; 
  // To calculate 3 Acceleration in four directions  
  double a = Math.round(Math.sqrt(Math.pow(x, 2) + 
  Math.pow(y, 2) + 
  Math.pow(z, 2))); 
  // The original gravitational pressure is eliminated  
  currentAcceleration = Math.abs((float)(a-calibration)); 
  if (currentAcceleration > maxAcceleration) 
  maxAcceleration = currentAcceleration; 
  } 
  }; 
  private void updateGUI() { 
   /* 
    *  The recommended 1 A refresh UI The method of  
    * Activity.runOnUiThread ( Runnable )  
    *  Update in a new thread UI 
    * Runnable is 1 An interface that you need to implement run Method, above TimerTask Just to implement this interface also needs to be implemented run methods  
    * */ 
   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(); 
   } 
   }); 
   } 
}

For those of you who are not good at thread knowledge, let's learn threads from now on. We will update relevant learning experience and share it with you

Forget 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> 

I hope this article has been helpful in Android programming.


Related articles: