Wednesday 20 June 2012

Run Specific task repeatedly after fixed time intervals


package com.test;


import java.util.Timer;
import java.util.TimerTask;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;


public class AndroidThreadActivity extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        toCallAsynchronous();
    }
    
   


    public void toCallAsynchronous() {
        TimerTask doAsynchronousTask;
        final Handler handler = new Handler();
        Timer timer = new Timer();
       


                doAsynchronousTask = new TimerTask() {
                 int count=0;
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        handler.post(new Runnable() {
                            public void run() {


                                try {
                                 System.out.println("sdgdsfgadfgfd"+count);
                                  count++;
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block




                                }


                            }
                        });


                    }


                };


                timer.schedule(doAsynchronousTask, 0,5000);//execute in every 50000 ms


            }
}

No comments:

Post a Comment