How unit 2 is examined
This unit covers the building blocks of an Android app (activities, services, intents, receivers), the manifest that declares them, intent filters and permissions; no topic was asked in the supplied papers, so each is kept short.
Anatomy of an Android applications
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>An Android application is a set of loosely coupled components (activities, services, broadcast receivers and content providers) plus resources, all declared in AndroidManifest.xml and packaged in one APK.</mark>
Key points.
- An Activity is one screen with a user interface, and a Service runs in the background without a UI.
- A Broadcast Receiver responds to system or app announcements, and a Content Provider shares data between apps.
- The res folder holds layouts, strings and images, and the src folder holds the Java or Kotlin code.
- The manifest lists every component and the permissions the app needs.
Android terminologies
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Android terminologies are the standard terms used to build apps: View, ViewGroup, Context, Intent, Activity, Fragment, Service, Manifest and APK.</mark>
Key points.
- A View is a basic UI element such as a Button or TextView, and a ViewGroup is a container of views such as LinearLayout.
- A Fragment is a reusable portion of UI that lives inside an activity.
- The APK is the installable package, and the Dalvik or ART runtime executes the compiled code.
- Resources are external files (layouts, strings, drawables) referred to through the generated R class.
Application Context
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Context is an abstract class giving access to application-specific resources and system services, and to operations such as starting activities and broadcasting intents.</mark>
Key points.
- The Application Context lives as long as the app process and is obtained with getApplicationContext().
- The Activity Context is tied to one activity's lifecycle and is needed for UI work such as dialogs.
- Holding an Activity Context in a long-lived object causes a memory leak, so use the Application Context there.
- Context is used to get resources, start services, read preferences and access databases.
Activities
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>An Activity is a single focused screen of an app that hosts the user interface and handles user interaction.</mark>
Key points.
- An activity is a subclass of Activity (or AppCompatActivity) and is declared in the manifest.
- Its lifecycle callbacks are onCreate, onStart, onResume, onPause, onStop, onRestart and onDestroy.
- onCreate sets the layout with setContentView, and onPause is where unsaved data should be saved.
- Activities are kept on a back stack, so Back returns to the previous one.
Services
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A Service is an application component that performs long-running work in the background and provides no user interface.</mark>
Key points.
- A started service is launched with startService() and runs until it calls stopSelf() or is stopped.
- A bound service is attached with bindService() and offers a client-server interface, ending when all clients unbind.
- A service runs on the main thread by default, so heavy work needs its own thread.
- Typical uses are music playback and file downloads, and every service must be declared in the manifest.
Intents
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>An Intent is a messaging object that requests an action from another component, used to start activities, start services and deliver broadcasts.</mark>
Key points.
- An explicit intent names the target class, for example new Intent(this, Second.class), and is used inside one app.
- An implicit intent gives an action and data, such as ACTION_VIEW with a URL, and the system picks a matching component.
- Extras carry data with putExtra() as key-value pairs.
- startActivityForResult() (now the Activity Result API) returns a result to the caller.
Receiving and Broadcasting Intents
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A Broadcast Receiver is a component that receives broadcast intents, sent with sendBroadcast(), from the system or from other apps.</mark>
Key points.
- A receiver extends BroadcastReceiver and overrides onReceive(Context, Intent).
- It is registered either in the manifest with a receiver element or at run time with registerReceiver().
- Examples of system broadcasts are BOOT_COMPLETED, BATTERY_LOW and airplane-mode changes.
- onReceive must finish quickly because it runs on the main thread.
Android Manifest File and its common settings
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>AndroidManifest.xml is the mandatory XML file at the root of the app that describes the app to the Android system: its package, components, permissions and requirements.</mark>
Key points.
- The manifest element carries the package name and the application element holds the components.
- Each activity, service, receiver and provider is declared with its own element.
- uses-permission requests permissions, and uses-sdk (minSdkVersion, targetSdkVersion) sets the API levels.
- The launcher activity has an intent filter with MAIN and LAUNCHER.
Using Intent Filter
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>An intent filter is a manifest declaration that tells the system which implicit intents a component can handle.</mark>
Key points.
- It contains an action element, an optional category element and an optional data element (scheme, host, mime type).
- The system matches an intent only if its action, category and data all match the filter.
- An activity that must receive implicit intents includes the DEFAULT category.
- MAIN with LAUNCHER makes an activity appear on the home screen, and data filters enable deep links.
Permissions
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A permission is a restriction that protects sensitive data or actions, and an app must declare it in the manifest and, for dangerous ones, get the user's approval.</mark>
Key points.
- Normal permissions, such as INTERNET, are granted automatically at install time.
- Dangerous permissions, such as CAMERA and READ_CONTACTS, need runtime approval from Android 6.0 (API 23).
- The app checks with checkSelfPermission() and asks with requestPermissions().
- The user's choice arrives in onRequestPermissionsResult(), and the app must cope with denial.
Last-minute revision
- An APK contains components, resources and the manifest.
- The four components are Activity, Service, Broadcast Receiver and Content Provider.
- Activity lifecycle order is onCreate, onStart, onResume, onPause, onStop, onDestroy.
- A started service uses startService() and a bound service uses bindService().
- An explicit intent names the class, and an implicit intent names an action.
- Receivers are registered in the manifest or with registerReceiver().
- Every component must be declared in AndroidManifest.xml.
- MAIN plus LAUNCHER marks the entry activity.
- Dangerous permissions need runtime approval from API 23.
- Use the Application Context for long-lived objects to avoid leaks.
Memory hooks
- ASBC: Activity, Service, Broadcast receiver, Content provider.
- Intent is the postman and the intent filter is the address on the door.
- Normal permission: install and go; dangerous permission: ask and wait.
- Activity lifecycle: Create, Start, Resume, then Pause, Stop, Destroy.
Coverage checklist
- Anatomy of an Android applications: no past questions.
- Android terminologies: no past questions.
- Application Context: no past questions.
- Activities: no past questions.
- Services: no past questions.
- Intents: no past questions.
- Receiving and Broadcasting Intents: no past questions.
- Android Manifest File and its common settings: no past questions.
- Using Intent Filter: no past questions.
- Permissions: no past questions.