package com.example.task02;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity 
{
			TextView text_reponse;
			Button btn_start, btn_stop;
			private Handler mHandler = new Handler();
			private int nCounter = 0;
			
			@Override
			public void onCreate(Bundle savedInstanceState)
			{
		        super.onCreate(savedInstanceState);
		        setContentView(R.layout.activity_main);
		       
		    	text_reponse = (TextView)findViewById(R.id.reponse);	
		        
		        btn_start = (Button)findViewById(R.id.btn_start);
		        btn_stop = (Button)findViewById(R.id.btn_stop);
			       
		        
		        btn_start.setOnClickListener(mButtonStartListener);
		        btn_stop.setOnClickListener(mButtonStopListener);
		    } 
 		
			
			View.OnClickListener mButtonStartListener = new OnClickListener()
			{
				public void onClick(View v) 
				{
						try {
								//mHandler.removeCallbacks(hMyTimeTask);
				         //        Parameters
				         //        r  The Runnable that will be executed. 
				         //        delayMillis  The delay (in milliseconds) until the Runnable will be executed. 
				                //mHandler.postDelayed(hMyTimeTask, 2000); // delay 1 second
				                mHandler.post(hMyTimeTask);
						} 
						
						catch (Exception e) 
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}
			};
			
			View.OnClickListener mButtonStopListener = new OnClickListener() 
			{
		    	public void onClick(View v) 
		    	{
		    		mHandler.removeCallbacks(hMyTimeTask);
		    		nCounter = 0;
			    	text_reponse.setText("Patoche thread counter: " + nCounter);
		    	}
		    };
 
		    private Runnable hMyTimeTask = new Runnable()
		    {
		    	public void run() 
		    	{
		    		nCounter++;
		    		text_reponse.setText("Patoche thread counter: " + nCounter);
	 				mHandler.postDelayed(hMyTimeTask, 2000);
		    	}
		    }; 
  
}


/*
class tartuffe
{
 
		private int cnt = 0;
 		Button Start, Stop;
 		TextView counter;
 		private Handler handler = new Handler();
 
 		
 		@Override
 		public void onCreate(Bundle savedInstanceState) 
 		{
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        counter = (TextView)findViewById(R.id.counter);
	        Start = (Button)findViewById(R.id.btn_start);
	        Start.setEnabled(true);
	        Stop = (Button)findViewById(R.id.btn_stop);
	        Stop.setEnabled(false);
	        
	        Start.setOnClickListener(new Button.OnClickListener()
	        {
				   @Override
				   public void onClick(View v) 
				   {
					    // TODO Auto-generated method stub
					    handler.post(timedTask);
					    Start.setEnabled(false);
					    Stop.setEnabled(true);
			   }
			});
        
	        Stop.setOnClickListener(new Button.OnClickListener()
	        {

				   @Override
				   public void onClick(View v)
				   {
				    // TODO Auto-generated method stub
					    handler.removeCallbacks(timedTask);
					    Start.setEnabled(true);
					    Stop.setEnabled(false);
					    cnt = 0;
					    counter.setText(String.valueOf(cnt));
				   }
			});
		}
    
 		private Runnable timedTask = new Runnable()
 		{

 			@Override
 			public void run() 
 			{
 				// TODO Auto-generated method stub
 				cnt++;
 				counter.setText(String.valueOf(cnt));
 				handler.postDelayed(timedTask, 2000);
 			}
 		};
}
*/