NOTE: Introduction part is directly from the android developers site http://developer.android.com/guide for more details read it there.
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 that can be invoked individually. For example activity that provide one screen UI(User Interface) and service that do some work in background.
From one component another component or call different activity of separate App using intent.
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 the Java programming language. 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,for example).
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). The manifest file must have all the App’s component be declared in it. 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"> <manifest ...> <application android:icon="@drawable/app_icon.png"...> <activity android:name="com.example.project.ExampleActivity" android:label="@string/example_label" ...> </activity> ... </application> </manifest>
<application> element, the
android:icon attribute points for an icon that identifies the App.
<activity> element, the
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 for activities
<service>elements for services
<receiver>elements for broadcast receivers
<provider>elements for content providers
Activities, services and content providers that are included in the source but not declared in the manifest are not visible to the system and can never run. 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:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.app.myapp"> <uses-permission android:name="android.permission.RECEIVE_SMS" /> ... </manifest>
We will learn about all the components of android in details in later chapters where all these topics will be covered in more details…
- 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. It is also used to declare the permissions and requirements of the App to the android system.