我用 a 构建了Android app
一个,如果应用程序正在运行Pedometer
,则工作正常。但是如果我锁定屏幕按钮,计步器停止计数,如果我解锁屏幕,计步器又可以正常工作,即使屏幕被锁定Pedometer
,我怎么能跑步。Pedometer
到目前为止,这是该功能的主要部分:
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;
import de.helper.GeneralHelper;
init();
function init () {
step_el.btn_step_start.addEventListener(MouseEvent.CLICK, function () {
if (Accelerometer.isSupported) {
trace("SUPPORTED");
if (myAcc == null) {
myAcc = new Accelerometer();
myAcc.setRequestedUpdateInterval(10);
}
if (!stepCounterStarted) {
myAcc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate3);
steps = 0;
step_el.step_number.text = steps.toString();
step_el.btn_step_start.title.text = "STOP";
stepCounterStarted = true;
} else {
myAcc.removeEventListener(AccelerometerEvent.UPDATE, onAccUpdate3);
step_el.btn_step_start.title.text = "START";
stepCounterStarted = false;
steps = 0;
step_el.step_number.text = steps.toString();
}
} else {
trace("NOT SUPPORTED");
}
})
}
function onAccUpdate3(evt: AccelerometerEvent): void {
var xx = evt.accelerationX;
var yy = evt.accelerationY;
var zz = evt.accelerationZ;
var dot = (px * xx) + (py * yy) + (pz * zz);
var a = Math.abs(Math.sqrt(px * px + py * py + pz * pz));
var b = Math.abs(Math.sqrt(xx * xx + yy * yy + zz * zz));
dot /= (a * b);
if (dot <= 0.82) {
if (!isSleeping) {
isSleeping = true;
var timerSleep: Timer = new Timer(300, 1);
timerSleep.addEventListener(TimerEvent.TIMER, wakeUp);
timerSleep.start();
steps ++;
saveStep();
if (step_el.step_number != null) step_el.step_number.text = steps;
}
}
px = xx;
py = yy;
pz = zz;
}