2

I am developing a Phonegap application which, among other things, has a step counter (which uses the accelerometer). Obviously, i need the app to count the steps even if the screen is locked. I have used a background service, acquired partial wake lock, but for some reason the service is still killed when i lock the screen. This is the code for the background service:

public class StepCounterService extends Service {

private ELStepCounter mStepCounter;

public static final String TAG = "STEP_COUNTER_SERVICE";


private void publishResult(int steps){
    Intent intent = new Intent(TAG);
    intent.putExtra("Steps", steps);
    sendBroadcast(intent);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    super.onStartCommand(intent, flags, startId);
    ELStepCounter.startInstance(StepCounterService.this);
    mStepCounter = ELStepCounter.getsInstance();
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "StepCounterService");
    wakeLock.acquire();
    mStepCounter.setmStepDetectionListener(new StepCounterInterface() {

        @Override
        public void onStepDetected(final long steps) {
            // TODO Auto-generated method stub
            Toast.makeText(StepCounterService.this, steps + " steps", Toast.LENGTH_SHORT).show();
            publishResult((int) steps);

        }

        @Override
        public void error(int errorCode) {
            // TODO Auto-generated method stub

        }
    });
    return Service.START_STICKY;
}

}

Thanks in advance

1 Answers1

0

I appreciate this answer comes very late. I am sending it just in case it is useful for future reference. I am not an expert on PhoneGap, however I have some experience with Android phones and I guess you may have been testing with one of them. In several Android phones the accelerometer is turned off when the screen is turned off. Not all of them. See a list of models at http://www.saltwebsites.com/2012/android-accelerometers-screen-off I have asked a question on StackOverflow about that because it looks like Google Fit is kind of overcoming the limitation but I found no answer (see Step Counter in Android: always on?) I hope it helps

Community
  • 1
  • 1
FabioC
  • 462
  • 5
  • 14