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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值