Android uses gravity sensing to listen for phone calls, flip the phone, and then mute

Source: Internet
Author: User

Add the following code to CallNotifier. java:

 

public void GetSensorManager(Context context) {         sm = (SensorManager) context                 .getSystemService(Service.SENSOR_SERVICE);         sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);         mySensorListener = new SensorEventListener() {             @Override             public void onSensorChanged(SensorEvent event) {                 x = event.values[0];                  y = event.values[1];                  z = event.values[2];                  if (x < 1 && x > -1 && y < 1 && y > -1) {                                      if (z > 0) {                         mGoUp = true;                                              } else {                                                  mGoUp = false;                     }                                      } else { //                  if (x > 1 || x < -1 || y > 1 || y < -1 ) {                                                   if ( z > 0 && !mGoUp ) {                             mRinger.stopRing();                             if(mySensorListener != null){                                 sm.unregisterListener(mySensorListener);    //Add by kylin 2013.07.25                                                               }                         }                          if ( z < 0 && mGoUp ) {                             mRinger.stopRing();                             if(mySensorListener != null){                                 sm.unregisterListener(mySensorListener);    //Add by kylin 2013.07.25                              }                         }    //                  }                                        }                              }               @Override             public void onAccuracyChanged(Sensor sensor, int accuracy) {                 // TODO Auto-generated method stub               }         };         sm.registerListener(mySensorListener, sensor,                 SensorManager.SENSOR_DELAY_GAME);              }  public void GetSensorManager(Context context) {  sm = (SensorManager) context    .getSystemService(Service.SENSOR_SERVICE);  sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  mySensorListener = new SensorEventListener() {   @Override   public void onSensorChanged(SensorEvent event) {    x = event.values[0];    y = event.values[1];    z = event.values[2];    if (x < 1 && x > -1 && y < 1 && y > -1) {         if (z > 0) {      mGoUp = true;           } else {            mGoUp = false;     }         } else {//     if (x > 1 || x < -1 || y > 1 || y < -1 ) {            if ( z > 0 && !mGoUp ) {       mRinger.stopRing();       if(mySensorListener != null){        sm.unregisterListener(mySensorListener);    //Add by kylin 2013.07.25               }      }      if ( z < 0 && mGoUp ) {       mRinger.stopRing();       if(mySensorListener != null){        sm.unregisterListener(mySensorListener);    //Add by kylin 2013.07.25       }      }//     }         }       }   @Override   public void onAccuracyChanged(Sensor sensor, int accuracy) {    // TODO Auto-generated method stub   }  };  sm.registerListener(mySensorListener, sensor,    SensorManager.SENSOR_DELAY_GAME);   }


You can call the above method at the corresponding position to implement this function.

 

 


Android gravity sensor monitoring:

 

Public class ShakeListener implements SensorEventListener {public static ShakeListener sensor1; // speed threshold value. When the shaking speed reaches this value, private static final int SPEED_SHRESHOLD = 400 is generated; // two detection intervals: private static final int UPTATE_INTERVAL_TIME = 70; // Sensor manager private SensorManager sensorManager; // sensor private Sensor sensor; // gravity Sensor listener private OnShakeListener onShakeListener; // Context private static context Context; // gravity sensing coordinate private float lastX; private float lastY; private float lati; // private long lastUpdateTime;

 

 
 

Public static ShakeListener newInstance (Context c) {if (sensor1 = null) {sensor1 = new ShakeListener (); context = c; return sensor1;} else {return sensor1 ;}} // start public void start () {// obtain the sensor manager if (sensorManager = null) {sensorManager = (SensorManager) context. getSystemService (Context. SENSOR_SERVICE);} if (sensorManager! = Null & sensor = null) {// obtain the gravity sensor Sensor = sensorManager. getdefasensensor (sensor. TYPE_ACCELEROMETER);} if (sensor! = Null) {sensorManager. registerListener (this, sensor, SensorManager. SENSOR_DELAY_NORMAL) ;}}// stop public void stop () {sensorManager. unregisterListener (this);} // shake listener interface public interface OnShakeListener {public void onShake ();} // set the gravity listener public void setOnShakeListener (OnShakeListener listener) {onShakeListener = listener;} // The gravity sensor senses the changed data @ Override public void onSensorChanged (SensorEvent event) {long currentUpdateTime = System. currentTimeMillis (); // interval between the two checks long timeInterval = currentUpdateTime-lastUpdateTime; // determine whether the detection interval has been reached if (timeInterval <UPTATE_INTERVAL_TIME) {return ;} // The current time is changed to lastUpdateTime = currentUpdateTime; // obtain the x, y, and zcoordinate float x = event. values [0]; float y = event. values [1]; float z = event. values [2]; // obtain the changing values of x, y, and z, float deltaX = x-lastX; float deltaY = y-lastY; float deltaZ = z-lastZ; // convert the current coordinate to the last coordinate lastX = x; lastY = y; lati = z; double speed = Math. sqrt (deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ)/timeInterval * 10000; // if (speed> = SPEED_SHRESHOLD) {// shake onShakeListener on the mobile phone. onShake () ;}} public void onAccuracyChanged (Sensor sensor, int accuracy ){}}

 

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.