简体   繁体   English

屏幕关闭时,倒计时器无法正常工作

[英]When the screen turns Off, the countdowntimer doesn't work properly

I have implemented a class which extends a CountDownTimer and it works good, but when the screen is turned Off, the timer is desynchronized, so the idea is that, when I get out of the activity or the screen is Off, the CountDownTimer should be continue working properly. 我已经实现了一个扩展CountDownTimer的类,它运行良好,但是当屏幕关闭时,定时器被去同步,所以我的想法是,当我退出活动或屏幕关闭时,CountDownTimer应该是继续正常工作。 I have the permission <uses-permission android:name="android.permission.WAKE_LOCK"/> , also I have tried with PowerManager , but I think the problem is not there. 我有权限<uses-permission android:name="android.permission.WAKE_LOCK"/> ,我也尝试过使用PowerManager ,但我认为问题不在那里。

PowerManager pm = (PowerManager) getSystemService((MenuApp.this).POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
       wl.acquire();
       myTimer =  new CustomTimer(min, 1000); 
       myTimer.start();                    
       wl.release();

.

@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi")
public class CustomTimer extends CountDownTimer
{
    public CustomTimer(long millisInFuture, long countDownInterval) 
    {

        super( millisInFuture, countDownInterval );
        byteTimer = 1;
    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @SuppressLint("NewApi")
    @Override
    public synchronized void onTick(long millisUntilDone) 
    {
   String hms = String.format("%02d:%02d", 
           TimeUnit.MILLISECONDS.toMinutes(millisUntilDone)- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilDone)),
           TimeUnit.MILLISECONDS.toSeconds(millisUntilDone)-TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilDone)));

                 tim.setText(hms);
                 edit.putLong("Minute",  millisUntilDone-1669);
                 edit.commit();

    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @SuppressLint("NewApi")
    @Override
    public synchronized void onFinish() 
    {   
        tim.setText("00:00");
                seconds = 0L;
                if(btnToggle.isChecked()){   
                        btnToggle.setChecked(false);
                        btnToggleTimer.setChecked(false);                   
                    }else{
                        btnToggle.setChecked(true);
                        btnToggleTimer.setChecked(false);
            }         
        }
    }

What do you recommend me to do? 你建议我做什么?

The clunky answer is to do what you tried to do: keep the CPU on all the time. 笨重的答案是做你想做的事情:始终保持CPU开启状态。 You would acquire() the WakeLock before you start the timer and only release() it when you stop the timer. 您可以在启动计时器之前acquire() WakeLock ,并在停止计时器时release()它。 However, users will want to do nasty things to you with pointy sticks if you keep the CPU on all the time during the countdown, as that drains the battery. 但是,如果你在倒计时期间一直保持CPU开启状态,那么用户会想要用刺尖的东西给你做些讨厌的东西,因为这会耗尽电池电量。 It also may be tricky to not screw up and leak the WakeLock , forgetting to release() it in some scenario, causing the CPU to remain on so long as your process continues running. 如果不搞砸并泄漏WakeLock ,忘记在某些情况下release()它也可能很棘手,只要您的进程继续运行,CPU就会保持打开状态。 That might be enough to cause users to come after you with heavier weaponry. 这可能足以让用户用更重的武器来追你。

Hence, don't use CountDownTimer . 因此,请勿使用CountDownTimer Instead, use AlarmManager (to get control when the countdown is over, even if the device is asleep) and something lightweight (eg, a postDelayed() loop) to keep updating the UI while you happen to be in the foreground. 相反,使用AlarmManager (在倒计时结束时控制,即使设备处于睡眠状态)和轻量级(例如, postDelayed()循环),以便在碰巧位于前台时继续更新UI。 This will minimize the battery drain while still giving you control at the right time. 这样可以最大限度地减少电池消耗,同时仍然可以在合适的时间控制您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM