Intent官方教程(5)在manifest中给组件添加<Intent-filter>
Receiving an Implicit Intent
To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an <intent-filter> element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category. The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters.
<Intent-filter>设置应用可收到的Intent。
Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.
An app component should declare separate filters for each unique job it can do. For example, one activity in an image gallery app may have two filters: one filter to view an image, and another filter to edit an image. When the activity starts, it inspects the Intent and decides how to behave based on the information in the Intent (such as to show the editor controls or not).
Each intent filter is defined by an <intent-filter> element in the app's manifest file, nested in the corresponding app component (such as an <activity> element). Inside the <intent-filter>, you can specify the type of intents to accept using one or more of these three elements:
<intent-filter>的子元素如下:
<action>
Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
<data>
Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path, etc.) and MIME type.
<category>
Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.
如果想让你的组件模糊匹配到更多的意图,必需在<category>添加默认选项: <category android:name="android.intent.category.DEFAULT"/>
For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is text:
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
It's okay to create a filter that includes more than one instance of <action>, <data>, or <category>. If you do, you simply need to be certain that the component can handle any and all combinations of those filter elements.
可以定义多个<action>,<data>,<category>,
When you want to handle multiple kinds of intents, but only in specific combinations of action, data, and category type, then you need to create multiple intent filters.
Restricting access to components
Using an intent filter is not a secure way to prevent other apps from starting your components. Although intent filters restrict a component to respond to only certain kinds of implicit intents, another app can potentially start your app component by using an explicit intent if the developer determines your component names. If it's important that only your own app is able to start one of your components, set the exported attribute to "false" for that component.
组件的exported="false"属性可以让组件不被其它应用通过Intent启动。
An implicit intent is tested against a filter by comparing the intent to each of the three elements. To be delivered to the component, the intent must pass all three tests. If it fails to match even one of them, the Android system won't deliver the intent to the component. However, because a component may have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another filter. More information about how the system resolves intents is provided in the section below about Intent Resolution.
精确型的Intent,严格检查上面介绍的3个子元素,完全匹配才算精确。
Caution: To avoid inadvertently running a different app's Service, always use an explicit intent to start your own service and do not declare intent filters for your service.
Note: For all activities, you must declare your intent filters in the manifest file. However, filters for broadcast receivers can be registered dynamically by calling registerReceiver(). You can then unregister the receiver with unregisterReceiver(). Doing so allows your app to listen for specific broadcasts during only a specified period of time while your app is running.
Example filters
To better understand some of the intent filter behaviors, look at the following snippet from the manifest file of a social-sharing app.
<activity android:name="MainActivity">
<!-- This activity is the main entry, should appear in app launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name="ShareActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>
</activity>
The first activity, MainActivity, is the app's main entry point—the activity that opens when the user initially launches the app with the launcher icon:
The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.
These two must be paired together in order for the activity to appear in the app launcher.
通常 ACTION_MAIN 表示是主程序 , CATEGORY_LAUNCHER表示在应用列表中显示。 它们要同时使用。
The second activity, ShareActivity, is intended to facilitate sharing text and media content. Although users might enter this activity by navigating to it from MainActivity, they can also enter ShareActivity directly from another app that issues an implicit intent matching one of the two intent filters.
Note: The MIME type, application/vnd.google.panorama360+jpg, is a special data type that specifies panoramic photos, which you can handle with the Google panorama APIs.
Intent官方教程(5)在manifest中给组件添加<Intent-filter>的更多相关文章
- Intent官方教程(2)Intent的两种类型
Intent Types There are two types of intents: Explicit intents specify the component to start by name ...
- Intent官方教程(4)用Intent构造应用选择框
Forcing an app chooser When there is more than one app that responds to your implicit intent, the us ...
- Intent官方教程(3)各属性介绍
Building an Intent An Intent object carries information that the Android system uses to determine wh ...
- Support Library官方教程(3)android studio中导入支援包
Support Library Setup How you setup the Android Support Libraries in your development project depend ...
- Intent官方教程(1)简介和作用
Intents An Intent is a messaging object you can use to request an action from another app component. ...
- Intent官方教程(6)常见Intent示例,启动日历,时钟,镜头等。
guide/components/intents-common.html 包含:Alarm Clock Calendar Camera Contacts/People App Email File S ...
- 在unigui中为组件添加hint
//改变hint的样式procedure TUniForm1.UniFormShow(Sender: TObject);var i: Integer;begin for I := 0 to Self. ...
- android三大组件之Intent
Android 应用程序中有三大核心组件: Activity, Service, Broadcast Receiver 都是通过被称之为意图的消息运行. Intent messaging is a f ...
- MVC4学习之官方教程中迁移版本库报错
因工作需要,学习MVC4,但是微软官方教程中迁移版本库步骤在本地测试报错 官方教程地址:http://www.asp.net/mvc/overview/older-versions/getting-s ...
随机推荐
- phpredis 订阅者模式
[TOC] 一.场景介绍 最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知.经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用 ...
- php session memcache
ini_set("session.save_handler", "memcache"); ini_set("session.save_path&quo ...
- (转)SQL Server 2005 中的计算字段
在实际工作上遇到的问题: 在订单表中有某项商品是将“订购数量(Quantity)”乘以“单件价格(UnitCost)”等于该项商品的总价(Subtotal). 在数据表中有的列(以下皆改叫为“字段”) ...
- 夺命雷公狗---DEDECMS----14dedecms首页导航条的完成
我们的首页完成了,那么下一步就开始创建一个模型了, 添加好电影模型后我们来给他添加一些字段,这些字段主要还是要看我们的项目需求来添加的,因为我们的项目里有: 我们在项目中要用得上这些字段,所以要对他们 ...
- beta阶段事后诸葛亮会议
项目名:约跑 组名:nice! 组长:李权 组员: 韩媛媛 于淼 刘芳芳 宫丽君 Beta Review会议 时间:2016.11.15 地点:冬华楼一楼大厅 会议内容: 约跑APP的Beta Rev ...
- win7 64位系统下 PL/SQL无法连接的问题
第一步:下载oracle客户端 由于 PLSQL Developer 没有64位版本,所以在64位系统上运行该程链接64位Oracle时就会报错,笔者为这个问题纠结了好几天,后来通过请教Google ...
- 在IIS站点中Adomd.net集成认证账号问题
最近在做一个Asp.net项目的时候 ,在C#代码里面用到了Adomd.net去连接SSAS服务器做MDX查询,开发完成后将Asp.net代码部署到IIS后发现Adomd.net老是连接不到SSAS服 ...
- 批处理命令——rem 和 pause
[1]rem命令简介 注释命令,在C语言中相当于/*----------*/,它并不会被执行,只是起到一个注释的作用,便于别人及自己将来阅读和维护脚本. 为了更具体的理解,请看示例: 新建一个文本文件 ...
- ERR: Call to undefined function openssl_random_pseudo_bytes()
最近使用TP5/PHP7,总是出现ERR: Call to undefined function index\index\openssl_random_pseudo_bytes(),才发现是php没有 ...
- linux 下安装gsl
访问 http://ftp.club.cc.cmu.edu/pub/gnu/gsl/下载最新版本的,现在最新的是gsl-1.16.tar.gz,已经是2013年更新的了.然后下载 安装 简便的安装过程 ...