问题
Please correct me If I am wrong :
1) A Service is used to perform long tasks in background. A service runs in the UI thread so if there comes a long task then it may freeze our UI. A service will continue to run independent of application as long as we tell it to stop.
2) An IntentService on the other hand is used to perform short tasks in separate thread. It automatically terminates when it finishes its task.
What I have to do :
1) check for location every 5 seconds
2) if there is a change in location send it to server and Update the UI with new location values
What confuses me :
Should I use a Service or IntentService as I need to do it continuously after 5 seconds and does not want my UI thread to become unresponsive.
This app will be used to track a vehicle.
回答1:
I wouldn't use an IntentService
because it finishes itself once the job is done and you would need to re-schedule it again after 5 seconds. To re-schedule it you would need either some complicated external Timer
mechanism associated with an application Context
or, even worse, use AlarmManager
that will suck your battery like crazy.
I would use a Service with a Timer
inside for scheduling TimerTasks
each 5 seconds and on each TimerTask
that anyway executes on a worker thread I would get the position and make an Http request.
Just don't forget to cancel
the timer on Service's onDestroy
method otherwise you'll leak the Service
instance.
EDIT
I just noticed this and Update the UI with new location values
... Keep using the Service
, but either use an AsyncTask
for sending the request in doInBackground
and then send a broadcast message in onPostExecute
, either keep using the same TimerTask
mechanism but use a Handler
that is instantiated with a UI Looper and make UI update requests on that handler.
回答2:
1) A Service is used to perform long tasks in background. A service runs in the UI thread so if there comes a long task then it may freeze our UI.
yes, but when you use service for some long task then you create thread inside it. One very important feature of services is that android system is less likely to kill your app if your if there is low memory. And if you use setForeground then it will kill your app even more less likely.
A service will continue to run independent of application as long as we tell it to stop.
it will run in your application process, so you cant tell it will run inependently, you can of course stopp it from your app, or service can stop itself if it finished its work.
2) An IntentService on the other hand is used to perform short tasks in separate thread. It automatically terminates when it finishes its task.
IntentService extends Service and makes implementing Service a lot easier, it runs your code on its own worker thread. It will terminate if there is no more queued work to do.
Should I use a Service or IntentService as I need to do it continuously after 5 seconds and does not want my UI thread to become unresponsive.
it greately depends on your task, if your job is triggered from UI by some function then go with IntentService. Bare services are rally for really long tasks, ie. like mp3 players, or if you need constant communication with server.
If your task can be issued while your app is in background, ie. it is triggered by BroadcastReceiver, then you should look into wakefull intentservice:
https://github.com/commonsguy/cwac-wakeful
it will keep your app alive for the time intentservice will process your job, otherwise android is allowed to kill your app (after broadcast onReceive returns).
回答3:
It should be IntentService
which you can schedule using AlarmManager
I have done something similar in this project:
https://github.com/madhur/MapMyLocation/tree/develop/src/in/co/madhur/mapmylocation
You may check it out
回答4:
IntentService
derives from Service
and just creates a thread for you and manages the lifecycle.
You should use an IntentService
in combination with the newer Android Location Strategies. In particular if you are checking user location, you can use a geofence and let the OS do the work for you - and save battery.
回答5:
About the location
there is a location distance update -> i.e if location of phone changes for more than X meters, then send the update, DO NOT ASK FOR IT EVERY 5 SECONDS , if you are in a moving vehicle, that X meters will go by fast, if you are walking, you WILL NEVER move over 5 seconds more than 3-5 meters - people will complain that your app kills their battery
Also - A service will only continue running when your app is not running, if you start it sticky. otherwise, when your app is destroyed from the application queue, so will be the service.
requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)
Register for location updates using a Criteria and pending intent.
void requestLocationUpdates(long minTime, float minDistance, Criteria criteria, LocationListener listener, Looper looper)
Register for location updates using a Criteria, and a callback on the specified looper thread.
void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
Register for location updates using the named provider, and a pending intent.
void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
Register for location updates using the named provider, and a callback on the specified looper thread.
void requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)
Register for location updates using the named provider, and a pending intent.
Location api
回答6:
If you don't want to use any service than you can use feature of location API. you have to register your location manager providing latest location. If you need to use one of the service than
IntentService
is best option for you.
locationManager.requestLocationUpdates(provider,
5000 ,
10, new myLocationListener());
Here is your location listener class.
public class myLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// Here you can get latest location and update to server
//location object provides you to latest location.
}
@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 onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Please mark accept to this answer if it is helpful to you.
来源:https://stackoverflow.com/questions/22035677/should-i-use-service-or-intentservice-for-my-android-app