How to stop a timer after certain number of times
<p>Trying to use a timer to do run this 4 times with intervals of 10 seconds each. I have tried stopping it with a loop, but it keeps crashing. Have tried using the schedule function with three parameters, but I didn't know where to implement a counter variable. Any ideas?</p>

<pre><code>final Handler handler = new Handler(); 
                    Timer timer2 = new Timer(); 

                    TimerTask testing = new TimerTask() {
                        public void run() { 
                            handler.post(new Runnable() {
                                public void run() {
                                    Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();

                                }

                            });


                        }
                    }; 

                  int DELAY= 10000;
                 for (int i=0; i!=2 ;i++)
                 {
                    timer2.schedule(testing,DELAY);
                    timer2.cancel();
                    timer2.purge();
                 }
</code></pre>
