Today we continue to learn an overview of how to organize an application in Android, with this overview, we’ll know an app in Android is held by which composition, which component is important and how the components are arranged inside an application.
1. The basic components in an android app:
An Android application is made by one of the Application components. Each component provides a different way to work with the operating system application. Each component exists as a separate entity and play a certain role in helping define the overall behavior of your application. There are 4 different types of Android components.
- Content Providers
- Broadcast Receivers
Three of the four component mentioned above here: Activities, Services and Broadcast Receiver is executed by an Intent. Intent is an asynchronous message that we will examine later
A Manifest file used to declare the system components and declare the requirements of the application.
Finally, Android Applications must include Resources, resources are separate from the source code portion of the application, allowing the application to use the images, sounds, fonts, … enrich the requirements of applications .
- Activity represents a single screen with a user interface in which users can view and interact. For example, an email application displays a list Activity emails, a different Activity provides an interface for composing email, and other Activity provides an interface to read email, …
- Although these Activity are closely linked in the email application, in order to bring the best experience to the user, but the fact they are completely independent of each other. Other applications are possible launch the Activity that we defined, of course, in case our email application allows.
- Each application has a “main activity”, this is the Activity will be displayed first when the application is launched, an application will have a main activity and others normal activity.
- An Activity may launch another Activity. Each time a new Activity is launched, earlier Activity was stopped and the system will save in it in a back stack. The return to the previous Activity (when users click the back button on the device), the system simply grab the Activity stored in the back stack, and Activity was taken in the back stack continue to be displayed on the user screen. Back stack was held under properly criteria of a stack (like “last in, first out”). Reference http://en.wikipedia.org/wiki/Stack_(abstract_data_type).
- To envision more clearly on the back stack works with Activity, let see the following diagram.
- In the diagram above, when the application is launched, a main activity initially will be activated, such as on the main activity was seen Activity 1, Activity 1 will be automatically stored in back stack and in the form of foreground , which is being displayed on the screen. Then Activity 1 will start Other Acitivity (due to user behavior click any button, or Activity 1 automatically start Activity 2 under preset timer) like we see Activity 2 is shown in the foreground, Activity 2 will replace Activity 2 and will display on the screen. Activity 1 will temporary stop and will be stored in back stack. Then Activity 2 re-start Activity 3. Activity 3 will serve as the foreground, while Activity 2 and Activity 1 will continue stored in back stack. At this point if the user clicks the back button on the device, then Activity 3 will be removed from the stack, at this point, the new Activity 3 is destroyed and not under the management of back stack. The role of foreground will display on the screen was ceded Activity then be put back at the previous stack Activity 2. If the user continues to press the back button, Activity 1 will be show again in Foreground and Activity 2 will be cancelled. If the user continues to hit back again then Activity 1 also be canceled from stack back, this time an application is closed (deactivated). References: http://developer.android.com/guide/components/tasks-and-back-stack.html
- A Service is a component run in the background, in order to implement long-term operation or to perform work for the handle remote (remote processes).
- Service does not provide a user interface. For example, a service can play music while the user is using another application.
- Another component (application component) such as an activity could start a service so that it runs independently or linked to interact with it.
- A service is basically 2 types:
- When booting up, the service will operate in background indefinitely, even launching its components ended.
- Usually a started service only be assigned to a single processor and not returning to the component called it.
- A bound service provides a client-server interface to allow the components can interact with each other.
- Time life of bound service a component depends on links (bound) with it, it means that the component link exists is bound service exists and vice versa.
- Many components can be linked with a bound service at a time, but when all these components are discontinued (unbind), then this type of service is canceled under.
4. Content Providers:
- Content Provider manages a set of shared data applications. Through Content Provider, other programs can access or even change the data of an application (if Content Provider allows).
- For instance the Android OS provides a Content Management Provider phonebook. So that the application (with the proper permissions) can read or add information to this phonebook.
- Content Provider is very useful for both read and write data to the application’s private and not shared out.
5. Broadcast Receivers:
- Broadcast Receiver is a component used to answer the messages transmitted from the system.
- In the process of operating mobile devices, there are many different messages notification system such as the display is turned off, the battery is low, taking a picture is finished, …
- A Broadcast Receiver does not create a user interface, but it can generate a notification (notification) on the status bar (status bar) to alert users when a certain event occurs.
- Three of the four types of application components (activity, service, broadcast receivers) are launched by an asynchronous call, we call Intent.
- Intent creates a linked basis between the components in the application together.
7. File Manifest:
- File Manifest is the place your application will be presented to the system that “what is in this application” such as:
- User permissions registed, are the rights that users have permission to use the features of the system, such as the right to reach the Internet, be read phonebook (contacts), recorded file to memory card, …
- Specify Minimum Required SDK, is the operating system of Android lowest that our application support, this parameter is generated when we create a new application, then we have shown Minimum Requied SDK and parameters we assign that are saved in this attribute of Manifest file.
- Specify hardware features and software applications we want to use, such as camera, bluetooth, or even multitouch features.
- Some external libraries (external here can understand that the library is not available in our Framework APIs) that applications have used to, such as Google Maps library, Google Play, …
- When creating a new application, ADT will automatically create a file AndroidManifest.xml us, we can follow the steps below to view a particular file will look like Manifest.
- In Eclipse, go to the Package Explorer window, navigate to the Hello World project, we seek Manifest file in the directory tree of the Hello World project, as shown below, double-click the file to open it up Manifest.
- Any xml file, including manifest.xml file and the resource file are also default xml originally opened up with a screen has multiple tabs below, the purpose of this screen is going to give us there are more options to review and customize the content of the xml file. Often the first tab will provide some of the editing interface, and the final tab will be the xml code for us to edit handmade crafts.
As I mentioned above, we look at the sequence tab and click on the tab below to open the code xml AndroidManifest.xml the review. I advise you to choose each tab and previous overview of how each tab, because depending each will choose a style of work later, for me, I like to edit XML code, but you can use other more effective ways to better yourself.
Tag in AndroidManifest.xml:
- <manifest> is the original tag. Key attributes that you need to be interested in this tag is the package. Like Minimum Required SDK package appears after you create a new application, you have to declare a package name and the name we have only indicated when it will be saved in this case of manifest file. Package shown here is the the original name of the application, to reach where you need to provide a new class in any other place in the Manifest file, you just put a dot (.) replaces both paths original package. As the example above, instead of providing the full name is the MainActivity android: name = “com.example.reminder.MainActivity” we just summarized is the android: name = “. MainActivity”.
- <uses-sdk> determine the version of the operating system to run the program. As well as the attributes above, the properties of this field is also generated after the value we assign Requires Minimum Target SDK and SDK when creating a new project.
- <application> is the most important tag in this file. It specifies the components in an application.
- android: icon attribute specified icon images used for our application, you can change the path to the application’s icon by changing the image name in this field.
- android: label attribute indicates the text that will be used as the name of the application, the 2 properties listed above are all depending on you create an application, when you assign a name and an icon Application for it .
- <activity> declare an Activity, it includes the following key attributes:
- android: name declaration name and path to an Activity, of course this path is the package that there are in the Activity, and we may not need to declare the full package name, but simply represented by an asterisk (.) if Activity package’s name coincides with the package that was declared in the <manifest> as mentioned above.
- android: label specifies the name for Activity, this name will appear on the title bar when the activity of this application displays
- <intent-filter> determine if the conditions when the card lying Activity is shown. The tag containing it is the determined Activity Main Activity program and will be the first appearance Activity when users start the application.
- An application must be mention the resource, which is the image, font, color, sound … used to add richness and aesthetic applications. In this review we find out a summary of the resource, specifically how it will be clarified in future articles.
- Normally resource concept in Android be understood as something separate from the source code, source code is the contained java code and where the logic of the application, while the resource is the what the rest of the application is not source code.
- Resource is contained in the folder res / (you can look at the Package Explorer to see this Resource folder), in this directory there are many other directories help the resource group by type and according to the features that resource support.
- Specifically, each of the resource categories and features are detailed in the following article, here we only mention two major resource types.
- Default resources: are all the resource that we do not care to show how different equipment, which will display a default resource like what it is offered in all facilities.
- Alternative resources: is the the resosurce will depend on the specific equipment we’ve specified, for example we want applications to run on devices with the default language is English will have to load the English text, picture British flag