`
talin2010
  • 浏览: 527090 次
  • 性别: 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-

分享到:
评论

相关推荐

    1.高焕堂讲解 Intent-based Programming

    在 Android 系统中,四大核心组件——Activity、Service、IntentReceiver 和 ContentProvider,它们并不直接相互调用,而是通过 Intent 来传递消息。Intent 类似于一个消息载体,包含了操作类型(Action)、数据...

    android intent 使用总结

    Intent 传递过程中,要找到目标消费者(另一个 Activity、IntentReceiver 或 Service),有两种方法来匹配:显示匹配(Explicit)和隐式匹配(Implicit)。 显示匹配(Explicit)是指明确指定要调用的组件,例如: ...

    android 4大组件.rar

    在Android操作系统中,四大组件是构建应用程序的核心元素,它们分别是Activity、ContentProvider、BroadcastReceiver(IntentReceiver)和服务(Service)。这四个组件各自扮演着不同的角色,共同协作以实现应用程序...

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

    本实验报告将深入探讨Android应用程序,尤其是Activity、Service和Intent Receiver的生命周期,并解释如何正确管理这些组件以避免不必要的进程销毁。 首先,Android应用通常在独立的Linux进程中运行。当应用需要...

    androidintent详解参照.pdf

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

    Android平台下应用程序的四大构造体系.docx

    IntentReceiver,也称为BroadcastReceiver,是一种接收并响应广播Intent的组件。广播Intent可以在系统范围内发送,即使接收者并未处于运行状态。BroadcastReceiver常用于响应系统事件(如网络连接变化、电池电量低等...

    Android应用的构成 Android应用的构成 Android应用的构成

    本文将详细介绍构成Android应用的主要组件:Activity、IntentReceiver、Service以及ContentProvider,并解释它们的作用及如何协同工作。 #### Activity **Activity**是Android应用中最基本的构建块之一,它代表了...

    Android智能手机天气预报系统设计.pdf

    在Android平台上,应用程序的设计基于四大核心组件:Activity、Intent Receiver、Service和Content Provider。 1. Activity组件是Android应用的基础,代表用户界面的单一屏幕。每个Activity都是一个独立的类,从...

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

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

    台湾高人高焕堂Android讲义

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

    Android智能手机天气预报系统设计及实现

    综上所述,本研究提出了一种基于Android智能手机的天气预报系统设计方案,通过对Activity、IntentReceiver、Service和ContentProvider等核心组件的合理利用,构建了一个能够为用户提供实时天气预报信息的应用。...

    高焕堂android讲义

    在Android开发中,应用程序通常由多个组件构成,主要包括Activity、Service、BroadcastReceiver(IntentReceiver的一种)以及ContentProvider。这些组件之间的通信是通过Intent机制实现的。Intent可以被视为一种消息...

    android天气预报系统设计借鉴.pdf

    2. **Intent Receiver** - Intent Receiver(BroadcastReceiver)允许应用对外部事件做出响应,如系统广播或自定义事件。在AndroidManifest.xml中注册接收器后,系统会在特定事件发生时自动启动对应的接收器,无需...

    Android帮助文档

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

    OPhone培训2应用编程.ppt

    Intent接收器(Intent Receiver)用于监听并响应如来电、闹钟等外部事件,而Service则是在后台运行的应用,没有用户界面,但能提供持续的服务,如音乐播放或后台数据同步。 Content Provider允许应用程序间的数据共享...

    用_Eclipse_开发_Android_应用程序

    - **源代码片段**:包括AndroidManifest.xml(片段)、IntentReceiver、SaySomething.java、Main.xml、R.java等,这些都是构建Android应用的核心部分。 - **AndroidManifest.xml**:作为应用的配置文件,详细定义了...

    开发术语,模拟器,LogCat工具的使用,ADB工具使用,

    12. **Intent Receiver**:Intent Receiver是Broadcast Receiver类,用于接收和处理系统或应用广播的消息。 13. **Layout Resource**:布局资源是XML文件,描述了Activity的用户界面结构,定义了各个UI元素的位置和...

    Android系统与应用开发.docx

    8. **Intent Receiver**:理解Intent的作用,如何使用BroadcastReceiver监听特定事件。 9. **AndroidManifest.xml**:了解这个配置文件的重要性,它是Android应用程序的元数据中心。 10. **Service**:创建和使用...

    基于Android技术的界面设计与研究

    对于一个完整的Android应用程序而言,其主要由四种构成块组成:Activity、IntentReceiver、Service、ContentProvider。 **1. Activity** Activity是Android应用程序中最基本的组成部分,通常表示一个单独的屏幕,...

Global site tag (gtag.js) - Google Analytics