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?
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 yourBroadcastReceiver. 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
Serviceon a background thread. As it was suggested, you can put your code in the service’sonStart()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
AsyncTaskorServiceto do your task in background. Your choice again depends on your task’s size and type.AsyncTaskis generally intended to perform background operations and publish results on the UI thread. Android documentation suggests that tasks forAsyncTaskshould not exceed few seconds. On the other hand,Serviceis designed to perform long operations without communications with UI.About canceling.
AsyncTaskwill be canceled when your activity is finished as it is tired to the UI thread. To stop service you will need to callstopService()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 theonPause()method of your activity.