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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:29:09+00:00 2026-06-12T11:29:09+00:00

I am trying to create a polling service for location which starts for updates

  • 0

I am trying to create a polling service for location which starts for updates for location and waits 2 mins
and then removes the updates

when i try to send location using my emulator it seems my locationlistener doesn’t recieve one.

is it because i didn’t implement a handler for the looper I created in the thread?

or because my thread sleeps so i don’t receive any location

package android.co.in;

import android.graphics.Canvas;
import android.os.Looper;
import android.view.SurfaceHolder;

public class customThread extends Thread {

boolean stop;
boolean run;
customThread()
{   
    BounceLogger.logIt(this, "Constructor()");
    stop=false;
    run=true;
}
@Override
public void run()
{
  Looper.prepare();
  BounceLogger.logIt(this, "run()");
  while(!stop)
  {
      while(run)
      {
        updateThread(); 
      }

      try 
      {
          wait();
      } 
      catch (InterruptedException e) 
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

  }
  Looper.loop();
}   

//derived class should overide this function
public void updateThread()
{


}


public void runThread()
{  
  start();
}

public void stopThread()
{
    stop=true;
}

public void pauseThread()
{
    run=false;
    BounceLogger.logIt(this,"calling wait on worker thread");
}

public void resumeThread()
{
    BounceLogger.logIt(this,"calling notify on worker thread");
    run=true;
    notify();       
}


}

//responsible for all location related queries
public class UserLocationManager extends customThread{

boolean locationFound;

LocationSelector locationSelector;

UserLocationManager(BuddiesAroundActivity activity)
{
    super();
    locationFound=false;
    locationSelector=LocationSelector.getLocationSelector(activity);
}

Location GetUserLocation()
{
    queryUserLocation();
    return locationSelector.getLastKnownLocation();
}

@Override
public void updateThread()
{
    locationSelector.startListening();
    try {
        Thread.sleep(200000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    locationSelector.stopListening();
}

void queryUserLocation()
{       
    runThread();    
}


}

package android.co.in;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationSelector 
{

private static LocationSelector locationSelector=null;
private static final int TWO_MINUTES = 1000 * 60 * 2;
LocationManager locationManager;
LocationListener locationListener;
Location lastKnownLocation;

LocationSelector()
{
    Intialize();
}

LocationSelector(BuddiesAroundActivity activity)
{
    Intialize();
    locationManager=
       (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
}

static LocationSelector getLocationSelector(BuddiesAroundActivity activity)
{
    if(locationSelector==null)
        locationSelector = new LocationSelector(activity);

    return locationSelector;    
}

void startListening()
{
    if(locationManager!=null)
    {
        BounceLogger.logIt(this, "started listening on location updates");

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100,     locationListener);
              locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,100, locationListener);
    }
}

public void stopListening()
{
    BounceLogger.logIt(this, "stopped listening on location updates");
    locationManager.removeUpdates(locationListener);
}


private void Intialize() 
{
    lastKnownLocation=null;
    // TODO Auto-generated method stub
    locationListener=new LocationListener() {
        public void onLocationChanged(Location current) 
        {
          // Called when a new location is found by the network location     provider.
            BounceLogger.logIt(this,"recived a location"+current.getLatitude()+":"+current.getLongitude());
            if(lastKnownLocation==null)
            {
                lastKnownLocation=current;
            }
            getBestLocation(lastKnownLocation,current);

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}

      };

}

private float getDistanceBetweenLocations(Location a,Location b)
{
    float distance =a.distanceTo(b);     
    return distance;     
}

private double getAngleBetweenLocations(Location origin,Location destination)
{
     double angle=0.0f;
     double longDiff;
     double latDiff;

     longDiff=destination.getLongitude()-origin.getLongitude();
     latDiff=destination.getLatitude()-origin.getLatitude();

     angle=Math.atan2(longDiff,latDiff);         
     return angle; 
 }

Location getLastKnownLocation()
{
    return lastKnownLocation;
}

Location getBestLocation(Location old,Location current)
{
    if(old ==null)
        return current;

    //check time
    long timeDelta = current.getTime() - old.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    int useCurrentLocationByTime=0;
    if(isSignificantlyNewer)
    {
        useCurrentLocationByTime++;
    }

    //check for accuracy
    int useCurrentLocationByAccuracy=0;
    if(old.getAccuracy() < current.getAccuracy())
    {
        useCurrentLocationByAccuracy++;
    }

    //check for provider this is blunt but u might want give priority to providers and then decide
    int useCurrentLocationByProvider=0;
    if(old.getProvider().equals(current.getProvider()))
    {
        useCurrentLocationByProvider++;
    }

    int points=useCurrentLocationByTime+useCurrentLocationByAccuracy+useCurrentLocationByProvider;

    if(points > 1.5)
    {
        return current;     
    }

    return old;     
}

}

  • 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-12T11:29:11+00:00Added an answer on June 12, 2026 at 11:29 am

    In your updateThread you start the request for Location and then you put the thread tp sleep for 200 seconds, which makes it impossible to receive new locations. As soon as the thread awake, you remove the Location updates request.

    You need to redesign your code, in a way that Location Listner is run in a different thread, or even better, you use a handler.post() method to run a runnable 200 seconds later that would stop location listener and would avoid using threads for that.

    –EDITED–

    you sould do something like:

    Handler handler = new Handler();
    startLocationListener(); //in this method you only add the listener for onLocationsChange(). No threads needed.
    handler.postDelayed(rStopLocationListener, 200000); //the runnable rStopLocationListener will run in 200 seconds
    
    //define rStopLocationListener
    private Runnable rStopLocationListener = new Runnable() {
      public void run() {
              stopLocationListener(); //in this method you only remove the listener for onLocationsChange(). No threads needed.
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a AJAX push implementation in PHP using a Comet long-polling
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I was trying to create a polling script in python that starts when another
Within my web application, I am trying to create a directory polling bean using
Ok so I am trying create a login script, here I am using PHP5
Trying to create a macro which can be used for print debug messages when
I am trying to create Facebook based login using Javascript. Once a person clicks
I am trying to create an application for polling different sensors. I want to
I am trying to create a server-side solution which periodically pushes data to the
I am trying to create a python script which I will later run as

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.