螺纹助益,而环 - [Android版] [英] Thread -while loop- [Android]

查看:103
本文介绍了螺纹助益,而环 - [Android版]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我进一步尝试实现机器人while循环我想出了下面的code:

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

这就是我想的第一个按钮的触摸有我的UI线程切换到线程将do_something一遍又一遍,直到第二个按钮的触感,在这种情况下,布尔变种k,便会关闭并完全停止从第一个按钮的按钮preSS新创建的线程。我已经google'd线程/处理器/ Android的while循环,但无济于事。对我所试图做的是多AP preciated任何帮助

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

简单地说,我怎么杀pssing第二个按钮是通过$ P $创建线程?

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

推荐答案

你有没有试过的AsyncTask?你可以做什么...开始firstButton点击一个新的AsyncTask并取消它secondButton点击。

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

注:这是伪code ...可能含有错误...只是想给你的概述。希望这帮助。

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

这篇关于螺纹助益,而环 - [Android版]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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