In Android, the use of intent is ubiquitous, it acts as a medium, can be used as an instruction, or a protocol. What it does is tell the Android system what to do and how to do it.
The Intent object group consists of six parts, namely the component Name,action,data,category,extras and the flags. So what are the roles of these parts, and when should they be used, a brief explanation below?
1,component Name
As the name implies, this refers to the names of the components. That is to pass the intent object to its component. The component name object is described by the ComponentName class, which contains the package name and the class name, and the component is bound to be declared in Androidmanifest.xml.
The component name is obtained through the getcomponent () by SetComponent (), SetClass (), Setclassname () settings.
Note that component name is an option, and if set, then the intent object explicitly specifies the component to turn to, and if it is not set, the intent object needs to filter the lookup based on other information.
For example: There are currently two service LocalService1 and LocalService2, and the configuration in the Androidmanifest.xml file is as follows:
<service android:name= ". Services. LocalService1, <intent-filter> <action android:name= " Com.example.test.LocalService1 "/ </intent-filter> </service> <service Android:name= ". Services. LocalService2, <intent-filter> <action android:name= " com.example.test.LocalService2 "/> </intent-filter> </service> |
String PackageName = Getpackagename (); String className = LocalService2.class.getName (); ComponentName component = new ComponentName (PackageName, className); Intent Intent = new Intent (); Intent.setcomponent (component); |
Intent.setclassname (Packagename,classname); |
This we specify that the intent will be sent to the object as LocalService2
2,action
This specifies an action, which is a string type. The intent class defines a number of action-related constants that correspond to some of the features available to the Android system for user use. Of course we can customize the action, only need to define a string, but it should be noted that the string should be prefixed with your app's package name!
The content of the action section is critical, and it should determine how other parts of the content are structured, such as "email", and the rest of the content will need to install the "Mail" protocol to construct the data. Action differs primarily from data and
Extras is different.
The action is set in the following way:
Intent Intent = new Intent (); Intent.setaction ("Com.example.test.LocalService2"); |
Consider if you are using the following method to set intent which service will be started
String PackageName = Getpackagename (); String className = LocalService1. Class.getname (); Intent Intent = new Intent (); Intent.setclassname (Packagename,classname); Intent.setaction ("Com.example.test. LocalService2"); |
The answer is to start LocalService1, and only if you do not set component name will follow the action to find it.
3,data
Before you talk about data, say the URI (Uniform Resource Identifier).
There are a lot of resources on the Internet (including local), including pictures, videos, programs and so on, but we need a uniform standard to locate all kinds of resources, this is the URI, it is actually a string. We can use a URI to interact with specific protocols and resources.
URI syntax structure: URI protocol name : What the protocol corresponds to
There are many URI protocol names, such as HTTP, ftp,mailto,file, etc., each protocol determines the syntax and semantics of the Protocol content, for example, if it is the HTTP protocol, then the protocol content should specify the hostname, port number, resource path, etc., if it is the Mailto protocol, Then the agreement content will need to specify the email address and so on.
In Android, the action and data are matched, and data describes the information that the corresponding action is to process. Data contains the following content:
<data android:scheme= ""/> <data android:host= ""/> <data android:port= ""/> <data android:mimetype= ""/> <data android:path= ""/> <data android:pathprefix= ""/> <data android:pathpattern= ""/> <data android:ssp= ""/> <data android:sspprefix= ""/> <data android:ssppattern= ""/> |
So what do you need to set up and what you don't need to set up when you use it? This is actually related to the use of the intent component, if that component is a browser, then we may need to be able to host, port number, path and other information.
If the configuration in the Androidmanifest.xml file is:
<service android:name= ". Services. LocalService2, <intent-filter> <action android:name= " Com.example.test.LocalService2 "/> <category android:name= "Android.intent.category.DEFAULT"/> <data android:scheme= " file "/>&NBSP; <data android:mimetype= " text/html /> </intent-filter> </service> |
The actual purpose of specifying the Data property in Intent-filter is to require that data in the received intent be compliant with the data property specified in Intent-filter, so that the role of the reverse-qualified intent is achieved.
So when you start the service, intent is written as follows:
Intent Intent = new Intent (); Intent.setaction ("Com.example.test.LocalService2"); Uri uri = uri.parse ("file://com.android.test:500/mnt/sdcard"); Intent.setdataandtype (URI, "text/html"); |
Data attribute parsing: Android:scheme, Android:host, Android:port, Android:path, Android:mimetype
The first four properties of data form part of the URI, MimeType sets the type of the data
The URI model consisting of the data element is as follows:
Example Description (URI):
File://com.android.test:500/mnt/sdcard |
Scheme-->file:
Host-->com.android.test
port-->500
Path-->mnt/sdcard
Where host and port are URI authority, Host,port will be ignored if not specified.
The properties of data are not independent, and the properties of data form the entire component of the URI. To make authority (host and port) meaningful, scheme must be specified and scheme and authority (host and port) must be meaningful to make path meaningful.
Uri and Intent-filter match:
In intent, the URI and the intent-filter are compared only partially:
(1) When only scheme is set in Intent-filter, only the scheme portion of the URI is compared;
(2) When only scheme and authority are set in Intent-filter, only scheme and authority in the URI are matched;
(3) When scheme, authority, and path are set in Intent-filter, only the scheme, authority, path in the URI will be matched, (path can be matched using wildcard characters)
(4) When MimeType is set in Intent-filter, data type matching is also performed.
The usual URI format for data is as follows:
tel://: The Number data format followed by the phone number.
mailto://: The message data format, followed by the mail recipient address.
smsto://: Short data format followed by SMS receiving number.
content://: The content data format, followed by what needs to be read.
file://: File data format followed by file path.
market://search?q=pname:pkgname: Market data format, in Google markets
Search for an app with a package named Pkgname.
geo://latitude,longitude: The latitude and Longitude data format that displays the location specified by the latitude and longitude on the map.
4,Category
The category in intent is an additional information about the action being performed. The category is a string that provides some of the default category string constants in intent. For example, Launcher_category indicates that the recipient of intent should appear as a top-level application in LAUNCHER, while Alternative_category indicates that the current intent is one of a series of optional actions, These actions can be performed on the same piece of data.
Common category constants and descriptions:
category_default: The default mode of execution in an Android system is performed according to the execution of normal activity.
category_home: Sets the component as the HOME Activity.
category_preference: Set the component to PREFERENCE.
Category_launcher: Sets the component to be the highest priority activity in the current application launcher,
Usually used with the entrance action_main.
category_browsable: Setting this component can be started using the browser.
category_gadget: Setting this component can be embedded into another activity.
5,extras
Additional information, which is a collection of all other additional information. You can use extras to provide extended information for your component, such as, if you want to perform the "Send e-mail" action, you can save the e-mail message's title, body, and so on in extras, to the e-mail sending component.
6,flags
Predefined series of property values that control the behavior of intent.
Intent.addflags (Intent.flag_activity_brought_to_front); If the activity is present in the task and gets to the top, no new activity is started |
Intent.addflags (Intent.flag_activity_clear_top); If activity is present in a task, all activity above activity is ended |
Intent.addflags (Intent.flag_activity_new_task); Default jump type, putting activity into a new task |
Intent.addflags (Intent.flag_activity_single_top); If the activity is already running to a task, jumping again will not run the activity |
This article comes from the "Simple and Easy" blog, so be sure to keep this source http://dengshuangfu.blog.51cto.com/8794650/1639759
Intent the use of a detailed