Android Intent and Intent-filter SDK file
Intent 使用了有一些时候了,但是往往不知道其真正的内涵,通常都是用来作为 Activity
传递数据的工具,另外些时候在使用 Broadcasting来控制今天尝试著看了下 SDK 文档。
前半部分是叙述 Intent 的用法,后半部分是使用 Notepad 的例子来描述 Intent 的用法。
其中总结的几张表比较好,不过想看更加细节的内容还是请阅读 android SDK 文档吧。

1. Intent 使用在 Activity()中:
Context.startActivity()或者 Activity.startActivityForResult(),中
当然我们也可以在 get 前一个 Activity 回传的值使用 Activity.setResult()来回传

2. Intent 使用在 Service()中:
通过 Context.startService()来唤醒某些 service 程式,或者通过 Intent 来捆
绑某些服务,例如 Context.bindService()
3. Intent 使用在 Broadcasting()中:
在广播中我们使用 Intent 来发送某些广播至系统例如 Context.sendBroadcast(),
Context.sendOrderedBroadcast(), or Context.sendStickyBroadcast()这些服
务都需要使用 Intent 来开啟广播服务

综合以上原因,android 系统发觉某些 Activity,Service 以及 Broadcasting
都是需要 Intent 的;而且这里不会有重復发送的信息。使用在 Broadcasting 的 Intent
只能够为Broadcasting来使用,而绝不会使用在Activity和Service中。一个Intent
Pass 至 Activity 中后就只能够被 Activity 使用。绝对不会被 Service 和
Broadcasting 使用。
在下面文档中将著重讲解 Intent 和 intent-filters 组件的使用方法。

Intent Objuct
一个 Intent 对象是用来捆绑信息的。
Component name(组件名)
有如下的组件:setComponent(), setClass(),
setClassName()getComponent().

Action(动作)
Constant Target component Action
ACTION_CALL activity Initiate a phone call.
ACTION_EDIT activity Display data for the user to edit. ACTION_MAIN activity Start up as the initial activity of a task,
with no data input and no returned
output.
ACTION_SYNC activity Synchronize data on a server with data
on the mobile device.
ACTION_BATTERY_LOW broadcast receiver A warning that the battery is low.
ACTION_HEADSET_PLUG broadcast receiver A headset has been plugged into the
device, or unplugged from it.
ACTION_SCREEN_ON broadcast receiver The screen has been turned on.
ACTION_TIMEZONE_CHANGED broadcast receiver The setting for the time zone has
changed.
以上 Intent 的 Action 可以使用 setAction()和 getAction()来实现。

Data/type
Intent 依靠 action 来判断行为模式,然后需要依靠传递 data 来实现功能,而
android 的 DATA 通常情况下都使用 Uri 方式来传递参数。例如:
之前使用 ACTION_EDIT 来进行编辑,这些数据将对 URI 传送过来的数据进行编辑。如果是
ACTION_CALL 那么 Uri 中包含了 tel:的内容,那么将会拨打电话,如果是 ACTION_VIEW,
只要在 Uri 中跟了 http:的内容,那么将会开啟这个网址。
因此 setData()方法只能使用在 URI 参数中,setType()只能使用在 MIME 类型,
setDataAndType()方法都可以使用在 URI 和 MIME 类型中。而读 URI 使用的是
getData(),而获取 MIME type 使用的是 getType()方法。
URI 是 data,而 MIME 是 media type 的简称,意味媒体的类型。
MIME 的英文全称是"Multipurpose Internet Mail Extensions" 多功能
Internet 邮件扩充服务,它是一种多用途网际邮件扩充协议。
具体的内容见:http://baike.baidu.com/view/160611.html?wtp=tt#2

Category(种类)
一个 Intent 中可以放置多个 Category,Category 的具体内同如下:
Constant Meaning
CATEGORY_BROWSABLE The target activity can be safely invoked by the browser to
display data referenced by a link — for example, an image or
an e-mail message.
CATEGORY_GADGET The activity can be embedded inside of another activity that
hosts gadgets. CATEGORY_HOME The activity displays the home screen, the first screen the
user sees when the device is turned on or when the HOME
key is pressed.
CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in
the top-level application launcher.
CATEGORY_PREFERENCE The target activity is a preference panel.
Category
有如下方法可以操作:
通过使用 addCategory()来增加一个 category Intent 的对象
使用 removeCategory() 删除一个 Category
使用 getCategories()来获取一个 Category

Extras
通常情况下我们使用 Extras 对象来传递参数,例如把一个 Activity 中的 value 传递到
下一个 Activity 中,通常的使用是把 Bundle 一起联用,首先先把数据放置到 bundle 中
然后使用 putExtras() and getExtras()方法来传递数据和获取数据,当然这种方法也可
以使用在 Activity.startActivityForResult()之中。

Intent Resolution( (( (Intent 的细节 的细节 的细节 的细节) )) )
Intent 可以分成连部分来讨论:
Explicit intent ,指向组件的名称,例如一些 Activity 和 Service 中
Implicit intents,指向没有名称的目标,某些域的组件是看不见的,因此需要这些内部
Intent 来啟动这些组件

通常情况下 Intent 的使用一般不需要 intent-filter 的声明,但是如果是一个特俗操作
或者声明一些广播操作则需要在 intent-filter 中声明。

当一个 intent 被 intent-filter 所测试的时候通常情况下有以下三种:
action
data (both URI and data type)
category
在一个 apk 中会存在多个 Activity,service,broadcasting 等操作,因此需要
使用 Intent-filter 来进行一些权限的声明。或者屏蔽某些权限的使用。 Intent-filter 的使用可以参考 IntentFilter()类来进行了解,但是通常情况下
我们会在 Androidmanifest.xml 中来声明;使用<intent-filter>元素来进行,
但是有唯一的例外就是 filter 在注册 Broadcasting 服务的时候需要呼叫,
Context.registerReceiver();来声明。
下面是 intent-filter 的某些实验
Action test
An <intent-filter> element in the manifest file lists actions as <action> subelements. For
example:
<intent-filter . . . >
<action android:name="com.example.project.SHOW_CURRENT" />
<action android:name="com.example.project.SHOW_RECENT" />
<action android:name="com.example.project.SHOW_PENDING" />
. . .
</intent-filter>

Category test
An <intent-filter> element also lists categories as subelements. For example:
<intent-filter . . . >
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
. . .
</intent-filter>

Data test
使用数据的测试可以获得某些数据的联系例如:
<intent-filter . . . >
<data android:mimeType="video/mpeg" android:scheme="http" . . . />
<data android:mimeType="audio/mpeg" android:scheme="http" . . . /> . . .
</intent-filter>

每一个数据元素都必须使用 URI 和 data type(MIME)来进行表示,每一个 URI 元素包含
scheme,host,port,path 等部分
scheme://host:port/path
scheme(意义为:计划,组合)

例如:
content://com.example.project:200/folder/subfolder/etc
这里的 scheme 为 content,主机 host 为 com.example.project,端口 port 为 200,
路径 path 为 folder/subfolder/etc

Intent-filter Intent 滤波器,筛选器
Path 路径是唯一的,但是 scheme 不是唯一的

<data>数据元素的 type 类型在对于 URI 来说不一定是唯一的,例如文字的类型可以用
text/*来表示,而音频的类型可以用 audio/*来表示
Data 和 type 在 URI 和 intent 中的使用準寻一下规则:
A.一个 Intent 对象既不包含 URI 的数据类型,也不是只有通过 intent-filter。也没有
指定仍和 URI 和数据类型。
B.一个Intent包含一个URI但不是一个数据类型 (因为不能通过一个type来推断出URI)
例如像是 mailto: 以及 tel:这些并不是实际指的数据
C.Intent 包含的 data type,和 Intent-filter 中的数据类型应当是不相同的

Common cases
定义 MIME type的方式
<data android:mimeType="image/*" />
定义 scheme和 type共同存在的方式
<data android:scheme="http" android:type="video/*" />
通常情况下一般的 apk 啟动之后需要準寻一下两个 intent-filter
<intent-filter . . . >
<action android:name="code android.intent.action.MAIN" />
<category android:name="code android.intent.category.LAUNCHER" /> </intent-filter>

下面是 SDK file 中对 Intent-filter 使用发的一些介绍
1.
action: android.intent.action.MAIN
没有数据的 activity

2.
action: android.intent.action.MAIN
category: android.intent.category.LAUNCHER
每增加一个 Activity 就增加一个 Launcher,这样的表示发。出现在最常见的 Activity


3.
action: android.intent.action.VIEW
data: content://com.google.provider.NotePad/notes
让 Activity 中显示后面地址中 NotePad 所有的 List 数据
content://com.google.provider.NotePad/notes. 用户可以从这个地址get到自
己需要的 Note
4.
action: android.intent.action.PICK
data: content://com.google.provider.NotePad/notes
用户可以从后面地址中的 Note List 中获取数据
content://com.google.provider.NotePad/notes. 然后 action 会返回一个 URI
来指向用户选择的值
5.
action: android.intent.action.GET_CONTENT
data type: vnd.android.cursor.item/vnd.google.note
通知 Activity 支持 NotePad 的单一项目

另外在 Intent-filter 和 AndroidManifest.xml 中定义自己的 Intent,让把自己的
Activity 的 intent 暴露出来让其他的程序调用
目前参考这里的资源

http://www.eoeandroid.com/forum-viewthread-tid-53474-highlight-Intent.html
在 AndroidManifest.xml中定义不就可以了么?

例如定义这个:<action android:name="android.intent.action.VIEW" />
别人就可以使用 Intent inent = new Intent(Intent.ACTION_VIEW);来调到你的Activity了。

更多相关文章

  1. 对Android(安卓)ListView的理解
  2. 浅入浅出Android(017):当前Activity向下一个Activity切换,并传递数据
  3. 开始使用Android(安卓)Sutdio(二)配置Android(安卓)Studio
  4. 页面跳转与传值(Activity跳转与传值)
  5. Android基于DataBinding的一个基础框架
  6. [android] 获取网页中的HTML数据
  7. 【转】Android上HDMI介绍(基于高通平台)
  8. [Android]自定义组件示例:使用attrs.xml文件定制RadioButton
  9. Android中sqlite之从网上获取数据更新UI

随机推荐

  1. Android获取一个月前的日期
  2. Source not found-Android 使用隐藏API(2)
  3. How to decompile Google Android .apk f
  4. Android遍历文件Listfile返回值为null问
  5. 编译jar包
  6. Does Android(安卓)2.2 support contentE
  7. 关于Android LOG系统
  8. [Android]在Manifest中注册广播监听器
  9. Android(安卓)添加程序到桌面favorites列
  10. 29.Android 传感器