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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:07:56+00:00 2026-06-18T01:07:56+00:00

I’m trying to create an application that sends the user’s location to the database

  • 0

I’m trying to create an application that sends the user’s location to the database every 5 minutes, after that the user will be able to see it. There are few constraints:

  • If the user is not moving DO NOT generate another location. I do that by checking if the new location distance to the current location is more then 1800M (I use only locations with accuracy under 900 so the radious of possible locations is 1800).

  • If I can get a better location based on accuracy to my current location then use it.

So I will provide now my code and I would like to know what do you think about it and if it can be done better because it’s not working perfectly. If you want a clear question : Ignore my code, how do I implement those 2 points I mentioned.

Here is the code:

public void onStart(Intent intent, int startId) 
{
    ...
    this.timer.scheduleAtFixedRate(new Send(), d1, TEN_MINUTES/2);
}
...
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_MINUTES/2, 0, listener);

if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TEN_MINUTES/2, 0, listener);
...
@Override
public void onLocationChanged(Location location) 
{
    handleLocation(location);
}
private void handleLocation(Location location) 
{
    if(isUsable(location))
    {
        this.isUsable = true;
        this.newLocation = location;
    }
}
public boolean isUsable(Location location)
{
    if (mGeocoderAvailable) 
    {
        this.address = reverseGeocode(location);
        return location.hasAccuracy() && location.getAccuracy() < 900 && !this.address.equals("");
    }
    return false;
}

class Send extends TimerTask
{
    boolean run = true;

    @SuppressWarnings("deprecation")
    public void run()
    {
        if(isUsable)
        {
            isUsable = false;
            if(newLocation != null)
            {
                if(location != null)
                {
                    if(location.getAccuracy() > newLocation.getAccuracy() + 100)
                        sendTask();
                    else
                        if(newLocation.distanceTo(location) > 1800)
                            sendTask();
                }
                else
                    sendTask();
            }
        }
    }
}

The most important piece of code is the Send class of course and the isUsable method which defines my constraints.

Bugs that popped out is duplication of same address, that shouldn’t be since this condition should cover this case:

if(newLocation.distanceTo(location) > 1800)

And another thing is that I drove my car for 20 km’s with my phone and I didn’t receive any location in my db, only when I finished driving (after 20km)..

  • 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-18T01:07:57+00:00Added an answer on June 18, 2026 at 1:07 am

    First of all, you probably want to have a variable uncertainty requirement.

    Secondly, here is your command to start the timer.

    public void onStart(Intent intent, int startId) 
    {
        ...
        this.timer.scheduleAtFixedRate(new Send(), d1, TEN_MINUTES/2);
    }
    

    This command isn’t right. You also might want to create an instance of Send before you send it to the timer.

    this.timer.scheduleAtFixedRate(new Send(), d1, 5, TimeUnits.MINUTES);
    

    Before you go out driving the app, I suggest you test key parts of it with a more frequent time in your test environment. Set the time to every 10 seconds, and check to make sure it is working correctly, that your math is correct, it is reporting a position, etc.

    Lastly, your method for adding is this:

    if(newLocation != null)
    {
        if(location != null)
        {
            if(location.getAccuracy() > newLocation.getAccuracy() + 100)
                sendTask();
            else
                if(newLocation.distanceTo(location) > 1800)
                    sendTask();
        }
        else
            sendTask();
    }
    

    First of all, you shouldn’t use an else in this case, you should really combine all of the if statements into one common theme. I’m not quite sure either where it is getting location/newLocation from, you should probably make getting functions to them, bonus points if they are thread safe. Also, and this is probably key, you are always executing sendTask if there isn’t a valid location, I suspect that’s your key bug. Try replacing that code with this:

    if (location!=null 
        && ((location.getAccuracy() > newLocation.getAccuracy() + 100)
        && newLocation.distanceTo(location) > 1800)
    {
        sendTask();
    }
    
    • 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 an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.