для наземного обслуживания замедление работы при выключенном экране

#android #timer #android-service #screen #android-handler

Вопрос:

я пытаюсь запустить секундомер в службе forground, используя класс обработчика. мои счетчики работают как 1,2,3… все работает нормально, даже когда приложение закрыто, но когда я выключаю экран (блокирую устройство), таймер замедляется и работает неправильно, но все еще работает. как мне это сделать?

вот мой класс обслуживания: ` private void runTimer() { конечный обработчик обработчика = новый обработчик();

     handler.post(new Runnable() {
        @Override
        public void run() {

                int i = Shared.preferences.getInt(records.getTitle(), -1)   1;
                Shared.editor.putInt(records.getTitle(), i);
                Shared.editor.apply();


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                String offerChannelName = "Service Channel";
                String offerChannelDescription = "LoggerChannel";
                int offerChannelImportance = NotificationManager.IMPORTANCE_DEFAULT;

                NotificationChannel notifChannel = new NotificationChannel(CHANNEL_ID, offerChannelName, offerChannelImportance);
                notifChannel.setDescription(offerChannelDescription);
                notifManager = getSystemService(NotificationManager.class);
                notifManager.createNotificationChannel(notifChannel);
            }

            int hours = seconds / 3600;
            int minutes = (seconds % 3600) / 60;
            int secs = seconds % 60;

            String Time = String.format(Locale.getDefault(), "%d:d:d", hours, minutes, secs);


            NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_baseline_timer_24)
                    .setContentTitle(records.getTitle())
                    .setContentText(Time)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bamboo));

            Notification servNotification = sNotifBuilder.build();
            startForeground(1, servNotification);


            if (running) {
                seconds  ;


                handler.postDelayed(this, 1000);
                records.setStringTime(Time);
            } else {
                name = Shared.preferences.getInt(records.getTitle(), -1);
                seconds = 0;
            }
 

`