Android Hacking Notes
Android Service
A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. There are 3 kind of service:
- Foreground Service. A foreground service performs some operation that is noticeable to the user.
- Background Service. A background service performs an operation that isn't directly noticed by the user.
- Bound Service. A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC).
By default, service will run on the main/UI thread if we don't explicitly specify the worker thread.
Foreground Service
- Typical use case : Music Player keep playing the sound although UI not active
- The term “Foreground” is about PRIORITY. Android will try its best not to kill this service when system becomes low on resource.
- There are valid scenarios to promote your app to the foreground: The prerequisites for using a foreground service are that your app is executing a task that is immediate, important (must complete), is perceptible to the user (most often because it was started by the user), and must have a well defined start and finish. If a task in your app meets these criteria, then it can be promoted to the foreground until the task is complete. … playing music, completing a purchase transaction, high-accuracy location tracking for exercise, and logging sensor data for sleep. The user will initiate all of these activities, they must happen immediately, have an explicit beginning and end, and all can be cancelled by the user at any time.1)
- Apps that target Android 9 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission.
- If your app targets Android 10 (API level 29) or higher and accesses location information in a foreground service, declare the location foreground service type as an attribute of your <service> component.
- If the user has granted the ACCESS_BACKGROUND_LOCATION permission to your app, the service can access location all the time. Otherwise, if the user has granted the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission to your app, the service has access to location only while the app is running in the foreground (also known as “while-in-use access to location”).
Background Service
Bound Service
A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
Fragment Lifecycle Event Sequence
- onAttach
- onCreate
- onCreateView
- onViewCreated
- onActivityCreated
- onViewStateRestored
- onStart
- onResume
- onPause
- onStop
- onDestroyView
- onDestroy
- onDetach