簡體   English   中英

如何暫停/恢復或停止線程並顯示彈出窗口

[英]How to pause/resume or stop a thread and display a popup

我有以下代碼顯示3 ... 2 ... 1開始一個主題:

...
final Handler handler = new Handler();
final TextView textView = (TextView) findViewById(R.id.textView2);
final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3);
final Runnable counter = new Runnable() {
    @Override
    public void run() {
        textView.setText(Integer.toString(n.get()));
        if(n.getAndDecrement() >= 1 )
            handler.postDelayed(this, 1000);
        else {
            textView.setVisibility(View.GONE);
            tv.post(new Roller(900)); //tv is a textView
        }
    }
};
handler.postDelayed(counter, 1000);
...
ImageButton ibStop;
ibStop.setOnClickListener(...() {
    //stop the thread
    //display a popup
});

ImageButton ibPause;
ibPause.setOnClickListener(...() {
    //pause the thread
    //display a popup
});

ImageButton ibPlay;
ibPlay.setOnClickListener(...() {
    //display the `handler` above with the 3...2...1...
    //resume the thread
});

可運行:

private class Roller implements Runnable
{
    private long delayMillis;
    private Boolean stop = false;

    public Roller(long delayMillis)
    {
        this.delayMillis = delayMillis;
    }

    @Override
    public void run()
    {
        int min = 0;
        int max = 1;
        int n = rand.nextInt(max - min + 1) + min;
        String roll = String.valueOf(n);
        tv.setText("Random number is " + roll); //tv is a textview

        if (roll.equals("0")) {
            inType = 0;
            ibRed.setImageResource(R.drawable.red_selector);
            ibGreen.setImageResource(R.drawable.green_dark);
        }
        if (roll.equals("1")) {
            inType = 1;
            ibRed.setImageResource(R.drawable.red_dark);
            ibGreen.setImageResource(R.drawable.green_selector);
        }

        tv.postDelayed(this, delayMillis);
    }
}

請幫助我停止和暫停/恢復線程。

正如@Alon建議的那樣,您可能需要更改標志變量才能暫停和重新啟動runnable。 特別是你可以嘗試將android的消息系統與處理程序一起使用。 不確定這是否是“理想的方法”,但它在過去對我有用:)

  boolean mRunning; int AYE_AYE_CAPTAIN=4; int MESSAGE_PAUSE=3; 
  int MESSAGE_RESUME=2;
  Runnable roller = new Runnable() {
            @Override
            public void run() {
                while(mRunning) {
                    try {
                       // Do your stuff here.
                       /* In case you want to communicate with handler */
                       Message msg = new Message();
                       msg.what=AYE_AYE_CAPTAIN;
                       handler.sendMessage(msg);
                    } catch (InterruptedException e) {
                       // Not good!!!
                    }
                }
            }
        }, timeToRun,this);

        mRunning=true;
        roller.start();

以及管理線程可運行狀態的處理程序。

   /** Handler to keep track of and manage thread runnable state */
   handler = new Handler(Looper.getMainLooper()){

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MESSAGE_PAUSE:
                        // Take an action 
                        mRunning=false;
                        break;
                    case MESSAGE_RESUME;
                        mRunning = true;
                        // Restart Runnable here.
                        roller.start();
                        break;
                    default:
                        break;
                }
            }
        };

然后通過消息執行暫停或恢復選項。

public void main sendLifeCycleMessage(int MESSAGE) {
    Message awesomeMessage = new Message();

    awesomeMessage.what = MESSAGE; //
    roller.sendMessage(awesomeMessage);
    /* alternatively if your calling from a service:
    mService.getHandler().sendMessage(awesomeMessage);
    */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM