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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:30:26+00:00 2026-06-06T01:30:26+00:00

I want to implement a LocationListener . Checked some tutorials and found this: /**

  • 0

I want to implement a LocationListener. Checked some tutorials and found this:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          makeUseOfNewLocation(location);
        }

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

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


}

But are event listeners really added in the onCreate method? Looks pretty messy to me. Is it more common to add them to a separate class and create an instance of the class in onCreate? I would like to know the best practices here.

Thanks!

  • 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-06T01:30:33+00:00Added an answer on June 6, 2026 at 1:30 am

    Your approach is almost correct but step by step, there is no “good” reason to implement LocationListener in separated class but you should implement your LocationListener out of onCreate() method and

    requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    

    is generally called rather in onResume() method and removeUpdates() in onDestroy() method.

    I recommend to you check for example WeatherPlus application by CommonsWare and i think all will be clearer.

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

Sidebar

Related Questions

Inside public class IAmHere extends Activity implements LocationListener { i have @Override public void
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
i starter in jqgrid, i want implement inline edit in jqgrid i have this
I want to implement a simple script to do some boring housekeeping on my
I need add paging to panel, which populated with dynamically-created controls. I want implement
I have WSDL and XSD files and want implement a webservice based on this
I want to implement SQLite database for iPhone using PhoneGap. I know some basics
I tried to implement the code found in this link location find The truth
I have a problem with my locationListener. This is my MyLocationListenerNetWork: public class MyLocationListenerNetwork
i want implement geolocation notification like the app reminders. this is what i have

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.