Batz Pogi Batz Pogi - 1 year ago 70
Android Question

Button to trigger every 5 seconds

My app needs tracking of real time so I need a button that needs to trigger every 5 seconds but I have no idea how to do it. Can you teach me how?

I want that in every 5 seconds that

will be triggered.

btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HashMap postLoc = new HashMap();
postLoc.put("txtLat", tvLat.getText().toString());
postLoc.put("txtLng", tvLong.getText().toString());
postLoc.put("txtOwner", pref.getString("username","").toString());

PostResponseAsyncTask taskLoc = new PostResponseAsyncTask(getActivity(), postLoc,false, new AsyncResponse() {
public void processFinish(String s) {
Log.d(TAG, tvLat.getText().toString());
Log.d(TAG, tvLong.getText().toString());
Intent i = new Intent(getActivity(),GPS_Service.class);


Answer Source

I think this code might be useful to trigger the code every 5 second

 Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
public void onCreate() {

public void startTimer() {
    //set a new Timer
    timer = new Timer();

    //initialize the TimerTask's job

    timer.schedule(timerTask, 0, 5000); 

public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {
   Runnable() {
                public void run() {
                   //code to run after every 5 seconds
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download