Android learning notes-Intent (1)

Source: Internet
Author: User

Android learning notes-Intent (1)
The Intent object is described as follows in the official Android API: It is a passive data structure holding an abstract description of an operation to be performed med. it is a data structure that abstracts the operations to be executed. Its function is to connect two different components while the program is running. The Intent mechanism is a message mechanism that enables communication between components of Android applications. It allows you to transmit Intent between components of an application to execute actions and generate events, A component can transmit messages across applications. Intent is designed to reduce coupling between components of Android applications. Intent can be understood as the communication media or Intent of different components. Intent messages can activate three core components of the Android program: Activity, Service, and BroadcastReceiver. Intent usage: 1. intent application in Activity: 1. intent consists of the following six parts: Component name, Action, Data, Category, Extras, and Flags. 2. intent example: pass an Intent object to Context. startActivity (intent) or Activity. startActivity ForResult (int) to run an Activity (you can call the Activity in the Activity that is started in this way. setResult () sets the result parameter, which will be received in the activity that starts the current activity-can be received through onActivityResult (int requestCode, int resultCode, Intent data ). A. example of no response parameter: android. content. intent requestIntent = new android. content. intent (OrginalActivity. class, DestinationActivity. class); startActivity (requestIntent); B. example with return parameters: copy the code public class OrginalActivity extends Activity {public void onCreate (Bundle saveInstanceState) {super. onCreate (saveInstanceState); final android. content. intent requestIntent = new android. content. intent (OrginalActivity. clas S, DestinationActivity. class); findViewById (R. id. button ). setOnClickListener (new OnClickListener () {public void onClick (View v) {startActivityForResult (requestIntent, RequestCode) ;});} protected void onActivityResult (int requestCode, int resultCode, Intent intent) {if (requestCode = RequestCode & resultCode = ResultCode) {Bundle ret = intent. getExtras (); If (ret! = Null) {TextView textView = (TextView) findViewById (R. id. textView01); textView. setText (ret. getString ("key "));}} else {// Execution Code for other conditions }}// end of class definition Public class DestinationActivity extends Activity {public void onCreate (Bundle saveInstanceState) {super. onCreate (saveInstanceState); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {public void onClick (View V) {android. content. intent resultIntent = new android. content. intent (); resultIntent. putExtras ("key", "values"); setResult (ResultCode, resultIntent); finish () ;}}} copy code 3. intent-Action indicates an Intent Action. Generally, an Action is divided into a system-defined Action and a custom Action. a. the constant Action defined in Intent is the system Action as follows: B. the custom Action can be as follows: copy the Code <activity android: name = ". orginalActivity "> <intent-filter> <action android: name =" selectAction "/> <category android Oid: name = "android. intent. category. DEFAULT "/> </intent-filter> </activity> copy the code c. supplement: android. intent. action. MAIN meaning android. intent. action. MAIN indicates the Activity to be started when the Application is started. If there are multiple android applications. intent. action. MAIN, start the first android in mainfest. intent. action. MAIN. If multiple activities registered in the mainfest file have the <Intent Filters> label, specify action android: name = "android. intent. action. MAIN ", the system starts the first Activtiy from top to bottom in the Activity list by default. 4. intent Resolution (Resolution Intent) in our application, when we need to jump to another Activity in an Activity, we will send an Intent message by sending it, in the Action, specify the package name + Class Name of the Activity to jump to. We can jump to another Activity. In this case, the Intent we send is the explicit intent (explicit Intent ). Therefore, it is generally used in applications. We also have such a situation that we do not know the specific name of the Activity component to jump to, that is, we do not have a clear Activity to process our actions, we only know what conditions must be met for the Activity that needs to process our actions, and the android system must find the most suitable component to process this intent. At this time, the filtering conditions specified by our Intent will filter all registered activities that meet the conditions in our Android system. Then, we usually choose the Activity we need to use to process our actions. In this case, the Intent we send is an implicit Intent (implicit Intent ). Here we will introduce Intent Filters to explain our Intent filtering process. 5. The Intent Filters IntentFilter object Filters out Intent that cannot be responded or processed by the component, and only receives Intent that you care about for processing. IntentFilter implements "White List" management, that is, only lists Intent that the component is willing to accept, but IntentFilter only filters the implicit Intent, and the explicit Intent is directly transmitted to the target component. The Android component can have one or more intentfilters. Each IntentFilter is independent of each other. You only need one of them to pass the verification. In addition to the IntentFilter used to filter broadcast, you can create one in the Code. Other intentfilters must be declared in the AndroidManifest. xml file. IntentFilter has fields corresponding to Intent used to filter Action, Data, and Category. A. Only one Action can be set for an Intent object, but the IntentFilter of an Activity can have multiple actions. If one of the actions is met, the Intent message can meet the conditions. If the Intent message does not contain a specific action definition, the filter matches the Intent message as long as an action description is defined in the filter rule; B. intent messages do not contain any URI data or specific data content. They can only match filters that do not contain any data filtering information. Intent does not provide data types, the system calculates the data type from the data. If the data type calculated in an Intent message is included in the list of data types declared in the filter rule, the message matches the filter. Otherwise, the message is filtered by the filter. if an Intent object contains a data type but does not contain a URI, the detection is successful only when the Intentfilter does not specify a URL and only contains the data type and is the same as the Intent. If an Intent object contains both the URI and the data type (or the data type can be inferred from the URI), only when the data type matches the data type in the Intentfilter, when the URL check is passed, the Intent object can pass the check. The URL consists of four attributes: scheme, host, port, and path, which correspond to each part of the URI. For example, content: // com. wjr. example1: 121/files scheme part: content host part: com. wjr. example1 port part: 121 path part: files host and port part constitute URI credenity (authority). If the host is not specified, the port is ignored. These four attributes are optional, but they are not completely independent. To make authority meaningful, scheme must be specified. To make the path interesting, scheme and authority must be specified. The path in Intentfilter can use wildcards to match the path field. Both Intent and Intentfilter can use wildcards to specify the MIME type. C. Multiple Category can be set in Intentfilter, and multiple Category can also be contained in Intent. Only when all Category in Intent can match the Category in Intentfilter can Intent pass the check. That is to say, if the Category set in Intent is the subset of the Category set in Intentfilter, Intent can pass the check. If no Category is set in Intent, it can pass the Category check of all intentfilters. If an Intent can use the Intentfilter of more than one component, the user may be asked which component is activated. If no target is found, an exception occurs. To sum up, the application components can declare one or more intentfilters to tell Android users which implicit Intent requests can respond and process. Each IntentFilter describes the ability of the component to respond to Intent requests-the type of request behavior and type of request data that the component wants to receive.

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.