Android Application Component Learning

Source: Internet
Author: User

  

Today I have studied Google's explanation of Android Application Components. Find out the theoretical principles of Android application development!

At least I understand that to develop a software that can interact with mobile terminals driven by the Android system, I need to open the mouth of the Android system from those aspects so that it can talk to and communicate with your application, help your applications do things.

Think about it, develop an application and install it on the Android-driven terminal device to process the logic required to write some applications to achieve the goal, it mainly interacts with the Android system and the functional software on it, such as camera, Bluetooth, network, phone, Address Book, email, and so on. Some of them require hardware support. The problem arises. How do we write programs to interact with these things?

Many channels of Android sdks exist as application components. The general applications we compile are basically built on them.

Google says they are the bricks you build your applications. Don't underestimate these rock tiles, but they are agents of your apps and Android systems. Of course, some may not directly meet your application's needs to communicate with the system or other software, but its existence proves that it is meaningful, the role it plays must be understood.

 

Google provides four types of components that require detailed research by developers:

  

1. Activities:

Activity? In fact, we open a user interface on a mobile terminal to perform relevant operations. This is an activity. For example, we open the new email list of the email program to view the new email. This is an activity. Similarly, opening the email Writing Page is another activity, select a new email from the new email list page and open the browsing interface, which is a new activity. In this case, an email application has become a collection of so many activities!

In the Android system, these activities are independent from each other. Of course, users cannot feel this when using the email program. The combination of these independent activities gives users a good operation experience. So where is independence reflected? For example, for those activities in the email above, one other application can start any activity mentioned above with the email application's permission. That is to say, the email application can be broken down and run. What a magic! Because the Android system is based on the Linux kernel, the most unique design of the Android system is that any application can start components of other applications. Of course, the activity license is required first!

As a result, we can understand why we can enable the app for photos, configure a photo, and then directly share the email with our friends. On the surface, the software provides the email function, not actually! The camera program calls the email activity in the email program.

Activity gives us great flexibility. You only need to obtain their permission beforehand to use other activities that come with the system or in other applications as you like.

In fact, when writing Android applications, we first consider how to define the applications as one activity. Then, these activities are generated by instantiating the activity class provided by the android SDK.

 

2. Services:

Service! No system will reject it, because it always provides people with silent help, making every action easy and comfortable!

Of course, you must have an Android system! So what is the service?

Google said: it is a component that runs in the background to perform long-term operations or work silently for remote processes.

You may not see it because it does not provide a user interface! But you can feel the benefits of its existence.

For example, if you are interacting with an activity, another component is helping you download data from the network, but you are not aware of it. This component is a service!

To implement a service component, You need to define a subclass of the services class provided by Google, and obtain some necessary interfaces running on the Android system by inheriting the class.

 

3. Content Providers:

The content provider. Therefore, the container name is a component that can provide you with some content, which is equivalent to a container. What content does it provide? As mentioned above, every application in Linux runs in its own thread. Programs are isolated from each other, and security rules are not allowed to restrict mutual access. Therefore, android can start components of other applications to complete a certain function for itself, so how can we exchange the results, it probably depends on it!

Each of its instances manages an application's shared data set. To put it bluntly, it is a proxy or channel for you to access data and resources of other application components.

In the Android system, you can save the application data to the system file, integrate the data in an SQLite database, or put it on an online cloud server, or any application that can access the storage. Through this content provider, other applications can query and even modify the data as allowed by the content provider.

For example, the android system provides a content provider (contactscontract. data) manages the user's address book information, so that any licensed application can query the content provider to read and write back information about a specific person.

The Android system provides a contentprovider class for developers. To use the content provider, you must inherit this class. At the same time, you can implement a standard transaction API to enable transaction processing capabilities.

 

4. Broadcast recievers:

It is actually a gateway or channel component of a system and other applications in the Android system. It is responsible for broadcasting notifications within the system.

For example, when we use a cell phone without power, we will suddenly receive notifications of insufficient power. This is a broadcast produced by the Android system. In fact, many broadcasts are produced in the system. For example, a broadcast notification says that the screen is off, the battery is too low, or an image is captured. Applications can also initiate broadcasts, for example, notifying other applications that some data is downloaded to the device. Although the broadcast system does not have a user interface, they may create a status bar notification to notify the user of a broadcast event.

The Android system also provides the parent broadcastreciever class to perform basic operations. We need to inherit the broadcast receiver when defining it.

 

To sum up the four components introduced by Google, we can feel the overall composition of an android application.

Activity is a process in which your program completes a function. From creation to execution to completion, it indicates the completion of a required function. Of course, in this process, you may be interrupted, continue later, or terminate unexpectedly. Activity is a class used by the Android system to manage the lifecycle of a functional process. At the macro level, you need to use activity to manage your function execution process. When designing an application, you must first consider how to design a lifecycle that conforms to the activity.

Second, the service is the background to help you deal with some long-term work and provide long-term support services for your activity. Your activity can operate on it, such as start, pause, stop, and close.

The service component can be a part of your application service or an independent application to help you implement some functions.

Contentprovider is a window for opening sharing. It helps you manage the shared data of your applications, including reading data and writing data to storage. You can call the contentprovider of other applications through permission settings to obtain the shared data resources of other applications. As for broadcast receivers, it is like a messenger. As mentioned before, every application component, including system component, runs in its own process, in Linux, each application is managed as an independent and unique user, therefore, in the Android system mentioned above, if the component of an application needs to call the activity of another application to complete a function, it must start a new process for the application where the activity is located to run. The caller cannot directly call this process. It can only express its own meaning, that is to say, an idea is generated and transmitted to Linux Through broadcast or message. Linux helps to start the process of this application to run the corresponding activity. The intermediate message transmission processing relies on the broadcast receiver.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.