【发布时间】:2015-03-02 13:38:32
【问题描述】:
我正在开发一个 Phonegap 应用程序,其中包括一个计步器(它使用加速度计)。显然,即使屏幕被锁定,我也需要应用程序来计算步数。我使用了后台服务,获得了部分唤醒锁定,但由于某种原因,当我锁定屏幕时该服务仍然被终止。这是后台服务的代码:
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;
}
}
提前致谢
【问题讨论】:
标签: android cordova accelerometer background-process