Android intent mechanism example (1)

Source: Internet
Author: User
Android provides an intent mechanism to assist in interaction and communication between applications. Or, to be more accurate, intent can be used not only between applications, it can also be used for interaction between activity/service in an application. Intent is an abstract concept that is not easy to understand for programmers who are less engaged in large-scale platform development, because it is different from the simple function/method call we usually use, or the method of calling interfaces through libraries mentioned in the previous section. In the use of intent, you cannot see direct function calls. Compared with function calls, intent is a more abstract concept. The granularity of software reuse implemented by intent is activity/service, higher than function reuse, and more loose coupling.

In Android, there are action/category and intent filter related to intent, and intent for broadcast. These elements are mixed together, making it difficult for beginners to quickly master intent usage. Before explaining these terms, let's first take a look at some basic usage of intent from the example below, and then think about the meaning behind this mechanism.

One of the keys to understanding intent is to understand the two basic usage of intent: an explicit intent, that is, the receiver is specified when the intent object is constructed, this method is similar to common function calls, but the granularity of reuse is different. The other is implicit intent, that is, when the intent sender constructs the intent object, I don't know and don't care who the receiver is. This method differs greatly from function call, which helps reduce the coupling between the sender and the receiver. In addition to sending, intent can also be used for broadcasting, which will be detailed later.

The following section describes the usage of the explicit intent.

Explicit intent (explicit intent)
2. Activity switching in the same application

We have discussed the concept of activity in the previous chapter. Generally, an application requires multiple UI screens and multiple activity classes, this switch is implemented through the intent mechanism.

When switching an activity in the same application, we usually know which activity to start. Therefore, explicit intent is often used for implementation. The following example is used to implement simpleintenttest, a very simple application, which includes two UI screens, namely, the simpleintenttest class and the testactivity class. The simpleintenttest class has a button to start testactivity, as shown in Figure 5.3:

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.