Introduction: Inter-/Intra-Process Communication
Author: Ian Darwin
Published? true --
Android offers a unique collection of mechanisms for inter- (and intra-) application communication. This chapter treats of the following:
- Intents specify what you intend to do next: either to invoke a particular class within your application, or, to invoke whatever application the user has configured to process a particular request on a particular type of data;
- Broadcast Receivers in conjunction with Intent Filters allows you to define an application as able to process a particular request on a particular type of data (i.e., the target of an Intent);
- AsyncTask allows you to specify long-running code that should not be on the "GUI Thread" or "main event thread" to avoid slowing the app down to the point that it gets "ANR" (application Not Responding) errors;
- Handlers allow you to queue up messages from a background thread to be handled by another thread such as the main activity thread, usually to cause information to update the screen safely.
Intents are commonly used to re-purpose existing components. For example, there is an Intent for sending email, which we have one recipe on in this chapter, and more details in later chapters. This will invoke the user's standard email application, or pop up a chooser if they haven't specified a default. This is usually the right choice, since the email will be coming from the user's device, it should appear to come from the user, so the user should see it and have a chance to read it before hitting Send.
There is another use case for email: sending private logs or debug messages to your own application server. While this can usually more readily be handled by a web service instead of email, there are cases where email makes sense for this purpose. In this case you typically do not want the user to be involved. A good solution here is to use the JavaMail API. We don't cover JavaMail in this book, but it is covered in a whole chapter of my previous Java Cookbook.