Reputation: 2624
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?
Upvotes: 3
Views: 324
Reputation: 7797
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.
Upvotes: 1
Reputation: 11211
Services are used to run your code in background, are used mainly to download, music play.. after the user puts the app in background(presses home or back button).
BroadcastReceivers
are used to receive messages sent through sendBroadcast()
method and are used to catch system notifications like Bluetooth, internet availability, get messages from other apps...
I think you want to use Services here because BroadcastReceiver
can't help you to listen a location change if the app is in background.
Upvotes: 2
Reputation: 121
It depends on your purpose. If you want to catch the location in the background, use in Service.
BroadcastReciever's onReceive method will call only when what action you registered to receive.
If you want to do in Foreground, you can simply use it in the Activity. But when your activity is closed, you can't get the locations.
So, you decide according to your functionality.
Upvotes: 2