Chinaunix首页 | 论坛 | 博客
  • 博客访问: 508242
  • 博文数量: 95
  • 博客积分: 5168
  • 博客等级: 大校
  • 技术积分: 1271
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-28 23:31
文章分类

全部博文(95)

文章存档

2013年(2)

2012年(3)

2011年(1)

2010年(8)

2009年(81)

分类:

2009-06-04 12:57:14

-----------------------------------------------------------------------------------------
本文系本站原创,欢迎转载!

转载请注明出处:http://sjj0412.cublog.cn/

-----------------------------------------------------------------------------------------

intent (receiver)

刚才上面多次提到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));

        }

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

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

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"

        >

           

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

 

           

            <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,然后调用NotePadProvidergettype函数得到服务的类型。

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.EDITandroidmanfest.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" />

           

正好满足上面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(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对应一个数据库的table1是这个tableid

这个在如下可以看到

Contentproviderquery函数里

case NOTE_ID:

        qb.setTables(NOTES_TABLE_NAME);

        qb.setProjectionMap(sNotesProjectionMap);

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

    break;

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

阅读(4069) | 评论(3) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2011-01-11 20:23:04

茅塞顿开!!!感谢LZ

chinaunix网友2010-11-11 15:26:52

真是好文章,受益匪浅,希望LZ再接再厉哦,我等着学习呢

chinaunix网友2010-07-09 15:50:00

真是好文章,受益匪浅,希望LZ再接再厉哦,我等着学习呢