<span id="Label3"></p><p><p>Category: C #, Android, VS2015;</p></p><p><p>Date created: 2016-02-23 I. INTRODUCTION</p></p><p><p>Intent: the intent, meaning is that you want to use it to invoke which component implements related functions, such as calling the camera component to take pictures, calling the contact component to get contacts information, etc.</p></p><p><p>Of the four core components of the Android system, in addition to the content provider, the other three core components (Activity, Services, broadcast receiver) are actually activated by an asynchronous message called Intent.</p></p><p><p>By passing these component functions called by the intent object, these components may be provided by the Android system itself, or you may be customized. second, the composition of the intent object</p></p><p><p>A unique aspect of the Android system design is that any program can launch components from other Programs. For example, If you want a user to use a camera device to capture a photo, if there is another program dedicated to it, then your program can call it directly instead of developing a photo activity Yourself. In other words, you can simply start the activity of taking pictures in the camera Program. When the photo is completed, the photo will be returned to your program for your Use. From the User's point of view, It's as if the camera component is part of your Program.</p></p><p><p></p></p><p><p>In fact, you invoke a component in the program, which is actually to let andoid automatically start the process of the program and instantiate the class required for the component in the process that is Started. For example, If your program initiates activity in the camera program to take a photo, the activity is actually running in the process of the camera program, rather than explicitly creating a process within your own program to start it.</p></p><p><p>When you write code in C # in a desktop application, the operating system, in addition to maintaining all of the processes and their access, is specifically provided with a progress class that lets you start and manage processes, and lets you pass arguments to the progress object, passing instances of the class that the process needs to invoke and other information , as to when to start the process and when to stop the process, you can decide by yourself according to the Situation. In Android applications, it simply provides a startactivity () method that lets you invoke the method to start another activity (an activity is actually a process) and let you use intent in the parameters of the method to pass the information that the process needs, The start and stop processes (that is, start, pause, and stop activities) are handled by the Androd system on its own (and it can be understood that it simply does not give you so much control to avoid more complex process management). In addition, the desktop application calls each of the methods that implement standalone functionality in a process called starting a thread, while Android calls each method in the activity that implements independent functionality called a component, some of which are implemented by the system themselves (called implicit intent, You can just tell it who this component is by intent, as to which method in the class you are calling, and you also have the ability to customize the class (called explicit intent, which you have to tell which class you are customizing and which method in the class).</p></p><p><p></p></p><p><p>Because the Android system runs all programs separately in each separate process and uses the permissions configuration to restrict access to other programs, your program cannot activate the component directly from other Programs. or, to activate a component in another program, you must send a message to the Android system that launches the specified component through your intent object, and the system activates the component for YOU.</p></p><p><p>In short, in android, intent is responsible for binding different independent components together at runtime and then letting the system invoke them according to the situation (which can be understood as messages that request actions from other components), whether they belong to your program or to other Programs.</p></p><p><p>A intent object can contain the following information:</p></p> <ul> <ul> <li>Component Name: the component to be activated</li> <li>Action: the action to be performed by the component</li> <li>Data: data for Action actions</li> <li>Category: additional information about the action being performed</li> <li>Extended Domain: Additional additional information about the action being performed (a series of "key/value" Pairs)</li> <li>Flag: tell Android how to start activity</li> </ul> </ul> <p><p>These concepts are described separately BELOW.</p></p><p><p>1. Component Name (componentname)</p></p><p><p>Refers to the name of the component that handles intent that contains the complete custom namespace.</p></p> <ul> <ul> <li>The namespace in which the component name is called by intent does not need to match exactly the namespace set in the manifest File.</li> <li>The component name of the intent call is Optional. In other words, instead of giving it a name, Android automatically assigns it a component name (using Intent's Other information to localize the appropriate target).</li> </ul> </ul> <p><p>(1) Specify the component name in the layout file</p></p><p><p>In a layout file (axml or XML file), you specify the component name in two ways:</p></p><p><p>The 1th way is to use Android:name to Specify.</p></p><p><p>The 2nd method is specified with class, at which point the intent object assigns it to an instance of the specified class. For example (note that in C # development, There is no requirement to have lowercase letters):</p></p><p><p><fragment class= "MyDemos.SrcDemos.ch1104FragmentTitles"</p></p><p><p>......</p></p><p><p></FrameLayout></p></p><p><p>Where Mydemos.srcdemos is a namespace, ch1104fragmenttitles is the class name under that Namespace. When specified in this manner, it automatically creates an instance of the class and assigns the intent to an instance of the class.</p></p><p><p>(2) get and set the component name in THE. CS file</p></p><p><p>In addition to setting the component name in the layout file, you can get the name of the component in C # code through the component property of the intent Instance. For example:</p></p><p><p>Intent Intent = new Intent (this,typeof (ch1104fragmenttitles));</p></p><p><p>String className = intent.Component.ClassName;</p></p><p><p>String PackageName = intent.Component.PackageName;</p></p><p><p>You can also call intent by calling. SetComponent () is a way to specify a component name, but it is not usually specified unless you feel the need to do so. It's like you have a name, and you feel the need to start a pseudonym Again.</p></p><p><p>2, Action (action)</p></p><p><p>The action defines the structure of the intent, including data and extended FIELDS. For example, called method names, parameters, return values, and so On. For this reason, a good way to use an action name is to specify a namespace for the action class so that you can clearly distinguish between different actions so that the action name and other action names are not Confused.</p></p><p><p>The intent class defines a number of action constants, all of which are string types, such as:</p></p><p><p>String action = intent.actioncall;</p></p><p><p>Intent Intent = new Intent (action);</p></p><p><p>You can right-click the Intent class in The. CS file to see what constants are available, or you can type "Intent." After the Drop-down box to see what the action constants, These constants are "action" Beginning. For example:</p></p><p><p>var call = new Intent (intent.actioncall);</p></p><p><p>Here are some common action constants (note: when using C # to represent these action constants, remove the underscore for separating words, and change the first letter of each word to uppercase and the other letters to lowercase, such as intent.actioncall, which is more intuitive than words that are capitalized.)</p></p><p><p>The following table lists some of the action constants and their activities (activity) descriptions:</p></p><p><p></p></p> <p><p>In the next section, we'll list more constants when "implicitly starting activity" is Described.</p></p><p><p>3. Data</p></p><p><p>The data required for an action is typically represented by a URI and a corresponding MIME type. For example:</p></p><p><p>If the action field is actionedit, the data needs to provide the URI that contains the document being Edited.</p></p><p><p>If the action is actioncall, the data requires the number to be dialed (denoted by tel:uri).</p></p><p><p>If the action is actionview, the data is the destination URI (denoted by http:uri).</p></p><p><p>In fact, many times the data type can be inferred from the uri, but Content:uri is special, which means that the data is localized to the device and controlled by a content provider.</p></p><p><p>You can specify intent data by calling the SetData () method of the indent instance, for example:</p></p><p><p>var call = new Intent (intent.actioncall);</p></p><p><p>Call. SetData (Android.Net.Uri.Parse ("tel:" + translatednumber));</p></p><p><p>StartActivity (call);</p></p><p><p>4, Categories (category)</p></p><p><p>A category is a string that contains additional information (the prefix is category), and the information refers to the type of component that needs to handle Intent. Any type of numeric description can be placed in an intent object as an additional category.</p></p><p><p>Just as you define constants for actions, the intent class also defines some category constants (you can right-click the intent class in The. CS file to see which constants begin with category). For example:</p></p> <p><p></p></p> <p><p>For more information, You can read the description of the intent class, which has a complete list of Categories.</p></p><p><p>Addcategory () adds a category to the intent instance, removecategory () deletes the previously added category, and getcategories () gets all of the current Categories. For example:</p></p><p><p>Intent Intent = new Intent ();</p></p><p><p>Intent. Addcategory (intent.categoryappbrowser);</p></p><p><p>5. Extended Domain (Extras)</p></p><p><p>Component extensions also allow you to attach one or more key/value pair information to a intent Object. For example, a actiontimezonechanged (i.e.: action_timezone_changed) intent has a time-zone extension field that indicates the new time zone, actionheadsetplug (that is: action_ Headset_plug) There is a state field that indicates that the headset is plugged in or unplugged, and that name has a field that indicates the type of Headset.</p></p><p><p>If you customize an action (such as show_color), the color value should be set in the extended "key/value" Pair.</p></p><p><p>The intent object provides a series of put ... () method to insert various types of data and a series of Get ... () method to obtain various types of Data. For bundle objects, These methods are executed in Parallel.</p></p><p><p>In addition, data can be read and inserted as bundles using the Putextras () method and the Getextras () method.</p></p><p><p>6. Logo</p></p><p><p>The flag tells Android how to start the activity (for example: which task the activity belongs to), and how to handle it (for example, whether it belongs to the current activity list). These flags are defined in the intent class. second, Intent Filter and its matching rules</p></p><p><p>The intent filter (an instance of the Intentfilter Class) is responsible for filtering the provided component Functionality. or, the Android system must know what the components are capable of before starting the component, and the functionality of the component is implemented through the intent Filter.</p></p><p><p>When Android parses the intent filter, it will be matched according to the following principle:</p></p> <ul> <ul> <li>Any mismatched information will be filtered Out.</li> <li>Filters that do not specify an action can match any intent, but filters that do not specify a category can only match intent with no Categories.</li> <li>Each part of the intent data URI is matched against each property (protocol, hostname, pathname, mime Type) in the database filter and is filtered out as long as there is a Mismatch.</li> <li>If more than one result is matched, it is sorted based on the priority defined in <intent-filter>, and then the highest priority matching result is Selected.</li> </ul> </ul> <p><p>In summary, intent filters are a filtering mechanism that is declared based on actions, categories, and Data. By parsing the intent filter, Android can filter out components that do not meet the criteria, and then filter out the components that meet the criteria for application Invocation.</p></p><p><p></p></p><p><p>From the implementation principle, The intent filter is very similar to the URL matching rules in asp. net mvc, except that the ASP is to find the corresponding Web page according to the URL of the match to, and Android to find the corresponding component according to the Uri.</p></p><p><p></p></p><p><p>1, the composition of intent filter</p></p><p><p>To inform the system which component filter can handle intent, it can be implemented by including multiple intent filters in the Configuration. This allows the functionality of each filter to be differentiated by component.</p></p><p><p>The intent filter opens a component type that can receive implicit intent, and if the component does not declare a intent filter, it can only receive an explicit Intent. If the intent of the target component is not explicitly indicated, then the process passes the intent filter to test the intent object, which determines the potential Recipient.</p></p><p><p>The intent extension and intent flags resolve an issue that cannot determine which component receives Intent.</p></p><p><p>The intent filter has no reliable Security. When it opens a component that receives processing explicit intent, it does not block implicit Intent. Although some filters restrict the actions and data that the component can handle, it is sometimes possible to put different actions and data into an explicit intent and target the Components.</p></p><p><p>The intent filter tests the intent object from 3 aspects:</p></p> <ul> <ul> <li>Actions (action)</li> <li>Data (uri and Data Type)</li> <li>Categories (category)</li> </ul> </ul> <p><p>Each intent filter has an action field, a data field, and a intent object category Field. For any explicit intent, Android will test these three domains. As soon as one Test fails, the Android system does not allocate components for this Intent.</p></p><p><p>2. Set Intent Filter</p></p><p><p>(1) Setting the intent filter in the manifest file</p></p><p><p>The intent filter can be set directly through the Androidmanifest.xml File. For example, in the Androidmanifest.xml file, you specify action filtering through the Android:name property:</p></p><p><p><intent-filter. ></p></p><p><p><action android:name= "com.example.project.SHOW_CURRENT"/></p></p><p><p><action android:name= "com.example.project.SHOW_RECENT"/></p></p><p><p><action android:name= "com.example.project.SHOW_PENDING"/></p></p><p><p>......</p></p><p><p></intent-filter></p></p><p><p>Note that a intent object name may be just a single action, or it could be a list of filters that cannot be null (multiple actions).</p></p><p><p>Category filtering is also specified by the Android:name property, for example:</p></p><p><p><intent-filter ... ></p></p><p><p><category android:name= "android.intent.category.DEFAULT"/></p></p><p><p><category android:name= "android.intent.category.BROWSABLE"/></p></p><p><p>. . .</p></p><p><p></intent-filter></p></p><p><p>Data filtering is similar to actions and Categories. Child elements can appear more than once, or they may not appear. For example:</p></p><p><p><intent-filter ... ></p></p><p><p><data android:mimetype= "video/mpeg" android:scheme= "http" .../></p></p><p><p><data android:mimetype= "audio/mpeg" android:scheme= "http" .../></p></p><p><p>. . .</p></p><p><p></intent-filter></p></p><p><p>Each <data> element can specify a URI and a data type (mime type). Each part that makes up a URI is combined by a variety of attributes (scheme, host, port, path):</p></p><p><p>Scheme://host:port/path</p></p><p><p>For example:</p></p><p><p>Content://com.example.project:200/folder/subfolder/etc</p></p><p><p>This URI scheme is content, the host is com.example.project, the port is 200, and the path is folder/subfolder/etc. The host and port constitute the authority of the uri, and if no host is specified, the port is Ignored.</p></p><p><p>The type attribute of the <data> element specifies the MIME type of the Data. Generally there are more cases in the filter than in the Uri. Itnent objects and filters can use * to represent subdomains--for example, text/* or audio/*--means matching any subdomain.</p></p><p><p>The <data> element can tell the Android system component what types of data are obtained from the content provider, such as obtaining image data and displaying it:</p></p><p><p><data android:mimetype= "image/*"/></p></p><p><p>Another common configuration item is a filter that has a schema and data type. For example: use <data> tell Android components to get video data from the network and display:</p></p><p><p><data android:scheme= "http" android:type= "video/*"/></p></p><p><p>(2) specifying filters in C # code</p></p><p><p>In C # code, a filter can be specified with an attribute declaration, which is much more convenient than specifying the intent filter directly in the manifest File.</p></p><p><p>For an explicit intent, you can specify a filter by property, for example:</p></p><p><p>var imageintent = new Intent ();</p></p><p><p>Imageintent.settype ("image/*");</p></p><p><p>Imageintent.setaction (intent.actiongetcontent);</p></p><p><p>For implicit intent, a filter can be specified by an attribute declaration, for example:</p></p><p><p>[Activity (Label = "...")]</p></p><p><p>[intentfilter (new string[] {"...", "..."}, datamimetype = "image/*")]</p></p><p><p>public class Myactivity:activity</p></p><p><p>{</p></p><p><p>......</p></p><p><p>}</p></p><p><p>Where the 1th parameter of the Intentfilter method is used to specify one or more Action.</p></p><p><p>however, in C # programming it is generally not necessary to specifically declare it, this is because the compiler itself will be processed according to the situation, and automatically write the filter declaration to the manifest file, which is more than your own to declare more Intelligent. third, How to activate the component with intent</p></p><p><p>There are several ways to activate various types of components with intent:</p></p> <ul> <ul> <li>Start an activity with context.startactivity () or Activity.startactivityforresult (), which is useful for situations where you want the activity to return a Result.</li> <li>Start a service through Context.startservice (), or interact with Context.bindservice () and background services.</li> <li>The intent was sent to broadcast receivers by Radio. For example, call the Sendbroadcast () method, the sendorderedbroadcast () method, or the sendstickybroadcast () method.</li> <li>Let content provider perform a query operation by calling Contentresolver's query () method.</li> </ul> </ul> <p><p>For more information about intent, check out the intent and intent Filters Documentation.</p></p><p><p>1. Activity</p></p><p><p>For activity, the role of the intent object is to define the action (action) to be performed and specify the data URL that the action needs to execute (the other data required to start the component). For example, send a request through intent, let an activity go to display a picture and so On.</p></p><p><p>The following code shows how to call the StartActivity () method to pass the intent object and use it to invoke the Dial-up component (the passed action is intent.actioncall, and the data URL that the action executes requires is the phone number to dial out):</p></p><p><p>var call = new Intent (intent.actioncall); Initialize a phone call</p></p><p><p>Call. SetData (Android.Net.Uri.Parse ("tel:" + translatednumber));</p></p><p><p>StartActivity (call);</p></p><p><p>The intent action is activated by getting or setting the static Action property of the intent class.</p></p><p><p>remember, in the 2nd chapter, "the first Android App" example, has demonstrated the complete use of this Code.</p></p><p><p>In other cases, you can also start an activity by calling the StartActivity () method and receive the results returned by the activity through intent (later by getting the contact program to explain its specific usage).</p></p><p><p>2. Service</p></p><p><p>For service, The intent action is also activated by getting or setting the static Action property of the intent class. The purpose of the intent object at this point is to establish a connection between the calling component and the target Service. If a service is not running, it will start it automatically. alternatively, the role of the intent object is to define the service to be executed (for example, what to view or Send) and to specify the data URL that the service needs to execute (the other data needed to start the component). For example, send a request through intent, let an activity go to open a webpage and so On.</p></p><p><p>In later chapters, we will learn the service separately.</p></p><p><p>3. Broadcast Receiver</p></p><p><p>For broadcast receiver, Intent simply defines what needs to be Broadcast. For example, a broadcast that indicates a device's low battery (actionbatterylow) can contain only one string: "low power."</p></p><p><p>Intent can be passed on to all interested broadcast Receivers. Many broadcast sources are stored in the system kernel, you can call the following method to send a broadcast message: context.sendbroadcast (), context.sendorderedbroadcast (), Context.sendstickybroadcast ().</p></p><p><p>For example, calling StartActivity () to pass a intent object implements the broadcast function, then the Android system will automatically find the appropriate activity or service or set up a broadcast receiver to respond to intent when it receives the broadcast, and instantiate them when Needed. however, startactivity () Simply assigns the intent to the broadcast receiver, rather than assigning it directly to an activity or service.</p></p><p><p>In the later chapters, we will learn broadcast Receiver ALONE.</p></p><p><p>4. Content Provider</p></p><p><p>Another component type is the content Provider, note that it is not activated with intent, but is activated by the Contentresolver property of the activity Object.</p></p><p><p>Content Resolver Master the direct business of all content provider (the transaction is either all done or not, but it is never possible to do only a part of it), So the components that use this provider to perform transactions do not need to be executed directly but are controlled by the Contentresolver Property. For security, It places an abstraction layer between the content provider and the component request Information.</p></p><p><p>In later chapters, we will learn the content Provider separately.</p></p><p><p>These are the basic concepts of Ok,intent.</p></p><p><p>"Android" 12.1 Intent Basic concept</p></p></span>
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