Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4624456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:07:55+00:00 2026-05-22T03:07:55+00:00

I am using the TimerTask in my app to update the current GPS to

  • 0

I am using the TimerTask in my app to update the current GPS to the server. I have extended the TimerTask and override the run method to do so. I have a stoptimer button in screen that should stop the timer, once it is clicked. but my problems is, even the timerobject.cancel()
is getting executed, but the timer is still running.

Can anybody share your opinion on stopping the timer when a particular button is clicked. Below is the code that i have written to run the timer task.

PointMyLocation.java

public class PointMyLocation 
{
  private String log;
  double longi;
  double lati;
  public String email, city;

  private HttpServiceCommunication mHttpService;

// Default Constructor
public PointMyLocation(int value){
       new LocationTracker(value).StopTimer();
}

public PointMyLocation(String email, String city)
{
    this.email = email;
    this.city = city;
    new LocationTracker();
    //mHttpService = new HttpServiceCommunication();

}
public boolean onClose()
{
    Application.getApplication().requestBackground();
    return false;
}

class LocationTracker extends TimerTask {
 private  double longitude, latitude;

private Timer timer;
private LocationProvider provider;
private BeaconingBean mBb;
int mTimeinterval;
Criteria cr;

public LocationTracker() {
    cr= new Criteria();  
    this.run(); // Calling the run

}

    public void run() {
     timer = new Timer();
     resetGPS();
     //mTimeinterval = mBb.getmTimeInterval();
     //System.out.println("Time Interval :" + mTimeinterval);
     timer.schedule(this, 0, 150000); 
    }

public void StopTimer(){
    // Terminates the timer
    this.timer.cancel();   // Though this statement gets executed, the timer starts   again
}

public void resetGPS()
{
    try 
    {
        provider = LocationProvider.getInstance(cr);
        if(provider != null) 
        {
            provider.setLocationListener(new MyLocationListener(), 3, -1, -1);
        }

    } catch(Exception e){ }
}

private class MyLocationListener implements LocationListener 
{

    public void locationUpdated(LocationProvider provider, Location location)
    {
        if(location != null && location.isValid())
        {               
            QualifiedCoordinates qc = location.getQualifiedCoordinates();
            try 
            {
                lati = location.getQualifiedCoordinates().getLatitude();
                System.out.println("latitude :: "+lati);
                longi = location.getQualifiedCoordinates().getLongitude();
                System.out.println("longitude ::"+longi);
                System.out.println("Email :: " + email);
                System.out.println("City ::" + city);
             } 
            catch(Exception e)
               { 

               }
        }

    }

    public void providerStateChanged(LocationProvider provider, int newState)
    {
        //LocationTracker.this.resetGPS();
         if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
            {
                provider.reset();
                provider.setLocationListener(null, 0, 0, -1);
            }
    }
}
} 
}

Any help is highly appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:07:56+00:00Added an answer on May 22, 2026 at 3:07 am

    Your basic problem is that you’re re-starting your timer inside the run() method, which you shouldn’t be doing. The run() method gets called every time the timer “ticks” – you don’t want to be altering and restarting your timer object in here.

    Try this (not tested but it should work). Add a method to your Timer_Task class called start:

    public void start() {
        timer = new Timer();
        timer.schedule(this, 0, 150000);
    }
    

    Change your run() method to this:

    public void run() {
        resetGPS();
    }
    

    Finally, in the class’ constructor, call this.start() instead of this.run().

    The reason your stop method doesn’t appear to stop the timer is that even when you cancel a timer, if there’s one pending run() call out there, the call will still happen even if the timer has been cancelled. When this last call happens in your existing code, a new timer is created and started, so the overall process never stops.

    Edit: One other change you should make is to add a boolean inside your Timer_Task class called _isRunning, and set this to true when you start the timer and set it to false inside your Stop method. You would then check this variable inside the run() method, and return if _isRunning is false (this lets you ignore any pending run() calls after you stop the timer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app I am using a timer and timerTask to perform a certain
I have implemented button's onLongClickListener in Android Dialog which auto-decreases number using timers. Here
I have scheduled a TimerTask to run at a fixed time with a interval
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
I have an android app that repeatedly collects fingerprints from the wifi-networks that are
I am writing a twitter app using jtwitter - and its running inside a
I have an extended dialog class that I want to show for 3 seconds
So I'm running a Java server and in one class I have a static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.