0

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.

Bharatesh
  • 8,943
  • 3
  • 38
  • 67
Ashu
  • 71
  • 10

2 Answers2

0

You can use Handler with Runnable to stop your thread after STOP button click. I am giving you hint use following code according to your requirement

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

Now you can call following line from your btnStop.onClick().

handler.removeCallbacks(runable);

Check this for more details on Handler and Runnable

Community
  • 1
  • 1
Rakesh
  • 756
  • 1
  • 9
  • 19
0

What I suggest it create a inner class which extends Thread and according to user's action start and stop the thread. here is an example

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

I saw your code which needs little more improvements that's why I wrote this big file :)

Suggestions :

  1. Checkout the implementation of onClickListener.
  2. Stopping thread at onDestroy() because thread will be running even after you close your application, so you need to stop when you come out (destroyed) of your main activity.
Bharatesh
  • 8,943
  • 3
  • 38
  • 67