gpt4 book ai didi

java - 保留 Android 进度条或恢复它

转载 作者:行者123 更新时间:2023-12-02 01:02:48 24 4
gpt4 key购买 nike

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

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

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() 中添加这些链接,我假设您可以在类(class)的实际进度中进行调整和应用它

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 中保存多个类(class)进度,我建议您保存进度,例如

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

关于java - 保留 Android 进度条或恢复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60451046/

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