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

  • Home
  • SEARCH
  • 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 8909793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:31:26+00:00 2026-06-15T03:31:26+00:00

Details: I have a service that needs to do the following: – listen constantly

  • 0

Details:

I have a service that needs to do the following:
– listen constantly for GPS locations, with 50 meters as parameter (works fine) and send it to a server
– each 60 seconds, if no GPS location was read, start listening for a Network location and send it to the server

This may sound weird, but this is the project requirement. So the user is constantly being tracked using GPS. When he stops, or GPS is not locked, or is inside a building, every 60 seconds start a quick Network location read, and send this location to the server. As the service uses the same thread as the main app, each server update is done in its own thread. And another important thing: each location read should be sent one after another, so for instance if the user is driving, and multiple reads are done, each should be send to the server, after the previous one has been sent. That’s why I decided to use ScheduledExecutorService as I can submit threads and they will be execute one after another.

Here is the code:

private ScheduledExecutorService scheduleTaskExecutor;
Handler locationHandler = new Handler();
private Location lastNetworkLocation;

@Override
    public void onStartCommand() {
        scheduleTaskExecutor = Executors.newSingleThreadScheduledExecutor();

        //prepare to execute Network reading every 60 seconds
        scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                    initNetworkReadings();

                    //usually a network location read is done almost instantly
                    //however after 5 seconds I check if a location has been read by the Network listener inside the locationRunnable
                    locationHandler.postDelayed(locationRunnable, 5000);
                } 

            }
        }, 60, 60, TimeUnit.SECONDS);

        locationRunnable = new Runnable() {
            @Override
            public void run() {
                    if (lastNetworkLocation !=null){
                         //send location to the server if valid
                    }
            lastNetworkLocation = null;
            }
        }
    }

    private void initNetworkReadings() {
        locationManager.removeUpdates(locationListenerNetwork);
        try {
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        if (isGpsEnabled) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        }
    }

    LocationListener locationListenerNetwork = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        lastNetworkLocation = location;
        // stop listening for Network locations
        locationManager.removeUpdates(locationListenerNetwork);
    }
    ...
    }

Each time I read a GPS location I add it to the threads queue as:

scheduleTaskExecutor.submit(new Runnable() {
        @Override
        public void run() {
            updateLocationOnServer(readLocation);
        }
    });

The problem I have is that the Network location listener never gets onLocationChanged() called,when I use it like in the code above, in the Runnable. BUT if I add on service start, the initNetworkReadings(), I get onLocationChanged() fired right away. So I believe it has something to do with being used in scheduleAtFixedRate.
What do you think it could be the problem ? Anything bad in the way I thought of the workflow ?

  • 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-06-15T03:31:28+00:00Added an answer on June 15, 2026 at 3:31 am

    What if you try to set up your repitition with a Handler and a Runnable instead of scheduleAtFixedRate()?

    Handler h = new Handler();
    Runnable run = new Runnable(){
        //Do your repeititive work here!
        h.postDelayed(run, 60 * 1000);
    };
    h.post(run);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a personal details form that allows you to enter a certain number
I have been asked to get details of every user that logs into our
I have written a SOAP based web service which runs fine on a Tomcat
I have a simple ASP.NET hosted WCF Service that has several methods that all
We have a tab based application that needs to support the ability for a
I have a load tester that calls my WCF service and I've built it
I have the following Json string that I need to deserialize. {123456789: {short_description:Delivered, detail_description:Your
I have a WCF service with the following method: [OperationContract] TernaryWebServiceResponse<long> ModifyObservations(...); Response object
I have a web service proxy that calls another web service with a user
I have the following php and wish to know how I can achieve that

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.