`
Ancool
  • 浏览: 30895 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android Intent and Intent-filter SDK file

阅读更多

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了。

分享到:
评论

相关推荐

    新版Android开发教程.rar

    的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...

    Google Android SDK开发范例大全(完整版)

    为了在 Eclipse 中创建应用程序,选择 File &gt; New &gt; Android project,这将启动 New Android Project 向导。 图 5. New Android project 向导 接下来,创建一个简单的应用程序,该应用程序有一个活动,并且在 ...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...

    Google Android SDK开发范例大全的目录

    2.1 安装AndroidSDK与ADTplug-in 2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    Google Android SDK开发范例大全(完整版) 包含部分书中源码 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Application Using an Intent Filter 130 Configuring Other Intent Filters 130 Registering Other Application Components 131 Working with Permissions 131 Registering Permissions Your Application Requires...

    Google Android SDK 开发范例大全01

    Google Android SDK开发范例大全 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1 安装AndroidSDK与...

    Google Android SDK 开发范例大全02

    Google Android SDK开发范例大全 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1 安装AndroidSDK与...

    Google+Android+SDK开发范例大全

    Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1 ...

    Google Android sdk 开发范例大全 部分章节代码

    6.16 开机程序设计——receiver与intent-filter协同作业 6.17 双向短信常驻服务——Service与receiver实例 第7章 娱乐多媒体 7.1 访问Drawable资源的宽高——ContextMenu与Bitmap的应用 7.2 绘制几何图形——使用...

    可扩展的文件选择器NoNonsense-FilePicker

    &lt;action android:name="android.intent.action.GET_CONTENT"/&gt; &lt;category android:name="android.intent.category.DEFAULT"/&gt; &lt;/intent-filter&gt; 在java代码中调用选择器: // This always works Intent i = new...

    ContentProvider

    &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;provider android:name=".provider" android:authorities="com.example.tigongzhe.provider" android:multiprocess=...

    疯狂Android讲义源码

     1.2.1 下载和安装Android SDK 5  1.2.2 安装Eclipse和ADT插件 7  1.3 Android常用开发工具的用法 10  1.3.1 创建、删除和浏览AVD 10  1.3.2 使用Android模拟器  (Emulator) 14  1.3.3 使用DDMS进行调试 15...

    疯狂Android讲义.part2

    1.2.1 下载和安装Android SDK 5 1.2.2 安装Eclipse和ADT插件 7 1.3 Android常用开发工具的用法 10 1.3.1 创建、删除和浏览AVD 10 1.3.2 使用Android模拟器 (Emulator) 14 1.3.3 使用DDMS进行调试 15 1.3.4 Android...

Global site tag (gtag.js) - Google Analytics