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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:44:10+00:00 2026-06-07T05:44:10+00:00

Currently i have the coding as below for use on my activity on android.

  • 0

Currently i have the coding as below for use on my activity on android. Right now i use this as an object and start scanning location and get back it’s location using returnBestLocation method 3seconds later using a handler.

However i would like to ask is there any possibility that MyLocationListener object will automatically return call the activity on location change instead of calling the object to retrtive location 3s later?

public class MyLocationListener implements LocationListener {

    LocationManager locationManager;
    Date currentBestLocationDate;
    Intent notificationIntent;
     Context mContext;
    Location currentBestLocation = null, lastKnownLocation=null;

 public MyLocationListener(Context mContext)
 {this.mContext = mContext;

 }


 public void startLocationScan()
 {
     Log.d(Config.log_id, "Custom Location Listener started");

     if (locationManager == null) {

        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        Location locationNETWORK = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (locationNETWORK != null) {
            lastKnownLocation=locationNETWORK;
        }
        Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (locationGPS != null) {
            lastKnownLocation=locationGPS;

        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,  MyLocationListener.this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,MyLocationListener.this);





    }


 }
 public void stopLocationScan()
 {

        if(locationManager!=null)
        {
            locationManager.removeUpdates(MyLocationListener.this);

             Log.d(Config.log_id, "Custom Location Listener Stopped");
        }
        }
public Location returnBestLocation()
{
    return currentBestLocation;
}

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onLocationChanged(Location location) {


        // TODO Auto-generated method stub
        if (currentBestLocation == null) {
            currentBestLocation = location;
        }

        long timeDelta = location.getTime() - currentBestLocation.getTime();
        Log.d(Config.log_id, "locationpostingservice's changed with accuracy " + location.getAccuracy() + " s different " + (float) timeDelta / 1000);

        if (timeDelta >= 120000) {
            currentBestLocation = location;
            Log.d(Config.log_id,"posting service Location changed due to over 2min "+ location.getAccuracy() + " s different "+ (float) timeDelta / 1000);

        }


        if (currentBestLocation.getAccuracy() >= location.getAccuracy()) {
            currentBestLocation = location;
        }

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
  • 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-07T05:44:11+00:00Added an answer on June 7, 2026 at 5:44 am

    You could pass the Activity you want to be notified to MyLocationListener as a Listener. Create your own Listener interface, let the Activity implement it and add a method to MyLocationListener like addListener(). Every time you want to notify those activities iterate through the list of Listeners and call their locationChanged method (or whatever you called it in your interface definition). Just be sure to add error handling for null listener Activities, etc.

    So basically you have your own custom Listeners listening to a LocationListener.

    Another way would be to use Broadcast Receivers and broadcast the location change.

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

Sidebar

Related Questions

I am developing RESTful with php. Currently I have started coding with MVC design
We're coding a Java6 project under Eclipse Indigo and we currently have some compilations
I'm coding Settlers of Catan for iOS and currently have my data tiles in
I have a minor concern: I'm currently coding a sign up form and I
I have a DOB column in a table that is currently in use. See
I have to compare(>,<,==) two class object based upon different criteria, explained below. class
Currently I'm hard coding another object directly when needed and I'd like the ability
I'm currently coding my design portfolio and have encountered a problem with the layout.
Currently have a drop down menu that is activated on a hover (from display:none
Currently have password protection on my main and sub directories, however I'd like to

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.