I built an Android app
with a Pedometer
, the Pedometer
works fine if the app is running. But if I lock the Screen button the Pedometer stop counting, if I unlock the Screen the Pedometer just works fine again, how can I make the Pedometer
run even if the Screen is locked.
So far this is the main part for that functionality:
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;
}