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

intent (receiver)

阅读更多

<!-- [if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

刚才上面多次提到intent , 那个Intent 就是指的的我要讲的intent ,那 intent receiver 又为什么呢,其实她和 activity service 一样响应相对应的 intent ,只不过 intent-receiver 多为接收事件,然后启动具体的 activity ,接收外部的 intent , 它没有 ui 界面,有点像是: activity 是老板,而 intent-reciever 像是秘书, intent-recieve activity 打理各种事情,然后通知老板即可,其实 intent-reciever sevice 是比较接近的, intent-receiver 可以看成是 activity 的代理,或者是输入事件型的 service ,是事件来了就处理,且很快能处理完,而 service 只是 service 是长生命周期的,往往需要长时间运行,通常为多个 activity 服务 , 正因为这个,他们的资源回收优先级也不同。既然 activity,intent-reciever service 都用到 intent , 那我们来说说 intent

intent 究竟是什么呢?

上面我们提到过,activity 是相互独立,一般他们是独立进程。但是刚才提到的面向服务要求又要求各个activity 有高频率的交互,这个怎么解决。这个解决方案就是使用intent.

其实我们可以将intent 看成是activity 通信的标准的。比如Intent 中的内容告诉了系统激发intentactivity 需要什么服务,服务者activity 应该满足的条件。然后就是android 系统的事了,它负责找出符合条件的activity 服务者,并将intentactivity 服务者,然后这个acitivity 根据intent 中剩余的信息做出相应的操作。

由上面可知,intent 包含两部分信息:

1. activity 服务者的信息,这个是给android 用来找到合适activity 的。

2. activity 服务者要做什么操作的信息,这个是给activity 服务者用的。

我们就以android 自带notepad 实例程序来说这个过程;

androidnotepad 实例程序中,入口activitynotelist

这里有一个onlistItenclick 函数。

protected void onListItemClick(ListView l, View v, int position, long id) {

Uri uri = ContentUris.withAppendedId (getIntent().getData(), id);

String action = getIntent().getAction();

if (Intent. ACTION_PICK .equals(action) || Intent. ACTION_GET_CONTENT .equals(action)) {

// The caller is waiting for us to return a note selected by

// the user. The have clicked on one, so return it now.

setResult( RESULT_OK , new Intent().setData(uri));

} else {

// Launch activity to view/edit the currently selected item

startActivity(new Intent(Intent.ACTION_EDIT , uri));

}

这个函数在 listview item 被单击时执行。

我们首先注意的就是这个函数。

startActivity( new Intent(Intent. ACTION_EDIT , uri));

这个函数有一个intent 对象,这个就是我们上面谈到的。

new Intent(Intent.ACTION_EDIT , uri) 这个可以看出 intent 承载了两个数据,一个是 uri 对像,一个是 Intent. ACTION_EDIT , 这个其实就是一个字符串变量,系统定义一些常量,当然也可使用你自己定义的。

如上图是程序运行后的效果,当我们点First 项时就会执行onListItemClick, 然后就会执行 startActivity(new Intent(Intent.ACTION_EDIT , uri)); 这个函数然后就到 android 系统了 ,android 通过 intent 中的信息找到对应的 activity, 在这里是 NoteEditor activity ,然后就会显示如下 .

android 是怎样找到的对应的 activity 的呢?

这个就是 intent 发挥作用的时候了。

下面我们就来讲讲:

new Intent(Intent.ACTION_EDIT , uri)

这个函数中 Intent.ACTION_EDIT =” android.intent.action.EDIT”

Uri 中一个重要的元素是 Uristring

这个其实是uri 特征标志,通过设断点,我们看下这里的uri 值。

从上图可以看出是Uristring =content://com.google.provider.NotePad /notes/1

然后我们看下Androidmanfest.xml,

其中有这个

< provider android:name = "NotePadProvider"

android:authorities = " com.google.provider.NotePad "

/>

发现没,也有com.google.provider.NotePad ,这个是content://com.google.provider.NotePad/notes/1 的一部分,同时

< activity android:name = "NoteEditor"

android:theme = "@android:style/Theme.Light"

android:label = "@string/title_note"

>

<!-- This filter says that we can view or edit the data of

a single note -->

< intent-filter android:label = "@string/resolve_edit" >

< action android:name = "android.intent.action.VIEW" />

< action android:name = " android.intent.action.EDIT " />

< action android:name = "com.android.notepad.action.EDIT_NOTE" />

< category android:name = "android.intent.category.DEFAULT" />

< data android:mimeType = "vnd.android.cursor.item/vnd.google.note" />

</ intent-filter >

<!-- This filter says that we can create a new note inside

of a directory of notes. -->

< intent-filter >

< action android:name = "android.intent.action.INSERT" />

< category android:name = "android.intent.category.DEFAULT" />

< data android:mimeType = "vnd.android.cursor.dir/vnd.google.note" />

</ intent-filter >

</ activity >

上面有 android.intent.action.EDIT 正好是

Intent.ACTION_EDIT =” android.intent.action.EDIT

这个时候估计大家看出了一点端倪。

现在就开始进入activity 选择机制:

在函数startActivity 执行后,系统接管,然后系统通过获取intent 中的uri ,找到了content://com.google.provider.NotePad/notes/1, 去掉开始的content: 标识,得到com.google.provider.NotePad/notes/1, 然后获取前面的com.google.provider.NotePad ,然后就到注册了的Androidmanfest.xmlauthoritiescom.google.provider.NotePadprovider ,这个就是后面要讲的contentprovider, 然后就加载provider

< provider android:name = "NotePadProvider"

android:authorities = " com.google.provider.NotePad "

/>

在这里是NotePadProvider, 然后调用 NotePadProvider gettype 函数得到服务的类型。

public String getType(Uri uri) {

switch ( sUriMatcher .match(uri)) {

case NOTES :

return Notes. CONTENT_TYPE ;

case NOTE_ID :

return Notes. CONTENT_ITEM_TYPE ;

default :

throw new IllegalArgumentException( "Unknown URI " + uri);

}

上面的sUriMatcher.match 用来检测uri 看自己能不能处理,在这里会返回return Notes.CONTENT_ITEM_TYPE;

这个是public static final String CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note ";

sUriMatcher .match(uri) 返回值其实是由

sUriMatcher = new UriMatcher(UriMatcher. NO_MATCH );

sUriMatcher .addURI(NotePad. AUTHORITY , "notes" , NOTES );

sUriMatcher .addURI(NotePad. AUTHORITY , "notes/#" , NOTE_ID );

决定的。

如果我们人为的将前面uristring 改为 content://com.google.provider.NotePad/notes/100, 然后match 会正常返回Notes. CONTENT_ITEM_TYPE . 可以找到相应activity ,但是在activity 执行managedQuery 时,调用 content-provider ,这个然后发现不存在 id=100 的数据项,就会异常,然后这个 activity 就会 stop.

在这里是正常返回了vnd.android.cursor.item/vnd.google.note

然后系统将这个和”android.intent.action.EDIT ”到androidmanfest.xml 去找匹配的activity.

< intent-filter android:label = "@string/resolve_edit" >

< action android:name = "android.intent.action.VIEW" />

< action android:name = " android.intent.action.EDIT " />

< action android:name = "com.android.notepad.action.EDIT_NOTE" />

< category android:name = "android.intent.category.DEFAULT" />

< data android:mimeType = " vnd.android.cursor.item/vnd.google.note " />

</intent-filter>

正好满足上面noteedit activityintent-filter 条件, 这样就找到了noteedit activity.

然后就加载这个类实例化,运行,然后就到了noteditoncreate

protected void onCreate(Bundle savedInstanceState) {

super .onCreate(savedInstanceState);

final Intent intent = getIntent();

// Do some setup based on the action being performed.

final String action = intent.getAction();

if (Intent. ACTION_EDIT .equals(action)) {

// Requested to edit: set that state, and the data being edited.

mState = STATE_EDIT ;

mUri = intent.getData();

} else if (Intent. ACTION_INSERT .equals(action)) {

// Requested to insert: set that state, and create a new entry

// in the container.

mState = STATE_INSERT ;

mUri = getContentResolver().insert(intent.getData(), null );

// If we were unable to create a new note, then just finish

// this activity. A RESULT_CANCELED will be sent back to the

// original activity if they requested a result.

if ( mUri == null ) {

Log.e ( TAG , "Failed to insert new note into " + getIntent().getData());

finish();

return ;

}

// The new entry was created, so assume all will end well and

// set the result to be returned.

setResult( EN-

分享到:
评论

相关推荐

    Service开机自动启动

    2.构造一个IntentReceiver类,重构其抽象方法onReceiveIntent(Context context, Intent intent),在其中启动你想要启动的Service。 3.在AndroidManifest.xml中,首先加入...

    Android课程第一次实验报告_Andorid应用程序Activity生命周期.docx

     对于进程生命周期,一个普遍的错误就是:当一个Intent Receiver在它的onReceiveIntent()方法中,接收到一个intent后,就会从这个方法中返回。而一旦从这个方法返回后,系统将会认为这个Intent Receiver不再处于...

    androidintent详解参照.pdf

    IntentFilter 是一个特殊的类,用于描述一个 Activity 或者 IntentReceiver 能够操作哪些 Intent。一个 Activity 如果要显示一个人的联系方式时,需要声明一个 IntentFilter,这个 IntentFilter 要知道怎么去处理 ...

    Android小区停车场管理系统app论文.docx

    应用程序框架是指 Android 系统中的一些基础框架,such as Activity、Intent Receiver、Service 等,这些框架为开发者提供了开发应用程序的基础设施。 4. 函数库 函数库是指 Android 系统中的一些预定义的函数,...

    台湾高人高焕堂Android讲义

    Android的4种嫡系组件(即Activity、Service、IntentReceiver和ContentProvider)之间如何互相沟通呢?这4种嫡系组件都是由Android启动的,并不是组件之间透过直接呼叫而启动的。就像我们打手机去车行叫出租车,而不是...

    Android帮助文档

    Android帮助文档(第一部分...Intent Receiver 20 Service 20 Content Provider 20 4、入门指引 20 入门指引:记事本应用 21 记事本练习 1 22 记事本练习 2 29 记事本练习 3 38 入门指引: 额外的练习(Extra Credit) 43

    android 4大组件.rar

    android四大主键部分,Activity、ContentProvider、IntentReceiver、service

    android 开发技巧合集

    2.2、INTENT RECEIVER 25 2.3、SERVICE 26 2.3.1、什么是Service 26 2.3.2、如何使用Service 27 2.3.3、Service的生命周期 32 2.3.4、判断服务开启状态 33 2.3.5、获取启动的服务 34 2.4、CONTENT PROVIDER 35

    Android应用程序的Life Cycle

    前端进程是指当前正在与用户交互的进程,例如当前屏幕最上层的 Activity 或 IntentReceiver。这种进程的重要性最高,系统只有在内存非常低的情况下才会销毁它们。 可视进程是指拥有一个在屏幕上可以被用户见到的 ...

    Android天气预报widget的设计与实现毕业论文

    3.2.2 Broadcast Intent Receiver介绍 13 3.2.3 Service介绍 14 3.2.4 Content Provider介绍 14 3.2.5 Intent介绍 15 3.3 ANDROID应用程序工程文件 17 第4章 天气预报WIDGET的设计 19 4.1 网络功能实现 19 4.1.1 ...

    基于Android平台课堂点名系统的设计与实现.pdf

    * Android 客户端应用是由 Activity、Intent Receiver、Service、ContentProvider 四部分组成的。 * Android 应用程序可以使用 Eclipse 作为开发平台,SQLite 作为数据库管理系统。 * Android 应用程序可以使用 ...

    android开发框架介绍

    2. Broadcast Intent Receiver:Intent 提供了各种不同 Activity 进行跳转的机制,譬如如果从 A activity 跳转到 B activity,使用 Intent 来实现。BroadcastReceiver 提供了各种不同的 Android 应用程序进行进程间...

    Android期末复习题总结.doc

    12. 对于直接 Intent,Android 不需要去做解析,因为目标组件已经很明确,Android 需要解析的是那些间接 Intent,通过解析,将 Intent 映射给可以处理此 Intent 的 Activity、IntentReceiver 或 Service。...

    android手机通讯录的设计与实现论文

    2.2.2 Intent Receiver 8 2.2.3 Service 9 2.2.4 Content Provider 9 3需求分析 9 3.1功能需求分析 9 3.2性能需求分析 10 3.3数据库需求分析 11 3.4安全需求 11 4功能设计 11 4.1总体设计 11 4.2系统功能设计 13 4.3...

    android手机通讯录的毕业论文

    2.2.2 Intent Receiver 8 2.2.3 Service 9 2.2.4 Content Provider 9 3需求分析 9 3.1功能需求分析 9 3.2性能需求分析 10 3.3数据库需求分析 11 3.4安全需求 11 4功能设计 11 4.1总体设计 11 4.2系统功能设计 13 4.3...

    Android应用开发期末考试题.doc

    其中,Activity 负责用户界面,Broadcast Intent Receiver 负责接收广播,Service 负责后台服务,Content Provider 负责数据共享。 Android 应用程序开发 Android 应用程序开发需要使用 Android SDK 和开发工具...

    andriod精华学习教程

    IntentReceiver执行时间的特殊限制意味着它应该做:在后台里做小的、琐碎的 工作如保存设定或者注册一个Notification。和在主线程里调用的其它方法一样 ,应用程序应该避免在BroadcastReceiver里做耗时的操作或计算...

    Android性能优化 ANR 分析指导文档

    * IntentReceiver 执行时间的特殊限制意味着它应该做小的、琐碎的工作,如保存设定或者注册一个 Notification。 * 应用程序应该避免在 BroadcastReceiver 里做耗时的操作或计算。 * 如果响应 Intent 广播需要执行一...

    Android 45 道面试题及答案.docx

    * receiver:IntentReceiver 能使 application 获得数据的改变或者发生的操作,即使它当前不在运行。 * service:Service 是能在后台运行任意时间的组件。 * provider:ContentProvider 是用来管理持久化数据并发布...

    Android Intent和Intent Filter详解

     三种应用程序基本组件——activity, service和broadcast receiver——是使用称为intent的消息来激活的。Intent消息传递是一种组件间运行时绑定的机制. intent是Intent对象, 它包含了需要做的操作的描述, 或者, ...

Global site tag (gtag.js) - Google Analytics