Android provides a rich application framework that allows you to build Apps and games for mobile devices in Java language environment. If you are new to java or want’s to brush your java little bit, I’ll suggest to read check Java for Android tutorial first.
Apps provide multiple entry points
Android Apps are a combination of different components. User or system can invoke these components directly to enter the app. For example, user can launch the app from launcher or clicking on notification in the status bar.
Apps adapt to different devices
Android provides an adaptive App framework that allows developer to provide unique resources for different device configurations. For example different layout XML for different screen size. Apps can query availability of different features of the device at run-time.
Android Apps are written in Java. The Android SDK tools compile the code, data and resource files into APK (Android package). one APK file contains all the content of and App.
Each App in Android lives in its own sandbox. Each App runs on its own virtual machine(VM) so that App can run isolated from other Apps. Every App runs in its own Linux process.
Android system works with the principle of least privilege. That is, each App has access only to the components that it requires to do its work and no more.
There are ways for an App to share data with other Apps either by sharing Linux user id between Apps and Apps can request permission to access device data such h SD card, contacts etc.
App Components are building blocks of an Android App. Each Components plays a specific role in an Android App. There are four different types of App components. Each type serves distinct purpose and has distinct life-cycle(defines the flow of how and when the component is created and destroyed). Here are the four types of app components:
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 can have one activity to list all the emails, an other activity to show email contents, yet another activity to compose new email. All the activities in an App work together to create perfect user experience.
A Service runs in background to perform long-running operations or to perform work for remote processes. A service does not provide any user interface it runs only in background with User’s input. 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 user’s interaction with the Android device.
A content Provider manages shared App data. There are four ways to store data in an App, data can be stored in the file system, in and SQLite database, on the web, or any other persistent storage location the App can access. Through content providers, other Apps can query or even modify the data(if content provider allows it). For example, Android system provides a content provider that manages the user’s contact information so that any App that has the permission can query device contacts. Content providers can also be used to save the data which is private to the App for better data integrity.
A broadcast receiver responds to system-wide broadcast of announcements, these announcements can be originated from the system – for example, a broadcast announcing that the screen has turned off, the battery is low etc. or 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 any user interfaces but they can show 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.
A unique aspect of the Android system is that any App can start another App’s component. For example, if you want to make call, send SMS, open a web page or click a photo, there are another App that does that and your App can use it, instead of developing and activity to capture a photo inside your App.
When the system starts a component, it starts the process for that App(it it’s not already running, i.e. only one process per App can run at any time in an Android system) and instantiate the classes need for that component. Thus the component runs on the process of that App that it belongs to, not on your Apps process. Therefore, unlike Apps on most other system, Android Apps don’t have a single entry point(there is no main() method).
Because the system runs each App in a separate process, one App cannot directly activate other App’s component, however Android system can. Thus to start another App’s component one App must send a message to the system that specifies your intent to start that component, and then the system will start that component for it.
Instances of the class
android.content.Context provide the connection to the Android system which executes the application. Instance of Context is required to get access to the resources of the project and the global information about the App environment.
Lets have easy to digest example, Consider You are in a hotel, you want to eat something, ask for room service, etc. You ask the room-service person to bring you things or clean up things for you. Now think this hotel as an Android App, yourself as an Activity then the room-service person is your context which provide you access to the App resources like room-service, food items etc.
Yet an other example, Lets you are in a restaurant sitting on a table, each table has an attendant, when ever you want to order food items you ask the attendant to do so. The attendant then places your order and your food items gets served on your table. Again 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 thus giving you a way to access the resources like food items.
Activating any of the above component requires the Context’s instance. Not just only the above, almost every system resource, either creation of UI using views(discussed later), creating instance of system services, starting new activities or services etc all require Context.
Three of the Four component types – activities, services and broadcast receivers – are activated by a message called an intent. Intents are the messengers that requests an action from the system or from other components, whether the component belongs to your App or another.
An intent defines a message to activate either a specific component or a specific type of component like open web page, then all components in the system that can open a web page are shown by the action picker.
For activities and services, an intent defines the action to perform(for example, to
send something). For example, an intent might convey a request for an activity to show an image or to open a web page. In some cases, you can start an activity to receive a result, the activity also returns the result in an Intent.
For broadcast receivers, the intent simply defines the announcement being broadcast.For example a broadcast to indicate the device battery is low.
Content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver. The content resolver handlers all direct transaction with the content provider so that the component that’s performing transaction on the provider doesn’t need to know anything about the provider, it provides a layer of abstraction.
There are separate methods for activating each type of component:
- You can start an activity(or give it something new to do) by passing an Intent to startAcitivty() or startActivityForResult() (when you want the activity to return a result).
- You can start a service(or give new instruction to an ongoing service) by passing an Intent to startService().
- You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
- You can perform a query to a content provider by calling query() on a ContentResolver.
The Manifest File
Every App in Android system must have a
manifest file). All the app components must be added in the Manifest file. Thus before starting a component, the Android system must know that the component exists by reading this file.
The manifest file has declaration of many things addition to App’s components, such as:
- Identify any user permissions the pp requires, such as Internet access or read-access to user’s contacts or write access on SD card.
- Declare the minimum API Level required by the App.
- Declare hardware and software features used or required by the App, such as Camera, Bluetooth, Multi-Touch screen etc.
- And more…
The primary task of the manifest is to inform the system about the App’s components. For example, a manifest file can declare an activity as follows:
<? xml version="1.0" encoding="utf-8">
android:icon attribute points for an icon that identifies the App.
android:name attribute specifies the fully qualified class name of the Activity subclass and the
android:label attribute specifies a string to use as the user-visible label for the activity.
You must declare all App components this way:
- Activity elements
- Service elements
- Broadcast receivers elements
- Content providers elements
Activities, services and content providers that are included in the source but not declared in the manifest are not visible to Android System and therefore cannot be launched. However, broadcast receivers can either declared in the manifest or created dynamically in code and registered with the system by calling
A basic Android App has no permissions associated with it by default like Internet access or SD card access etc. To make use of protected features of the device, you must include permissions in AndroidMenifest.xml using tag
For example, an App that needs to monitor incoming SMS messages would specify:
<? xml version="1.0" encoding="utf-8">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
- Android Apps have multiple entry points.
- Android system works on the principle of least privilege.
- There are four different types of App components: Activities, Services, Content Providers and Broadcast Receivers.
- An Activity is a single Screen with UI.
- A service runs in background to perform long running operations or work for remote process without UI.
- A Content Provider manages shared App data.
- A Broadcast Receiver responds to system-wide broadcast of announcements.
- An Intent defines a message to activate either a specific type of component or a specific component.
- Activities, Services and Broadcast Receivers are activated by intents but Content Providers are activated by ContentResolvers.
- Every App must have a AndroidMenifest.xml and it must contain entries regarding all the components in the App. Permissions and other app requirements are also declared in the Manifest file.
Hope you liked the post, if you have any query or you see any problem please let me know by commenting below.