Android Data Filtering

Source: Internet
Author: User

If the program wants to tell the Android system which intent can respond to and process, the intentfilter object needs to be used. The intentfilter object filters out intent requests that can be processed by the component, and the implicit intent (explicit
Intents) and intent filter (implicit intents) are compared to the intent action, data, and category. In fact, an implicit intent request must be passed to the target component through these three checks. Android will not pass the hidden intent to the target component if it does not match any aspect. When it matches an intent to a component that can process data, generally, it is important to know the data type (its MIME type) and its Uri. For example, a component can display image data and should not be called to play an audio file.

1. Action Test

The <intent-filter> element can contain sub-elements <action>, for example:
<Intent-filter>
<Action Android: Name = "com. example. matexample"/>
<Action Android: Name = "com. example. match2"/>
<Action Android: Name = "com. examplematch3"/>
</Intent-filter>
A <intent-filter> element must contain at least one <action>. Otherwise, no intent request can match the <intent-filter> element. If the action type is not set in the intent request, as long as <intent-filter> contains the action type
The intent request will pass the <intent-filter> behavior test smoothly.

2. Category Test

The <intent-filter> element can contain <Category> sub-elements, such:
<Intent-filter...>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. browsable"/>
</Intent-filter>
The intent request will pass the test only when all the category in the intent request matches the <Category> of an intentfilter in the component, the redundant <Category> declaration in intentfilter does not cause a matching failure. An intentfilter that does not specify any category for testing only matches intent requests that do not set a category.
3. Data Testing

The description of data in <intent-filter> is as follows:
<Intent-filter...>
<Data Android: TYPE = "Video/*" Android: Scheme = "HTTP".../>
<Data Android: TYPE = "audio/*" Android: Scheme = "HTTP "..
./>
<Data
Android: TYPE = "image/*" Android: Scheme = "HTTP".../>

</Intent-filter>

The <DATA> element specifies the data Uri and Data Type of the intent request to be accepted. The URI is divided into three parts for matching: Scheme, authority, and path. The URI data type and scheme of the inteat request set with setdata () must be consistent with the one specified in intentfilter. If authority or path is also specified in intentfilter, they also need to match to pass the test.

In many cases, the data type can be inferred from the URI, especially content: Uris, which indicates the data on the device and is controlled by the content provider. However, the type can also be set explicitly. The setdata () method specifies the data Uri, settype () specifies the MIME type, and setdataandtype () specifies the data Uri and MIME type. Use getdata () to read Uri and GetType.

If you want to get a XX browser, you need to open the URL starting with HTTP/HTTPS ~

Configure intent-filter under the XX activity attribute in androidmanifest. xml
 
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. browsable"/>
<Data Android: Scheme = "HTTP"/>
<Data Android: Scheme = "HTTPS"/>
</Intent-filter>
 
In this way, when you encounter code like this (starting a XX activity in YY activity ),
 
Uri uri = URI. parse ("http://www.baidu.com ");
Startactivity (new intent (intent. action_view, Uri ));

At this time, the request will be received and processed in XX activity!
Getintent (). getdata (). tostring ()
You can get http://www.baidu.com!


Most applications can start new activities without referencing any special data. The activity has a filter for the action specified by "android. Intent. Action. Main" to start the application. If they appear in the application launch list, they also specify the "android. Intent. Category. launcher" type:

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.