android notepad详解

intent (receiver)
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 中的内容告诉了系统激发 intent 的 activity 需要什么服务,服务者 activity 应该满足的条件。然后就是 android 系统的事了,它负责找出符合条件的 activity 服务者,并将 intent 给 activity 服务者,然后这个 acitivity 根据 intent 中剩余的信息做出相应的操作。

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

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

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

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

在 android 的 notepad 实例程序中,入口 activity 是 notelist 。

这里有一个 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 , 这个其实就是一个字符串变量,系统定义一些常量,当然也可使用你自己定义的。

[img]http://dl.iteye.com/upload/attachment/350001/ec7db609-fdaa-332a-91e0-85b4c4f40e78.jpg[/img]




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



[img]http://dl.iteye.com/upload/attachment/350003/8c81b48a-0a41-3ecd-843e-0c72a64550a5.jpg[/img]



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

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

下面我们就来讲讲:

new Intent(Intent.ACTION_EDIT , uri)

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

Uri 中一个重要的元素是 : Uristring

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


[img]http://dl.iteye.com/upload/attachment/350005/bb248f75-7065-34cf-9342-30d05461d627.jpg[/img]


从上图可以看出是 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.xml 找 authorities 为 com.google.provider.NotePad 的 provider ,这个就是后面要讲的 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 );

决定的。

如果我们人为的将前面 uri 的 string 改为 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 activity 的 intent-filter 条件 , 这样就找到了 noteedit activity.

然后就加载这个类实例化,运行,然后就到了 notedit 的 oncreate 。

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( RESULT_OK , ( new Intent()).setAction( mUri .toString()));



} else {

// Whoops, unknown action! Bail.

Log.e ( TAG , "Unknown action, exiting" );

finish();

return ;

}



// Set the layout for this activity. You can find it in res/layout/note_editor.xml

setContentView(R.layout. note_editor );



// The text view for our note, identified by its ID in the XML file.

mText = (EditText) findViewById(R.id. note );



// Get the note!

mCursor = managedQuery( mUri , PROJECTION , null , null , null );



// If an instance of this activity had previously stopped, we can

// get the original text it started with.

if (savedInstanceState != null ) {

mOriginalContent = savedInstanceState.getString( ORIGINAL_CONTENT );

}

}



上面 mCursor = managedQuery( mUri , PROJECTION , null , null , null ); 其实就是返回符合要求的数据 , 然后就是显示而已。

而其中 managedQuery 函数,又会用到 content-provider ,这里是 notepadprovider, 它创建一个 notepadprovider 实例,然后调用 query 函数,这个函数以 uri 类型的参数为依据返回合适的数据,这个 query 函数就是我们具体 content-provider 要实现的了,如果是用 sqlite, 那就通过 sqlitedatabase 取数据,如果是文件存储的,就通过 ifile 相关接口读取数据,其实在 query 函数中, content://com.google.provider.NotePad/notes/1, 后面的 /notes/1 才用到,对于 sqlite 实现的 content-privide: notes 对应一个数据库的 table , 1 是这个 table 的 id 项 。

这个在如下可以看到

Contentprovider 的 query 函数里

case NOTE_ID :

qb.setTables( NOTES_TABLE_NAME );

qb.setProjectionMap( sNotesProjectionMap );

qb.appendWhere(Notes. _ID + "=" + uri.getPathSegments().get(1) );

break ;

上面讲的 Intent 包含的是 uri,action 对象 , 其实还有一种指定方式,这个下一章节再讲。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/woshishushangdezhu/archive/2009/07/26/4382560.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值