android IntentFilter 使用之 data过滤

需求: 过滤指定action的intent

场景:一个广播接收器需要接收一个广播,起初只用actiion来标示,但是后期发现

我如果正在跟好友A聊天的情况下,我只想监听来自A以外的广播

解决办法:IntentFilter通过设置data相关属性来实现


先看一下官方文档 

Data test
Like the action and categories, the data specification for an intent filter is contained in a subelement. And, as in those cases, the subelement can appear multiple times, or not at all. For example:
<intent-filter . . . >
    <data android:mimeType="video/mpeg" android:scheme="http" . . . /> 
    <data android:mimeType="audio/mpeg" android:scheme="http" . . . />
    . . .
</intent-filter>

Each <data> element can specify a URI and a data type (MIME media type). There are separate attributes —schemehostport, and path — for each part of the URI:

scheme://host:port/path

For example, in the following URI,

content://com.example.project:200/folder/subfolder/etc

the scheme is "content", the host is "com.example.project", the port is "200", and the path is "folder/subfolder/etc". The host and port together constitute the URI authority; if a host is not specified, the port is ignored.

Each of these attributes is optional, but they are not independent of each other: For an authority to be meaningful, a scheme must also be specified. For a path to be meaningful, both a scheme and an authority must be specified.

(如果一个authority要有意义,那么必须指定scheme: 一个path要想有意义,必须同时指定authority和scheme)

When the URI in an Intent object is compared to a URI specification in a filter, it's compared only to the parts of the URI actually mentioned in the filter. For example, if a filter specifies only a scheme, all URIs with that scheme match the filter. If a filter specifies a scheme and an authority but no path, all URIs with the same scheme and authority match, regardless of their paths. If a filter specifies a scheme, an authority, and a path, only URIs with the same scheme, authority, and path match. However, a path specification in the filter can contain wildcards to require only a partial match of the path.

The type attribute of a <data> element specifies the MIME type of the data. It's more common in filters than a URI. Both the Intent object and the filter can use a "*" wildcard for the subtype field — for example, "text/*" or "audio/*" — indicating any subtype matches.

The data test compares both the URI and the data type in the Intent object to a URI and data type specified in the filter. The rules are as follows:

  1. An Intent object that contains neither a URI nor a data type passes the test only if the filter likewise does not specify any URIs or data types.
  2. An Intent object that contains a URI but no data type (and a type cannot be inferred from the URI) passes the test only if its URI matches a URI in the filter and the filter likewise does not specify a type. This will be the case only for URIs like mailto: and tel: that do not refer to actual data.

  3. An Intent object that contains a data type but not a URI passes the test only if the filter lists the same data type and similarly does not specify a URI.

  4. An Intent object that contains both a URI and a data type (or a data type can be inferred from the URI) passes the data type part of the test only if its type matches a type listed in the filter. It passes the URI part of the test either if its URI matches a URI in the filter or if it has a content: or file: URI and the filter does not specify a URI. In other words, a component is presumed to support content: and file:data if its filter lists only a data type.

If an intent can pass through the filters of more than one activity or service, the user may be asked which component to activate. An exception is raised if no target can be found.

然后看一下在代码中如何filter

IntentFilter mFilter
mFilter.setPriority(1000);
mFilter.addAction("。。。。");
mFilter.addDataScheme("content");
mFilter.addDataAuthority("aothority", null);
mFilter.addDataPath("/path", PatternMatcher.PATTERN_LITERAL);
注意:path需要以/开头,否则压根就filter不到

发送广播的时候

Intent intent = new Intent();
intent.setAction(intentAction);
intent.setData(uri);
context.sendOrderedBroadcast(intent, null);


发送广播的时候,mFilter会通过匹配 你传入Intent的uri

xml监听的时候

 <receiver
            android:name="c..........."
            android:exported="true">
            <intent-filter>
                <action android:name="xxxxxx"/>
                
                <data
                    android:host="com.package"
                    android:pathPattern="/.*"
                    android:scheme="content"/>
            </intent-filter>
        </receiver>

注意一下pathPattern就可以了, /.*是监听所有的path




你可能感兴趣的:(android IntentFilter 使用之 data过滤)