gpt4 book ai didi

android - 设备屏幕锁定时计时器在服务中变慢

转载 作者:太空狗 更新时间:2023-10-29 13:28:09 28 4
gpt4 key购买 nike

我在服务中使用倒数计时器。计时器像 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);



}

}

};

最佳答案

在您的 GuardMeActivity Activity 的 onResume() 方法中写下以下代码并检查结果。

要导入的文件

import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

关于android - 设备屏幕锁定时计时器在服务中变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19419191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com