线程-while循环-[Android]

发布于 2024-11-27 15:39:57 字数 1387 浏览 0 评论 0原文

因此,在我进一步尝试在 android 中实现 while 循环时,我想出了下面的代码:

private boolean connected = false;
private Thread loop = new Thread() {
    public void run() {
        Looper.prepare();
        while (connected) {
            do_Something();
        }
        Looper.loop();
    }
};

onCreate() {
    //.....
    ok = (Button) findViewById(R.id.button1);
    ok.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (connected) {
                try {
                    loop.start();
                }
                catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Exception caught", Toast.LENGTH_LONG).show();
                }
            }
            return true;
        }
    });
    stop = (Button)findViewById(R.id.button2);
    stop.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //disconnects current connection
                connected = false;
            }
            return true;
        }
    });
}

也就是说,我试图在触摸第一个按钮时将我的 UI 线程切换到将一遍又一遍地执行某些操作的线程直到触摸第二个按钮,在这种情况下,布尔变量 k 将关闭并完全停止按下第一个按钮时新创建的线程。我用谷歌搜索了“线程/处理程序/android while 循环”,但无济于事。对我想做的事情的任何帮助将不胜感激

简单地说,如何杀死通过按第二个按钮创建的线程?

So in my further attempts to implement a while loop in android I have come up with the code below :

private boolean connected = false;
private Thread loop = new Thread() {
    public void run() {
        Looper.prepare();
        while (connected) {
            do_Something();
        }
        Looper.loop();
    }
};

onCreate() {
    //.....
    ok = (Button) findViewById(R.id.button1);
    ok.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (connected) {
                try {
                    loop.start();
                }
                catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Exception caught", Toast.LENGTH_LONG).show();
                }
            }
            return true;
        }
    });
    stop = (Button)findViewById(R.id.button2);
    stop.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //disconnects current connection
                connected = false;
            }
            return true;
        }
    });
}

That is, I am trying to on the touch of the first button have my UI thread switch to the thread that will do_something over and over again until the touch of the second button, in which case the boolean var k will switch off and completely stop the newly created thread from the button press of the first button. I have google'd "threads/handlers/android while loops" but to no avail. Any help towards what I am trying to do would be much appreciated

Simply put, how do I kill the thread that was created via pressing the second button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2024-12-04 15:39:57

您尝试过 AsyncTask 吗?您可以做什么...在第一个按钮单击时启动一个新的 AsyncTask 并在第二个按钮单击时取消它。

//define global variable
private DoSomething doSomething;

//register firstButton onClickListener
firstButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //start your asynctask
        if(doSomething == null || doSomething.isCancelled()){
            doSomething = new DoSomething();
            doSomething = doSomething.execute();
        }
    }
});

//register secondButton onClickListener
secondButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        doSomething.cancel();
    }
});

//Inner AsyncTask class
class DoSomething extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params) {
        //doSomething();
        while(true){
            System.out.println(1);
            if(isCancelled()){
                break;
            }
        }
        return null;
    }
}

注意:这是伪代码...可能包含错误...只是想给您概述。希望这有帮助。

Did you tried AsyncTask ? What you can do...start a new AsyncTask on firstButton click and cancel it on secondButton click.

//define global variable
private DoSomething doSomething;

//register firstButton onClickListener
firstButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //start your asynctask
        if(doSomething == null || doSomething.isCancelled()){
            doSomething = new DoSomething();
            doSomething = doSomething.execute();
        }
    }
});

//register secondButton onClickListener
secondButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        doSomething.cancel();
    }
});

//Inner AsyncTask class
class DoSomething extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params) {
        //doSomething();
        while(true){
            System.out.println(1);
            if(isCancelled()){
                break;
            }
        }
        return null;
    }
}

Note: This is pseudocode...might contain error...just want to give you overview. hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文