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 6185467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:42:57+00:00 2026-05-24T01:42:57+00:00

In my code i’m using an IntentService to listen to location updates (either GPS

  • 0

In my code i’m using an IntentService to listen to location updates (either GPS or network updates) and this IntentService is triggered when an event is received, so it is started with startService() from any activity.

public class AddLocationService extends IntentService implements LocationListener {
    /*My code here*/
}

    @Override
protected void onHandleIntent(Intent intent) {
    if(getOldLoc() == null) 
    { 
        //Get a new location
        this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_GPS, 0, this);
        this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_INTERVAL_GPS, 0, this);
        Log.d(AddLocationService.TAG, "Network listener started");

        this.time_start_listening = System.currentTimeMillis();
        mTimerThread mTimerRunnable = new mTimerThread();
        this.timerThread = new Thread(mTimerRunnable);
        this.timerThread.start();
    }
    else    
        /*REUSE OLD LOCATION*/
}

Now my problem is : When two events start this IntentService and the second starts it while the first one is still requesting for updates, I will like the second one to wait until first one is fully finished (location found OR timer thread finishes).
However whenever the IntentService is executed a second time (first instance still running), it prints me the log and does as it was executing in parallel.

However I thought that the main goal of IntentService was that it is something sequential so a second intent would have to wait until first one is done…

Did I missunderstood something ?

  • 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-24T01:42:58+00:00Added an answer on May 24, 2026 at 1:42 am

    It appears that your onHandleIntent method is not blocking the thread it is executing on, so it will return quickly and allow the second intent to be processed. Not only that, but any callbacks from the LocationManager to that thread are unlikely to be processed as the background thread is likely to be killed when onHandleIntent is finished.

    If you really want to use IntentService to manage your intent queue then you will need to do your location handling on its own thread, and join the IntentService thread to the location thread whilst it is waiting for the location callback.

    Heres a bit of code that demonstrates the idea:

    public class TestService extends IntentService {
        private static final String TAG = "TestService";
    
        private Location mLocation = null;
    
        public TestService() {
           super(TAG);
        }
    
        @Override
        public void onHandleIntent(Intent intent) {
            Log.d(TAG, "onHandleIntent");
    
            if (mLocation == null) {
                Log.d(TAG, "launching location thread");
                LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
                LocationThread thread = new LocationThread(locationManager);
                thread.start();
                try {
                    thread.join(10000);
                } catch (InterruptedException e) {
                    Log.d(TAG, "timeout");
                    return;
                }
    
                Log.d(TAG, "join finished, loc="+mLocation.toString());
            } else {
                 Log.d(TAG, "using existing loc="+mLocation.toString());
            }
        }
    
        private class LocationThread extends Thread implements LocationListener  {
            private LocationManager locationManager = null;
    
            public LocationThread(LocationManager locationManager) {
                super("UploaderService-Uploader");
                this.locationManager = locationManager;
            }
    
            @Override
            public void run() {
                Log.d(TAG, "Thread.run");
                Looper.prepare();
               this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    
                Looper.loop();
            }
    
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                Log.d(TAG, "onLocationChanged("+location.toString()+")");
                mLocation = location;
                Looper.myLooper().quit();
             }
    
             @Override
             public void onProviderDisabled(String arg0) {
             }
    
             @Override
             public void onProviderEnabled(String arg0) {
             }
    
             @Override
             public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
             }
    
       }
    }
    

    Of interest in there is the Looper that runs a message loop on the thread (to allow handling of the callbacks).

    Given the effort required to do this with IntentService it might be worthwhile investigating deriving from Service instead and managing your own intent queue.

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

Sidebar

Related Questions

Code like this often happens: l = [] while foo: # baz l.append(bar) #
Code below sets default values for new row if row is added using form.
Code: $(#telecomGrayscale, this).stop().animate({ top: '467px' }, { duration: 400 }).delay(800).queue(function() { $(#boxcaptionTelecom, this).stop().animate({ top:
code below. i'm tryind to obtain string answers like a1, c4 this is what
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
Code: #include <iostream> using namespace std; #define ADD(x,y) ((x)+(y)) int main( int argc, char**
Code would explain this much better :) def a(): x=0 def b(z=x): print(X: %d,
Code that was working fine last week is suddenly throwing this exception: System.Data.SqlClient.SqlException (0x80131904):
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.