简体   繁体   中英

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. 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.

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. You would acquire() the WakeLock before you start the timer and only release() it when you stop the timer. 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. 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. That might be enough to cause users to come after you with heavier weaponry.

Hence, don't use 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. This will minimize the battery drain while still giving you control at the right time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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