这是旨在使我的Android手机在某些条件为真时振动的代码。它应该振动直到方位角(我从磁力计获得)不在给定范围内。
但无论我将手机指向哪个方向,它都不会振动。它进入if
语句,但从不进入while
语句。并且振动功能正在工作(即手机没有坏)。我的目的是寻找东南西北。
我究竟做错了什么?
public void onCreate(Bundle savedInstanceState) {
// some code
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
// Success! There's a magnetometer.
Toast.makeText(
getApplicationContext(),
"Detecting magnetic field",
Toast.LENGTH_LONG).show();
speakOut("not Working");
}
else {
Toast.makeText(
getApplicationContext(),
"not working",
Toast.LENGTH_LONG).show();
speakOut("not Working");
}
// some code that calls the function
}
some_function(){
if (//some condition) {
while (!(85 < azimuth_angle) && !(azimuth_angle < 95)) {
viberator.vibrate(1000);
//this should happen when it faces east.
}
}
}
public void onSensorChanged(SensorEvent event) {
// TODO: Auto-generated method stub
azimuth_angle = event.values[0];
pitch_angle = event.values[1];
roll_angle = event.values[2];
}