Intentfilter matching rules in Android--android development art Exploration notes

Source: Internet
Author: User

Welcome reprint, Reprint please indicate the source http://blog.csdn.net/l664675249/article/details/50640288

There are two ways to start activity, both explicit and implicit. The display call is simple, directly indicating the activity to be started, and here is the main introduction to implicit invocation. An implicit call requires intent to match the filtering information set in the Intentfilter of the target component. only one intent match action,category and data to match successfully.

Example
<intent-filter>    <action android:name="com.sparkyuan.c" />    <action android:name="com.sparkyuan.d" />    <category android:name="com.sparkyuan.c" />    <category android:name="com.sparkyuan.d" />    <category android:name="Android.intent.category.DEFAULT" />    <data android:mimetype="Text/plain" /></intent-filter>`
Action matching rule

The action is a string, the system has predefined actions, and we can define the action ourselves.
Add method

intent.setAction("com.sparkyuan.a");

Note:

    • You can have more than one action in a intent-filter
    • The action in the intent is identical to the one in the Intent-filter
    • Action is case sensitive;
Category matching Rules

Add method

intent.addCategory("com.sparkyuan.d");

Note:

    • The category may not exist in intent, but if it exists it must match intent-filter one
    • When the system is startactivity or Startactivityforresult, the default is intent plus a android.intent.category.DEAFAULT, so you must add Intent-filter in Android.int Ent.category.DEFAULT this category
Data matching rule data syntax
<data android:scheme="http"      android:host="www.baidu.com"      android:port="80"      android:path="string"      android:pathPattern="string"      android:pathPrefix="string"      android:mimeType="text/plain" />
Composition

Data consists of two parts, MimeType and URIs.
The URI format is as follows:

<scheme>:////示例
content://com.example.project:200/folder/subfolder/etc

Matching rules

The matching rule is similar to action, as long as a data match is available.

Note:

<data  android:mimeType="text/plain" />

Although the URI is not specified, the URI has a default value of content and file, so the URI portion of the intent must be either content or file.
The following method can match his

intent.setDataAndType(Uri.parse("file://abc"),"text/plain");

At last

The rules for Intent-filter are the same for service and Broadcastreceiver, but it is recommended to use the display method to start the service as much as possible.
When using implicit intent, it is possible to make a judgment on whether or not there is a corresponding activity, in case of an error. Use the Resolveactivity method of Packagemanager or the resolveactivity of intent, and return NULL if the match is not.

contex.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);intent.resolveActivity(getPackageManager());

The purpose of match_default_only is to remove activity that does not contain DEFAULT in the category.

Welcome reprint, Reprint please indicate the source http://blog.csdn.net/l664675249/article/details/50640288

Intentfilter matching rules in Android--android development art Exploration notes

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.