Android uses sensors to achieve micro-wave function _android

Source: Internet
Author: User
Tags abs stub

This example for you to share the Android micro-wave function of the implementation method for your reference, the specific content as follows

Import java.util.ArrayList;  
Import java.util.List;  
Import Java.util.Random;  
Import android.app.Activity;  
Import Android.app.Service;  
Import android.content.res.Resources;  
Import Android.hardware.Sensor;  
Import android.hardware.SensorEvent;  
Import Android.hardware.SensorEventListener; 
Import Android.hardware.SensorManager;  
Import Android.os.Bundle;  
Import Android.os.Vibrator;  
Import Android.view.View;  
 
Import Android.widget.Button; Need to implement Sensoreventlistener interface public class Loverschoise extends activity implements sensoreventlistener{Butto 
 
    n Clear; 
 
  Define sensor manager private Sensormanager Msensormanager; 
 
     Shaking private vibrator vibrator; 
 
        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
       Setcontentview (r.layout.loverschoise); 
 
        Get sensor Management Services Msensormanager = (Sensormanager) getsystemservice (Sensor_service); Vibration Vibrator = (VibraTor) Getsystemservice (Service.vibrator_service); 
 
        Clear = (Button) Findviewbyid (r.id.clear); 
 
         Clear.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) { TODO auto-generated Method Stub//Click button, set text CLEAR.SETTEXT to button ("Now give the button the Te 
 
    XT Assignment ~ "); 
 
}); 
 
 @Override protected void Onresume () {super.onresume (); Acceleration Sensor Msensormanager.registerlistener (This, msensormanager.getdefaultsensor (sensor.type_accelerometer),/ /There are sensor_delay_ui, Sensor_delay_fastest, Sensor_delay_game and so on,//according to different applications, the need for different reaction rates, specific according to the actual situation set Sensormanager.sensor_ 
 
Delay_normal); 
 
 } @Override protected void OnStop () {Msensormanager.unregisterlistener (this); 
 
Super.onstop (); 
 
 } @Override protected void OnPause () {Msensormanager.unregisterlistener (this); 
 
Super.onpause (); @Override public void onaccuracychanged (Sensor Sensor, int accuracy) {//TODO auto-generated Method Stub//when the sensor's precision changes, the callback is to do nothing. @Override public void onsensorchanged (Sensorevent event) {//TODO auto-generated method stub int Sensorty 
 
 PE = Event.sensor.getType (); 
 
 Values[0]:x axis, values[1]:y axis, values[2]:z axis float[] values = event.values; 
 
 if (SensorType = = Sensor.type_accelerometer) {/* * because normally, the maximum value of any axis is between 9.8~10, only when you suddenly shake the phone *, the instantaneous acceleration will suddenly increase or decrease. * So, after the actual test, just listen to any axis of acceleration greater than 14, change the settings you need * OK ~ ~ ~/if ((Math.Abs (values[0)) >14| | Math.Abs (values[1]) >14| | 
 
  Math.Abs (values[2]) >14) {//Shake mobile phone, set button on the display of the word is empty clear.settext (null); 
 
  Shake mobile phone, and then accompanied by vibration hint ~ ~ vibrator.vibrate (500); 
 } 
 
 } 
 
} 
 
}

Now many applications have been used to shake the phone to change the content of the function, such as micro-letter in the "Shake" function. The first two days to implement this function, a little look at the contents of the sensor sensor.

There are several types of sensors:

Direction sensor: Sensor.type_orientation
Acceleration (Gravity) Sensor: Sensor.type_accelerometer
Light sensor: Sensor.type_light
magnetic field Sensor: Sensor.type_magnetic_field
distance (proximity) sensor: Sensor.type_proximity
Temperature sensor: Sensor.type_temperature

In the function of shaking mobile phone, we only use the acceleration sensor on the line ~ ~

The unit of the acceleration sensor return value is the unit m/s^2 of the acceleration (meter every two seconds), and the values in three directions are

  Values[0]: X-axis direction acceleration
VALUES[1]: Y-axis direction acceleration
VALUES[2]: Z-axis direction acceleration

The definition of X,y,z direction is to position the right leg of the mobile phone as the reference coordinate origin.

 x direction is the mobile phone's horizontal direction, right is positive
The y direction is the horizontal vertical direction of the cell phone, the front is positive
Y direction is the cell phone's vertical direction, the direction of the sky is positive, the direction of the Earth is negative

So, your cell phone is placed in a different space, and it has a different acceleration in three directions. Here, the acceleration in three directions is different from our traditional acceleration (9.8M/S2), which needs to be savored
Above is the code that shakes the phone and clears the TextView text.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.