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