我在服務中使用倒數計時器。計時器繼續像01,02,03,....一切正常。但是當設備屏幕鎖定計時器變慢但仍然運行。我使用PowerManager但不工作.PowerManager只在USB電纜連接到PC時工作,但是當我從PC定時器移除設備時速度變慢。我不知道爲什麼會出現這個問題。請幫助我解決這個問題。謝謝advace.Below是我的代碼。當設備屏幕鎖定時,計時器在服務中速度變慢
@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);
}
}
};
你給'<使用許可權的android:NAME = 「android.permission.WAKE_LOCK」/>'許可清單中? – Vigbyor
是的,我已經給了這個權限。PowerManager只適用於連接到PC的USB電纜,但是當我從PC定時器移除設備時速度變慢。 –