android 利用重力感應監聽 來電時翻轉手機後靜音

來源:互聯網
上載者:User

  在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);   }

    
    再在相應位置調用如上方法即可以實現此功能。              

 

 


android 重力感應監聽:

 

public class ShakeListener implements SensorEventListener {     public static ShakeListener sensor1;     // 速度閾值,當搖晃速度達到這值後產生作用     private static final int SPEED_SHRESHOLD = 400;     // 兩次檢測的時間間隔     private static final int UPTATE_INTERVAL_TIME = 70;      // 感應器管理器     private SensorManager sensorManager;     // 感應器     private Sensor sensor;     // 重力感應監聽器     private OnShakeListener onShakeListener;     // 上下文     private static Context context;     // 手機上一個位置時重力感應座標     private float lastX;     private float lastY;     private float lastZ;      // 上次檢測時間     private long lastUpdateTime;  

 
     
 
 

    public static ShakeListener newInstance(Context c) {         if (sensor1 == null) {             sensor1 = new ShakeListener();             context = c;             return sensor1;         } else {             return sensor1;         }     }      // 開始     public void start() {         // 獲得感應器管理器         if(sensorManager==null){             sensorManager = (SensorManager) context             .getSystemService(Context.SENSOR_SERVICE);           }         if (sensorManager != null&&sensor==null) {             // 獲得重力感應器             sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);         }         if (sensor != null) {             sensorManager.registerListener(this, sensor,                     SensorManager.SENSOR_DELAY_NORMAL);         }     }        // 停止檢測     public void stop() {         sensorManager.unregisterListener(this);     }      // 搖晃監聽介面     public interface OnShakeListener {         public void onShake();     }      // 設定重力感應監聽器     public void setOnShakeListener(OnShakeListener listener) {         onShakeListener = listener;     }      // 重力感應器感應獲得變化資料     @Override     public void onSensorChanged(SensorEvent event) {          long currentUpdateTime = System.currentTimeMillis();         // 兩次檢測的時間間隔         long timeInterval = currentUpdateTime - lastUpdateTime;         // 判斷是否達到了檢測時間間隔         if (timeInterval < UPTATE_INTERVAL_TIME) {             return;         }         // 現在的時間變成last時間         lastUpdateTime = currentUpdateTime;          // 獲得x,y,z座標         float x = event.values[0];         float y = event.values[1];         float z = event.values[2];          // 獲得x,y,z的變化值         float deltaX = x - lastX;         float deltaY = y - lastY;         float deltaZ = z - lastZ;          // 將現在的座標變成last座標         lastX = x;         lastY = y;         lastZ = z;          double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ                 * deltaZ)                 / timeInterval * 10000;         // 達到速度閥值,發出提示         if (speed >= SPEED_SHRESHOLD){             // 手機晃動             onShakeListener.onShake();         }     }     public void onAccuracyChanged(Sensor sensor, int accuracy) {      }  }  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.