Created
October 18, 2013 17:54
-
-
Save gnuton/7045407 to your computer and use it in GitHub Desktop.
Android Services in a nutshell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Android services in a nutshell | |
| - Started when startService() is run by a component (activity or broadcast receiver) | |
| -> Service.onStartCommand() runs in the Service | |
| - Service stops when the service calls stopSelf() or a component calls stopService() | |
| - If a component calls bindService() and onStartCommand() is not called | |
| -> Service is stopped when all clients are unbound | |
| - Runs in the application thread unless you don't set a different process name in the manifest file | |
| - The more the service runs the more the service is susceptible to killing because | |
| it gets lower importance. A service is killed only when memory is required by the system. | |
| -> Service is restarted automatically by the system if killed. | |
| -> Service should works so be designed so that it can be restarted | |
| NOTE: the mask value returned by onStartCommand tells to the system how to handle service restarts | |
| - Services as activities are defined in the Manifest file <application> <service>...</service></application> | |
| --> DOC: https://developer.android.com/guide/topics/manifest/service-element.html | |
| <-- manifest allow you to set a custom icon, label, process and more for your service | |
| - If the service defines intent filters, other apps can launch the service if a intent matching the filter is | |
| argument of startService(). | |
| -> Set android:exported=False into Manifest file, if you want to define filters but you don't let other app | |
| launch your service | |
| - Service classes: | |
| 1. Service - base class - you need to create a separate thread where to run your code | |
| 2. IntentService - a worker thread handle START request one at time. | |
| -> onHandleIntent() is the only required method to implement | |
| - onStartService returns: | |
| START_STICKY (API 11), makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, | |
| <-- if service gets killed after start, the system can launch it but intent is null | |
| START_NOT_STICKY, makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explicit start themselves again later to do more work | |
| <-- if service gets killed after start, it can be restarted only by an intent sent by the app | |
| START_REDELIVER_INTENT, if service gets killed after start, the system resends the last intent to launch it. The intent is destroyed in the system queue only when the | |
| service calls stopSelf(int) | |
| START_STICKY_COMPATIBILITY, as START_STICKY, but works for API 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment