我在服务中使用倒数计时器。计时器像 01,02,03,.... 一样运行。一切正常。但是当设备屏幕锁定时,计时器变慢但仍在运行。我正在使用 PowerManager 但不工作。PowerManager 仅在 USB 电缆连接到 PC 时有效,但当我从 PC 中移除设备时,计时器变慢。我不知道为什么会出现这个问题。请帮我解决这个问题。谢谢。下面是我的代码。
@Override
public void onCreate(){
super.onCreate();
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
wl.acquire();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
flagVibrate=intent.getBooleanExtra("FlagVibrate",false);
if(mStartTime == 0L){
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mUpdateTimeTask);
stopSelf();
wl.release();
}
private Runnable mUpdateTimeTask = new Runnable(){
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis()- start;
int seconds = (int) (millis / 1000);
int stopTime = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
GuardMeActivity.timerView.setText("" + minutes + ":"
+ String.format("%02d", seconds));
timerStop1 = minutes + ":"
+ String.format("%02d", seconds);
}
}
};