如何暂停/恢复或停止线程并显示弹出窗口

时间:2016-12-04 03:11:00

标签: java android multithreading

我有以下代码显示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);
    }
}

请帮助我停止和暂停/恢复线程。

1 个答案:

答案 0 :(得分:3)

正如@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);
    */