gpt4 book ai didi

android - 延迟效果很好;稍作修改,没有延迟——为什么?

转载 作者:行者123 更新时间:2023-11-29 20:34:40 25 4
gpt4 key购买 nike

此代码包含在 QuizFragment.java 中,延迟 ms 毫秒,然后执行 loadNextFlag。工作正常。 (毫秒 = 2000 毫秒)

public void pauseAndThenShowNextFlag(long ms){
showTimer("pauseAndThenShowNextFlag");
pauseTimer(); // 7/17/15--DAS
handler.postDelayed
(
new Runnable() {
@Override
public void run() {
loadNextFlag();
}
}, ms // ms-second delay
);
startTimer();
}

我将延迟更改为在另一个对象中,所以上面变成了:

public void pauseAndThenShowNextFlag(long ms){
showTimer("pauseAndThenShowNextFlag");
Timer.xhaltTimer4(ms);
loadNextFlag();
startTimer();
}

这是 Timer.java 中的 xhaltTimer4:

public static void xhaltTimer4(long ms){
customHandler.postDelayed
(
new Runnable() {
@Override
public void run() {
pauseTimer();
}
}, ms); // 2000 milliseconds for 2-second delay
}

绝对不会出现延迟。是不是因为 loadNextFlag 包含的代码比 pauseTimer 多很多?为什么会有所不同?根据 Runnable run 方法必须执行多少操作才能启用延迟,划定的界线在哪里?

我尝试了修改后的 xhaltTimer4——仍然没有延迟:

public static void xhaltTimer4(long ms){
Handler h = new Handler();
h.postDelayed
(
new Runnable() {
@Override
public void run() {
pauseTimer();
}
}, ms); // ms-millisecond delay
}

这是pauseTimer:

public static void pauseTimer(){
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}

这是loadNextFlag:

public void loadNextFlag() {
startTimer();
String timerDisplayString;
oneImageView.setVisibility(View.INVISIBLE);
showTimer("loadNextFlag");
currentTimeSpent = timeStringToMillis(Timer.getTimerString());// was prevTimeSpent--7/17/15--DAS
InputStream stream ;
// get file name of the next flag and remove it from the list
consecutiveWrong = 0;

String nextImage = quizFlagPathList.remove(0);
correctAnswer = nextImage; // update the correct answer
answerImageView.setVisibility(View.INVISIBLE);
ableButtons(true);

// display current question number--2nd and 3rd parameters are INPUT into the xml statement
questionNumberTextView.setText(getResources().getString(R.string.question,
(correctAnswers + 1),
FLAGS_IN_QUIZ));
// extract the region from the next image's name
String region = nextImage.substring(0, nextImage.indexOf('-'));

// use AssetManager to load next image from assets folder
AssetManager assets = getActivity().getAssets();

try{
stream = assets.open(region + "/" + nextImage + ".png");

Drawable flag = Drawable.createFromStream(stream, nextImage);
flagImageView.setImageDrawable(flag);
}catch (IOException exception){Log.w(TAG, "Error loading " + nextImage, exception);}

Collections.shuffle(flagPathList); // shuffle file names

// put the correct answer at the end of flagPathList
int correct = flagPathList.indexOf(correctAnswer);

flagPathList.add(flagPathList.remove(correct));

int k = 0;
countriesToUse.clear();
for(int row = 0; row < guessRows; row++)
for (int col = 0; col < guessLinearLayouts[row].getChildCount(); col++)
countriesToUse.add(getCountryName(flagPathList.get(k++)));

countriesToUse.set(countriesToUse.size() - 1, getCountryName(flagPathList.get(flagPathList.size() - 1)));

if(MainActivity.getAlphabetize()) Collections.sort (countriesToUse);
else Collections.shuffle(countriesToUse);

k = 0;
// add 3, 6, or 9 guess Buttons based on the value of guessRows
for (int row = 0; row < guessRows; row++) {
// place Buttons in currentTableRow
for (int column = 0; column < guessLinearLayouts[row].getChildCount(); column++) {
// get reference to Button to configure
Button newGuessButton = (Button) guessLinearLayouts[row].getChildAt(column);
newGuessButton.setEnabled(true);
// get country name and set it as newGuessButton's text
String fileName = countriesToUse.get(k++);
newGuessButton.setText((fileName));
}
}
} // end method loadNextFlag

最佳答案

这是因为xhaltTimer4是同步方法调用,不会等待ms。在这里面你发布了一个 Runnable,它将在一些延迟后运行。您应该尝试将 loadNextFlag() 移动到 runnable 的 run() 方法中,如下所示

public static void xhaltTimer4(long ms){
Handler h = new Handler();
h.postDelayed
(
new Runnable() {
@Override
public void run() {
pauseTimer();
loadNextFlag()
}
}, ms); // ms-millisecond delay
}

关于android - 延迟效果很好;稍作修改,没有延迟——为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493185/

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