保留 android 个进度条或恢复它

Keep android progress bar or resume it

我想知道如何保持进度条的状态或从中断处恢复。让我解释一下,它是一个带进度条的计时器,当用户关闭 activity 或关闭 activity 时,计时器作为后台服务启动(计时器正常工作),我希望这样当用户关闭 activity 或关闭应用程序时,当他 returns 时,进度条将恢复到原来的位置。现在发生的事情是当它 returns 到计时器时,进度条 returns 到零。这是我的适配器代码,课程和计时器服务在其中发生

这是我保存进度条状态的方式

private static SharedPreferences.Editor pref;
private static int saveProgressBarState;
private static SharedPreferences getProgressBarState;

private CountDownTimer countDown(final long time, final ProgressBar lessonProgress, long originalTimerSize) {

        final int timerLengthInSecond = (int) time / 1_000;

        lessonProgress.setMax((int) originalTimerSize / 1_000);

        return new CountDownTimer(time, 1000) {
            @Override
            public void onTick(long l) {

                long secondRemaining = l / 1_000;

                lessonProgress.setProgress((int) (timerLengthInSecond - secondRemaining));

                //Getting progress of progress bar
                saveProgressBarState = lessonProgress.getProgress();

            }

            @Override
            public void onFinish() {

                lessonProgress.setProgress(timerLengthInSecond);
            }
        };

    }

//this method is called when onPause() is called on the activity
public static void runLessonTimerInBackground(Context context) {

        pref = context.getSharedPreferences("LESSONPROGRESS", MODE_PRIVATE).edit();
        pref.putInt("savedprogress", progressBarStateSave);
        pref.apply();

    }

我想保存进度条状态并在原处恢复进度并继续进度

您需要将这些链接添加到您的 onResume() 我假设您可以调整并将其应用到 lesson

的实际进度中
int lastProgress = pref.getInt("savedprogress", 0); //0 is default value

lessonProgress.setProgress(lastProgress); //this will set the progress

//here you might also need to do some other stuff related to your code and req
//you can also resume your lesson video from here, etc.

如果你想在sharePref中保存多个课程进度,我建议你保存进度像

String progressPrefName = "savedProgress_" + lessonID; //here lessonID is lesson's unique ID
pref.putInt(progressPrefName, progressBarStateSave); // here pass the name which can be "savedProgress_1" referring to lesson with ID 1

然后你就可以像

String progressPrefName = "savedProgress_" + lessonID; 

int lastProgress = pref.getInt(progressPrefName, 0); //0 is default value

lessonProgress.setProgress(lastProgress); //this will set the progress