如何暂停/恢复或停止线程并显示弹出窗口 [英] How to pause/resume or stop a thread and display a popup

查看:82
本文介绍了如何暂停/恢复或停止线程并显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码显示3 ... 2 ... 1正在启动线程:

I have the following code which displays 3...2...1 starting a thread:

...
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);
    }
}

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

Please help me with the stopping and pausing/resuming the thread.

推荐答案

如@Alon所建议的那样,您可能需要更改标志变量才能暂停和重新启动可运行对象.特别是,您可以尝试将android的消息传递系统与处理程序一起使用.不确定这是否是理想方法",但过去对我有用:)

As @Alon suggested, you might need a flag variable to change in order to pause and restart the runnable. In particular you could attempt to use android's messaging system with the handler. Not sure if this is the "ideal method" but it's worked for me in the past:)

  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();

以及用于管理线程可运行状态的处理程序.

And a handler to manage the thread runnable state.

   /** 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;
                }
            }
        };

然后通过消息执行暂停或继续选项.

Then perform the pause or resume options through messages.

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

这篇关于如何暂停/恢复或停止线程并显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆