Please explain an Android Service. How does it differ from an Activity? Does it depend on an application state such as running in the foreground/background?
Please explain an Android Service . How does it differ from an Activity ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
From the Android Developer’s SDK reference for Service:
It is very important to note
This is in contrast to an activity which is best understood as something a user directly sees and interacts with (a UI.)
A service, as mentioned above, can be used for longer-running operations that will continue even if you have no foreground activity, but they can, and eventually will be killed by Android’s lifecycle if left in the “background” state. If you need your service to continue running as a single instance without being killed and restarted, I would recommend placing startForeground(int id, Notification notification) in your Service’s
onCreatemethod and stopForeground(boolean removeNotification) in your Service’sonDestroymethod.For example, I have an app that uses a
foreground Serviceto record accelerometer data all night while the android device is next to the user’s body. Though it is not required to be active, I also have anActivitythat broadcast anIntentto aBroadcastReceiverinside theServicewhich tells theServicethat it should also broadcast anIntentwith accelerometer data as extras to aBroadcastReceiverinside theActivity.Code:
SleepActivity
SleepAccelerometerService
Good luck and let me know if you need any more information!