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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:34:36+00:00 2026-06-13T13:34:36+00:00

I have a confusion between Service and BroadcastReceiver. I am working on a Location-based

  • 0

I have a confusion between Service and BroadcastReceiver. I am working on a Location-based app. In this, I want to store user’s location into database whenever there is a significant change in his/her location(eg. 10 meters).

I am using LocationListener for it and it is working fine. But my dilemma is : where to write the onLocationChanged() method – In the onReceive() method of BroadcastReceiver or in a Service?

And if I am using a Service, then in which method of Service , shall I write the below code?

Here is my Location Listener:

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            if(location != null){
                //Code to populate location-data into the database table.
            }

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

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5, 10, locationListener);

Can anyone please suggest me what is the better way to implement it? I have read all the Android Developer Docs about both Service and BroadcastReceiver , but I am still not able to differentiate between their usage?

  • 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-13T13:34:37+00:00Added an answer on June 13, 2026 at 1:34 pm

    In my opinion, it depends on how much work you want to do.

    If you want to do something small when you receive a broadcast, then you can do it in onReceive() method of your BroadcastReceiver. BUT, onReceive() is called on the main application thread. So, Android OS will kill it if it takes too long.

    Thus, if you want to do something that might take a while, you should do it in Service on a background thread. As it was suggested, you can put your code in the service’s onStart() method. Don’t keep the service running if it does not do anything (waist of battery and other resources). Start service every time you receive a broadcast and finish it as soon as it has completed the assigned job.

    P.S. Mark L. Murphy (aka CommonsWare) has several excellent books about Android development. Google Busy Coder’s Guide Android Development.

    UPDATE:

    About your desire to cancel your background task when your applications goes to background.

    Consider what you will do if your background task is interrupted in the middle. You need to handle this situation. If you are OK with that, then you can use either AsyncTask or Service to do your task in background. Your choice again depends on your task’s size and type.

    AsyncTask is generally intended to perform background operations and publish results on the UI thread. Android documentation suggests that tasks for AsyncTask should not exceed few seconds. On the other hand, Service is designed to perform long operations without communications with UI.

    About canceling. AsyncTask will be canceled when your activity is finished as it is tired to the UI thread. To stop service you will need to call stopService() from your activity.

    As you need to store location information frequently, you can start service when you activity starts, communicate with it making use of the service’s onBind() method, and then stop your service in the onPause() method of your activity.

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

Sidebar

Related Questions

I have been into C# and always feel confusion between what should be selected
I am writing a location based App and want to get some wisdom on
I have a confusion between two B keywords. The official B reference manual uses
Based on an answer from a candidate I have a confusion regarding the functioning
Possible Duplicate: difference between WCF Services and Web Services and REST Service I have
I Have an asp.net web service that uses an oracle database. It works when
I have a little confusion regarding difference between ORM and PDO? Is PDO a
I have some basic questions related to the conversion of Web Service (.asmx) into
I have some confusion regarding Events . What are the basic differences between C#
I have a query below. I want get items between 4 and 6 so

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.