我已经在我的应用程序中实现了传感器功能。现在 2 分钟后振动处于激活模式,因此设备得到一些运动并且传感器开始工作。我希望传感器只有在用户手动移动设备时才会激活。那里振动对传感器应该没有影响。以下是我的代码。感谢提前...
vib= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
vib.vibrate(val);
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if(deltaX>NOISE){
compareX();
}
else if(deltaY>NOISE){
compareX();
}
else if(deltaZ>NOISE){
compareX();
}
mLastX = x;
mLastY = y;
mLastZ = z;
}
}
private void compareX() {
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
vib.cancel();
stopService(new Intent(getApplicationContext(),AlarmService.class));
System.out.println("Movemenet>><><><><><><>><Occur");
}