public class MainActivity extends ActionBarActivity { String n=""; Timer timer; TimerTask timerTask; final android.os.Handler handler = new android.os.Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startTimer(); Test2(); } public void startTimer() { // Pull(); //set a new Timer timer = new Timer(); //initialize the TimerTask's job initializeTimerTask(); //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms timer.schedule(timerTask,0, 15000); // } public void Timer15() { timer =new Timer(); initializeTimerTask(); timer.schedule(timerTask,0,5000); } public void initializeTimerTask() { timerTask = new TimerTask() { public void run() { //use a handler to run a toast that shows the current timestamp handler.post(new Runnable() { public void run() { Test1(); // Toast.makeText(getApplicationContext(),"hai",Toast.LENGTH_SHORT).show(); } }); } }; } public void Test1() { n="vetri";//Assigning value for n } public void Test2() { if (n=="vetri")//Here n value is null { timer.cancel(); Timer15(); Toast.makeText(getApplicationContext(),n,Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); } } }
startTimer
Test2
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)