0

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;
}
Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Jaan
  • 251
  • 1
  • 6
  • 16

1 Answers1

0

Per the docs, AIR throttles your app to 4fps when it loses focus to another app.

The Accelerometer asdocs don't mention focus.

Most likely, you'll need to write a native extension to get lower-level access to the accelerometer.

Brian
  • 3,850
  • 3
  • 21
  • 37
  • are you 100% sure that this won't work, since a client asked for it :D – utdev Mar 09 '18 at 11:38
  • Oh, well, since a client asked for it, AIR will definitely start working differently :P In all seriousness, I haven't actually written anything using the Accelerometer classes, but a native extension would probably help. – Brian Mar 09 '18 at 16:23