All Questions
Sticky Intents allows communication between a function and a service.
sendStickyBroadcast() performs a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter).
Intents are messages that can be used to pass information to the various components of android. For instance, launch an activity, open a webview etc.
Two types of intents-
- Implicit: Implicit intent is when you call system default intent like send email, send SMS, dial number.
- Explicit: Explicit intent is when you call an application activity from another activity of the same application.
We need to register a LocalBroadcastReceiver in the activity. And send a broadcast with the data using intents from the background service. As long as the activity is in the foreground, the UI will be updated from the background. Ensure to unregister the broadcast receiver in the onStop() method of the activity to avoid memory leaks. We can also register a Handler and pass data using Handlers. I have detailed a sample implementation on this.
Serializable is a standard Java interface. Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient than Serializable (The problem with this approach is that reflection is used and it is a slow process. This mechanism also tends to create a lot of temporary objects and cause quite a bit of garbage collection.).
Serialization Serialization is the process of converting an object into a stream of bytes in order to store an object into memory, so that it can be recreated at a later time, while still keeping the object's original state and data.
How to disallow serialization? We can declare the variable as transient.
Creating and destroying threads has a high CPU usage, so when we need to perform lots of small, simple tasks concurrently, the overhead of creating our own threads can take up a significant portion of the CPU cycles and severely affect the final response time.
ThreadPool consists of a task queue and a group of worker threads, which allows it to run multiple parallel instances of a task.
AIDL is for purpose when you've to go application level communication for data and control sharing, a scenario depicting it can be : An app requires list of all contacts from Contacts app (content part lies here) plus it also wants to show the call's duration and you can also disconnect it from that app (control part lies here).
In Messenger queues you're more IN the application and working on threads and processes to manage the queue having messages so no Outside services interference here.
Messenger is needed if you want to bind a remote service (e.g. running in another process).
A bound service is a service that allows other android components (like activity) to bind to it and send and receive data. A bound service is a service that can be used not only by components running in the same process as local service, but activities and services, running in different processes, can bind to it and send and receive data.
When implementing a bound service we have to extend Service class but we have to override onBind method too. This method returns an object that implements IBinder, that can be used to interact with the service.
Implementing Android bound service with Android Messenger
Service based on Messenger can communicate with other components in different processes, known as Inter Process Communication (IPC), without using AIDL.
A service handler: this component handles incoming requests from clients that interact with the service itself.
A Messenger: this class is used to create an object implementing IBinder interface so that a client can interact with the service.
Hot Questions
How to Get the Size of all Tables in a database in MySQL?