Analysis of Android message passing application functions

Source: Internet
Author: User

In the Android mobile phone operating system, there is a message transmission mechanism called Intent. This is a core technology in actual programming and deserves further research. Here, you will learn some basic application skills on Android message passing to help you understand.

  • Android ListBox application skills
  • Summary of Android constructed block categories
  • Android Button rules
  • Analysis of Android simulated SD card Implementation Method
  • Android onKey Operation Method

Each Cursor and ContentResolver serves as a small registration center. Relevant observers can register in this center, and update messages are distributed to various observers by the Registration Center. In MFC or Winform, a message network is formed to allow the message to flow in the network, which is used, eaten, or died at the egress by each node.

In contrast, I personally think the Intent-based Android message transmission mechanism is different. It should have a global registration center, which is invisible and the entire Android system is like this. All message recipients are implicitly registered to this center. Includes Activity, Service, and IntentReceiver. In fact, invisible registration is not accurate. We still manually tell the Registration Center about all registration operations, but it is different from the traditional method. We usually do it through the configuration file instead of code. In the Manifest of the application, Intent-filter is added for some activities or services, or the <author ER> </author ER> item is added to the configuration file. In fact, this is equivalent to registering the Intent-filter and aggreger related items with the Registration Center of the system. This can be done through code, but it loses the flexibility of modification ).

When a program wants to send a message, it needs to encapsulate the message into an Intent and send it. At this time, there should be a unified center. It is possible that the underlying implementation of Android is not, but it is okay to simply look at it ...) the process of accepting the message, parsing it, and determining the Message Type reduces coupling ...), check that a matched filter or receiver is registered, create or wake up the receiver, and distribute the message to it. This has many advantages. Although this type of transmission is sometimes less fast than point-to-point transmission, we can see that Android will do it through direct communication ), but sometimes it just goes through a hop ...), faster than complex flows.

More importantly, it has low coupling and is suitable for use when mobile phone platforms are changing program components. In addition, it can easily implement precise or fuzzy match of messages, which is highly elastic. I personally wanted to introduce this mechanism when developing a C ++ secondary platform. However, in C ++, it is not easy to establish a complete data marshal mechanism. In contrast, it is much simpler to use java ...)

Well, I spoke a lot of nonsense about the use of Intent in Android message transmission. When you need to transmit a message, if you know which Activity or other Class you need to respond to, you can specify this Class to accept the message, which is called explicit sending. You need to set the Intent class attribute to the target. This is a common situation. For example, when startActivity is used up, it will know which Activity the current Activity is supposed to be finished, and the message will be sent explicitly.

However, sometimes you are not sure which class your message needs to be executed, but only know what conditions the receiver should meet. For example, if you only need a receiver to display the data selected by the user and do not want to develop a specific method, you need to use the implicit sending method. Traditionally, we may consider using polymorphism, but obviously this method is more flexible ...).

In Android, you can specify an action for Intent to indicate what you need to handle. The system defines many Action types for us. These types enable the system to communicate with us. For example, if a Main filter is added to the Activity, the activity will be the entry point of the application ), of course, you can also use it for communication between your own applications. Of course, you can also customize it ...). It is strongly recommended that a system action be received or sent by your program. For example, if you respond to a view action and do the edit action, you send a pick message. In fact, if you want others to do the edit action, this will cause confusion.

Of course, only Action is sometimes not enough. In Android, we can also specify the catalog information and type/data information. For example, all the activities that display data may respond to the View action. However, many of the data types we need to display are different. We can add a type information to clearly indicate the data types we need to display, or even add a catalog information, indicates that only when you press "Middle" and send such a message to respond.

From the above, we can see that the Android Intent can be added to class, action, data/type, catalog and other messages. The Registration Center will help you find the appropriate receiver based on the information. Class indicates point-to-point. Once specified, other information is ignored. You can also add key/value data to Intent. The sender and receiver must maintain unified key information and value type information. The marshal of such data is implemented in java, no effort is required.

Android message transmission Intent can be divided into unicast and broadcast. The receiver of the broadcast is all registered intentreceivers that meet the conditions. In the case of unicast, even if there are many qualified recipients, it is good to have one to process the message. In my opinion, I have not found a definite clause or an algorithm for choice, I was trying to try it out before I could ...), this situation is easy to understand. When you need to modify a data, you certainly won't want ten editors to take turns for you to handle it. If this is not the case for broadcasting, one caster cannot prevent other broadcasters from handling Broadcast events. This situation is also easy to understand. For example, the clock has changed, and many programs such as alarm clocks and memos need to be processed separately. In the use of your own program, you should clearly distinguish between different and reasonable use.

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.