首页 > 解决方案 > 在不停止 UI 线程的情况下向 android 中的循环添加延迟

问题描述

我一直在查看 Android Studio Docs 和 StackOverflow,但我似乎无法找到一种方法来做与 IOS 中所做的等效的操作: 在 Android 中添加延迟而不阻塞 UI

我尝试使用处理程序,但没有运行以下代码:

// Iteration 1
// Delay of 500ms
// Iteration 2
// Delay of 500ms
// ...

代码似乎运行如下:

// Iteration 1
// Iteration 2
// Delay of 500ms
// next state

我使用的代码结构如下:

Handler myHandler = new Handler();
while (someCondition) {
    myHandler.postDelayed(new Runnable() {
        public void run() {
            myFunction();
        }
    }, 500);
}

我在运行此 Activity 时看到的行为是跳过它,并在 500 毫秒后转到下一个 Activity(具有预期结果),但没有向用户显示正在发生的事情。

我将如何延迟循环以便用户可以看到发生了什么?


澄清:

当前状态(在 moveCard() 之后)需要显示 x ms,然后再通过我的 while 循环的逻辑。

这样做直到达到最终状态。

然后开始下一个Activity。


public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.horserace_game);

    this.cardDeck = findViewById(R.id.deck);
    this.game_ended = false;

    this.deck = new Deck();
    this.aces = new ArrayList<>();
    this.acesPos = new ArrayList<>();
    this.hidden = new ArrayList<>();

    // Remove all Aces from deck and put them in aces
    //resetDeck creates a deck ordered on Face (so Ace SDHS, Two SDHS etc, which is handy for this purpose.
    this.deck.resetDeck(1);
    for (int i = 0; i < 4; i++) {
        this.aces.add(this.deck.removeCard(0));
        // Add a new pos for card
        this.acesPos.add(0);
    }

    // Shuffle the deck
    this.deck.shuffleDeck();

    // Get 7 cards from the top of the deck to put on the
    // side of the track and place them face down on the screen
    for (int i = 0; i < 7; i++) {
        this.hidden.add(this.deck.removeCard(0));
    }


    // Whilst the game is still running, pick a card from the deck and move the ace with the same suit
    while (!this.game_ended) {
        // Pick the next card from the deck
        this.next = this.deck.removeCard(0);
        cardDeck.setImageResource(getCardImage(next));
        // Find the ace with the same suit as this.next and set it's 'score'++
        for (Card ace : this.aces) {
            if (ace.getSuit().equals(next.getSuit())) {
                this.acesPos.set(ace.getSuit().ordinal(), this.acesPos.get(ace.getSuit().ordinal()) + 1);
                break;
            }
        }
        // Final state: the last ace now has a pos >7, this ace is the result. start new Activity
        if (this.acesPos.get(next.getSuit().ordinal()) > 7) {
            this.game_ended = true;
            Intent winner = new Intent();
            winner.putExtra("winner",next.getSuit().ordinal());

            setResult(RESULT_OK, winner);
            finish();
        // If the ace is not at it's final position, move it 
        // and pick a new card in the next iteration.
        } else {
            // Move card
            myHandler.postDelayed(new Runnable() {
                public void run() {
                    Log.e("run: ", "moveCard");
                    moveCard();
                }
            }, 500);
        }
    }
}

标签: javaandroid

解决方案


也许创造这样的东西应该可行,

protected static void startTimer() {
    timer.schedule(new TimerTask() {
        public void run() {
            mHandler.obtainMessage(1).sendToTarget();
        }
    }, 500);
}

public Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
         myFunction();
    }
};

推荐阅读