Application Fundamentals

suggest change

Android Apps are written in Java. The Android SDK tools compile the code, data and resource files into an APK (Android package). Generally, one APK file contains all the content of the app.

Each app runs on its own virtual machine (VM) so that an app can run isolated from other apps. The Android system works with the principle of least privilege. Each app only has access to the components it requires to do its work, and no more. There are ways for an app to share data with other apps, such as by sharing a Linux user ID between apps, or an app can request permission to access device data such as the SD card, contacts, and so on.

App Components

App components are the building blocks of an Android app. Each component plays a specific role, serves a distinct purpose, and has a distinct life cycle (how and when the component is created and destroyed). Here are the four types of app components:

  1. Activities: An activity represents a single screen with a user interface (UI). An Android app may have more than one activity. (For example, an email app might have one activity to list all the emails, another to show the contents of each email, and another to compose a new email.) All the activities in an app work together to create the user experience (UX).
  2. Services: A service runs in the background to perform long-running operations or to perform work for a remote process. A service does not provide a UI. (For example, a service can play music in the background while the user is in a different app, or it might download data from the internet without blocking the user's interaction with the device.)
  3. Content providers: A content provider manages shared app data. There are four ways to store data in an app: it can be written to a file and stored in the file system, inserted or updated in a SQLite database, posted to the web, or saved in any other persistent storage location the app can access. Through content providers, other apps can query or even modify the data. (For example, the Android system provides a content provider that manages the user's contact information so that any app with permission can query the contacts.) Content providers can also be used to save data that is private to the app, for better data integrity.
  4. Broadcast receivers: A broadcast receiver responds to system-wide broadcast announcements (for example, a broadcast announcing that the screen has turned off, the battery is low, and so on) or broadcasts from apps (for example, to let other apps know that some data has been downloaded to the device and is available for them to use). Broadcast receivers don't have UIs, but they can show a notification in the status bar to alert the user. Usually broadcast receivers are used as a gateway to other components of the app, mostly activities and services.

One unique aspect of the Android system is that any app can start another app's component. (For example, if you want to make a call, send an SMS, open a web page, or view a photo, an app already does that and your app can use it, instead of developing a new activity for the same task.)

When the system starts a component, it starts the process for that app if it is not already running, and instantiates the classes needed for that component. The component then runs in the process of the app it belongs to. Unlike apps on other systems, Android apps don't have a single entry point (there is no main() method). An app can declare more than one process, but components run in some process of their own app.

Because the system runs each app in a separate process, one app cannot directly activate another app's components, but the Android system can. To start another app's component, an app sends a message to the system that specifies an intent to start that component, and the system starts it.

Context

Instances of the class android.content.Context provide the connection to the Android system that executes the application. An instance of Context is required to get access to the project's resources and to global information about the app's environment.

Here is an easy-to-digest example. Consider you are in a hotel and you want to eat something. You call room service and ask them to bring you things or clean things up for you. Think of this hotel as an Android app, yourself as an activity, and the room-service person as your context, which gives you access to the hotel's resources such as room service and food.

Yet another example: you are in a restaurant sitting at a table, and each table has an attendant. Whenever you want to order food, you ask the attendant to do so. The attendant then places your order and your food is served at your table. In this example, the restaurant is an Android app, the tables or the customers are app components, the food items are your app resources, and the attendant is your context, giving you a way to access resources such as food.

Activating any of the components above requires a Context instance. So does almost every system resource: creating the UI with views (discussed later), creating system services, and starting activities or services.

More detailed description is written here.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents