Android intent application development overview

Source: Internet
Author: User

Today, let's take a look at intent. That's right. It's the one that's hard to understand.ArticleThen, you find that the previous sentence is actually incorrect.
As mentioned in the previous article, intent is like a double-sided adhesive between activities. In terms of its literal meaning, "intention, intention, and purpose" can basically interpret the role of this object. It contains information that tells us what is happening and what we want to do. I think it is very similar to the previous event-driven events. The difference is that it not only contains events, but also some data information. Intent can also be said to describe the current event and some data like a requirement description. Next, Android will find an activity based on this intent description, and give the intent to the activity. Intent looks good
Like a wedding advertisement, Intent puts forward some of its own descriptions (the difference is that he describes himself rather than others), and then Android finds the activity based on these descriptions.
Intent can be either an explicit intent or an implicit intent. Explicit intent directly declares in its own instructions who (which activity) receives and processes the intent. The explicit intent means that I already have a target, and tells android that it doesn't have to work hard to find the target, so you can directly locate the target. For example, the example in the previous article:

Intent intent = new intent ();
Intent. putextra ("name", edittext. gettext (). tostring ());
Intent. setclass (activitylifecycle. This, alertdialog. Class );

The setclass method directly tells android that the intent is handled by alertdialog. You can also use setcomponent (), setclassname (), and other methods to do the same thing.
In normalProgramIn, I think it is enough to use an explicit intent. The program looks simple and easy to understand.MultipleAndroid programs of dtplus do not have too complex relationships between activities.
In another method described above, the intent is implicitly used. Implicit intent is more subtle, confused, vague, and has no clear goals. It is pessimistic and does not require the other party. It only describes its own situation, that is to say, I am the one I described. Anyone who can accept it can apply for it. :)

Intent intent = new intent ();
Intent. putextra ("name", edittext. gettext (). tostring ());
Intent. setaction ("com. roiding. sample. Action. Main ");

Intent:

action indicates the action to be executed, such as action_view, action_edit, action_main, this is a standard action defined in Android. developers can define their own required actions and use strings to represent them. For more standard actions, see here.
"category" describes the additional information of an action. I have never quite understood this attribute. Why is it designed? So far, my understanding of this attribute is as follows: it is like an action category, for example, category_launcher and category_alternative. Which belongs to the category_launcher category and can be executed by Android in the top-level list (similar to the interface of the jiugongge). If it belongs to the category_default category, startactivity (intent) can be used to start the activity, and so on. These behaviors are determined by Android, so custom category does not know what it means
. For more information about standard category, see here.
data of Data action operations. The data is carried to the activity that receives the data in the URI format like dowry. The concept of URI is the same as that of the traditional Uri, which follows RFC 2396. In the end, Uri is provided by contentprovider, so it involves the concept of contentprovider. Here we will not explain it for the time being. We only need to know the data declared by this data, just like a URL, through this URL, you can find the data provided by contentprovider that provides this URL, and then you can operate on the data. The data here seems to overlap with the previous extras function? According to my understanding, data can indeed be transmitted in extras, but it is intended to be put forward separately. It will be explained in the later intentfilter.

Now that we have compared an advertisement for marriage, and compared intent to a requirement for marriage, what are the candidates selected by the Android system?
This file: androidmanifest. XML, and compared it to the household register. Now, all the young people of the appropriate age (activities) are in this large household register, you can search for it directly in the user register according to the intent requirement. Then, how do we know which young people of the appropriate age meet the requirements?
In the androidmanifest file, the intent-filter describes the requirements that young people of the current age are willing to accept, in this way, when the intent is consistent with the description in the intent-filter, the intent finds the activity.
In this way, when all the descriptions of an intent are accepted by the intent-filter of the activity, this activity is one of the candidates. If multiple activities become candidates, Android prompts you to manually select one of the multiple activities as the final intent receiver performer when running the program.
For an activity, there can be multiple actions, multiple category, and multiple data in its intent-filter, so that multiple intents can match.
Speaking of this, the previous issue about overlapping functions of data and extras does not seem to be solved yet, so the important role of this data is that it provides a reverse selection mechanism, the activity can also make requirements for intent, requiring that the intent data must be one of the data declared in intent-filter (because multiple data may exist ). This is very important, otherwise there will be errors such as activitynotfoundexception.
By the end of this article, General Android interface programming should be enough, and for contentprovider, adapter, services, etc., in general applications, you do not have to consider it at all. Wait until it is necessary.

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.