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