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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:31:12+00:00 2026-05-27T09:31:12+00:00

I’m building a service that will let me know if the location hasn’t change

  • 0

I’m building a service that will let me know if the location hasn’t change in a period of time for a certain amount of metres.

Then thing is I have the event onLocationChanged on my Listener.. but I don’t know how to do the opposite.. that is, send a broadcast if the location is within the distance I provided after some minutes.

This is the code I have so far

LocationService

public class LocationService extends Service {

    public static final String LOC_INTENT = "com.xxx.intent.action.LOCATION";

    private Thread triggerService;
    protected LocationManager locationManager;
    protected MyLocationListener MyLocationListener;
    protected Criteria criteria;

    public static final int MIN_TIME = 300000; // 5 Minutes
    public static final long MIN_DISTANCE_MOTOR = 50; // 50 Metres

    private SharedPreferences settings;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        settings = getSharedPreferences(getString(R.string.settings_prefsName), 0);
        addLocationListener();

        return START_STICKY;
    }

    private void addLocationListener()
    {
        triggerService = new Thread(new Runnable(){
            public void run(){
                try{
                    Looper.prepare();//Initialise the current thread as a looper.
                    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

                    criteria = new Criteria();
                    criteria.setAccuracy(Criteria.ACCURACY_FINE);

                    final String PROVIDER = locationManager.getBestProvider(criteria, true);

                    updateLocation(getLastBestLocation(MIN_TIME, MIN_DISTANCE_MOTOR));

                    MyLocationListener = new MyLocationListener();
                    locationManager.requestLocationUpdates(PROVIDER, MIN_TIME, MIN_DISTANCE_MOTOR, MyLocationListener);
                    Log.d("LOC_SERVICE", "Service RUNNING! ("+PROVIDER+")");
                    Looper.loop();
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }, "LocationThread");
        triggerService.start();
    }


    public Location getLastBestLocation(int minDistance, long minTime) {
        Location bestResult = null;
        float bestAccuracy = Float.MAX_VALUE;
        long bestTime = Long.MIN_VALUE;

        List<String> matchingProviders = locationManager.getAllProviders();
        for (String provider: matchingProviders) {
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                float accuracy = location.getAccuracy();
                long time = location.getTime();

                if ((time > minTime && accuracy < bestAccuracy)) {
                    bestResult = location;
                    bestAccuracy = accuracy;
                    bestTime = time;
                }
                else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
                    bestResult = location;
                    bestTime = time;
                }
            }
        }

        return bestResult;
    }

    public static void updateLocation(Location location)
    {
        Context appCtx = MyApplication.getAppContext();

        double latitude, longitude;
        float speed;

        latitude = location.getLatitude();
        longitude = location.getLongitude();
        speed = location.getSpeed();

        Intent filterRes = new Intent();
        filterRes.setAction(LOC_INTENT);
        filterRes.putExtra("latitude", latitude);
        filterRes.putExtra("longitude", longitude);
        filterRes.putExtra("speed", speed);
        appCtx.sendBroadcast(filterRes);
    }


    class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location)
        {
            if(settings.getBoolean("active", false))
                updateLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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

}
  • 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-27T09:31:13+00:00Added an answer on May 27, 2026 at 9:31 am

    Set a timer for however long you want to test this. When it goes off, check if the last location you got in onLocationChanged is older than the timer length.

    EDIT

    Here is how I would imagine your service looking

    Service starting

    • requestLocationUpdates called with appropriate minimum time and minimum distance that that you will be notified after
    • Repeating task set where you check if an update was received (check out Timer.scheduleAtFixedRate

    Service running

    • Perform necessary actions when your timer goes off or onLocationChanged is called

    Service stopping

    • Remove location updates with removeUpdates
    • Stop your timer
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.