Interuppting onClick() method with other click

时间:2016-03-02 10:56:43

标签: java android loops android-studio

I am trying to do this simple task. I have two buttons called START and STOP, I want to execute some task in loop by clicking on START and want to exit from this loop by clicking STOP. Code-

public class DrawPath extends Activity implements View.OnClickListener {
ArrayList<LatLng> positions = new ArrayList<LatLng>() ;
static int c=1;

Location location;
GoogleMap mMap;
Button btStart,btStop;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawpath);
    initializeVar();
    btStart.setOnClickListener(this);
    btStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            c = 0;
            System.out.println("tested2");
        }
    });
}
private void initializeVar()
{
    btStart=(Button)findViewById(R.id.btStart);
    btStop=(Button)findViewById(R.id.btStop);
}

@Override
public void onClick(View v) {
    getupdate(1);
    }

private void getupdate(int d) {
    c = d;
    CurrentPosition currentPosition = new CurrentPosition(this);
    if (c == 0) {
        System.out.println("Done");
    } else {
        location = currentPosition.getCurrentLocation();
        LatLng pos = new LatLng(location.getLatitude(), location.getLongitude());
        positions.add(pos);
        System.out.println("running");
        try {
            Thread.sleep(5000);
            getupdate(c);
        } catch (Exception e) {
        }
    }
}
}

Somebody please share any idea how to achieve it.

2 个答案:

答案 0 :(得分:0)

您可以使用带有Runnable的Handler在单击STOP按钮后停止线程。 我根据您的要求给您提示使用以下代码

Handler handler = new Handler();
Runnable runable = new Runnable({
   @Override
   public void run(){
        // count
        handler.postDelayed(this, 1000);
   }
});

现在您可以从btnStop.onClick()调用以下行。

handler.removeCallbacks(runable);

Check this了解有关Handler和Runnable的详细信息

答案 1 :(得分:0)

我建议它创建一个内部类,它扩展Thread并根据用户的操作启动和停止线程。这是一个例子

class DrawPath extends Activity implements View.OnClickListener {

    MyThread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawpath);
        initializeVar(); //not in my code so you add it
        btStart.setOnClickListener(this);
        btStop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btStart:
                if (thread == null) {
                    thread = new MyThread();
                    thread.start();
                }
                break;

            case R.id.btStop:
                if (thread != null) {
                    thread.interrupt();
                }
                break;
        }
    }

    class MyThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(3000);
                    //your stuff goes here or before sleep
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    thread = null;
                    break;
                }
            }
        }
    }

    //whey interrupt here bcz infinite loop will be running until and unless you stop it.
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (thread != null)
            thread.interrupt();
    }
}

我看到你的代码需要更多的改进,这就是为什么我写这个大文件:)

建议:

  1. 检查onClickListener的实现。
  2. 在onDestroy()处停止线程,因为即使关闭应用程序后线程也会运行,所以你需要在你停止时停止 出来(毁掉)你的主要活动。