Android学习笔记-学习Intent的使用

刚看到Intent的时候,我的确有点困惑:从字面上来说,它表示一种意图和目的;从使用上看,它似乎总是用于Activity之间的切换;而从它所在包 android.content来看,它似乎与内容有关。所以,我想或许可以这样理解它: Intent类绑定一次操作,它负责携带这次操作所需要的数据以及操作的类型等。

  如果是这样的话,是否可以将它与事件处理联想起来?即一个Intent类似于一个Event。从Intent的两个最重要的成员操作类型(Action)和数据(Data)来看,似乎是有道理的。文档中说, Intent的Action的取值主要是一些定义好了的常量,例如PICK_ACTION,VIEW_ACTION,EDIT_ACTION之类的,而 Data则是一个ContentURI类型的变量,这一点,我们前面提到过。

  而且文档中说Intent分为两大类,显性的(Explicit )的和隐性的(Implicit)。在前面的例子中,我们在两个Activity之间跳转时初步使用了Intent类,当时是用setClass来设置 Intent的发起方与接收方,它被称为显性的Intent,而隐性的Intent则不需要用setClass或setComponent来指定事件处理器,利用AndroidMenifest.xml中的配置就可以由平台定位事件的消费者。

  一般来说,intent要定位事件的目的地,无外乎需要以下几个信息:
1.种类(category),比如我们常见的 LAUNCHER_CATEGORY 就是表示这是一类应用程序。
2.类型(type),在前面的例子中没用过,表示数据的类型,这是隐性Intent定位目标的重要依据。
3.组件(component),前面的例子中用的是setClass,不过也可以用setComponent来设置intent跳转的前后两个类实例。
4.附加数据(extras),在ContentURI之外还可以附加一些信息,它是Bundle类型的对象。

  Implicit Intent的使用相对有点麻烦,我们来做一个例子。首先,我们需要增加一个类:HelloThreeProvider,它必须实现于ConentProvider接口,所以代码如下: 引用:
public class HelloThreeProvider extends ContentProvider ...{

    public boolean onCreate() ...{
        return true;
    }
   
    public int delete(ContentURI url, String where, String[] whereArgs) ...{
        return 0;
    }
    public ContentURI insert(ContentURI url, ContentValues initialValues)...{
        return url;
    }
    public Cursor query(ContentURI url, String[] projection, String selection,
            String[] selectionArgs, String groupBy, String having, String sort) ...{
        return null;
    }
    public int update(ContentURI url, ContentValues values, String where, String[] whereArgs) ...{
        return 0;
    }
   
    public String getType(ContentURI url) ...{
        return "vnd.sharetop.hello.three/vnd.hello.three";
    }

}这里面有一堆方法要实现,因为它们都是ContentProvider中的abstract方法,但是今天的例子中它们都半没有什么用处,只是一个 getType方法我们让它不管什么url都返回一个表示Intent所携带的数据类型是我们定义的一个长字串: vnd.sharetop.hello.three/vnd.hello.three。

  然后,在AndroidMenifest.xml中我们将上面这个HelloThreeProvider类加入应用程序: 引用:
<application android:icon="@drawable/icon">
        <provider class="HelloThreeProvider" android:authorities="cn.sharetop.android.hello" />               
        <activity class="HelloThree" android:label="@string/app_name">
            <intent-filter>
                <action android:value="android.intent.action.MAIN" />
                <category android:value="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity class="HelloThreeB" android:label="bbb">
        <intent-filter>
             <action android:value="android.intent.action.VIEW" />
             <category android:value="android.intent.category.DEFAULT" />
             <type android:value="vnd.sharetop.hello.three/vnd.hello.three" />
        </intent-filter>
        </activity>
    </application>相对于前面的例子,主要修改了HelloThreeB的配置,包括增加了一个<category>标签表示这是  一个一般性的 activity而已。增加了<action>标签,定义它负责处理VIEW_ACTION类型的操作。增加了<type>标签给出一个数据类型的定义串vnd.sharetop.hello.three/vnd.hello.three。最主要的是在< application>下增加的那个<provider>标签,有个authorities属性,我们给的值是 cn.sharetop.android.hello,待一会我们再说它的用处。

  最后就是修改以前的跳转代码如下: 引用:
Intent intent = new Intent();
  intent.setData(new ContentURI("content://cn.sharetop.android.hello/one"));
  intent.setAction(intent.VIEW_ACTION);
  startActivity(intent);现在我们的setData里的东西可与以前不一样的,是吧?注意到它的格式了吗?content://是个协议头,固定这样写就行了。然后就是那个 authorities中定义的串了,再后面就是我们自定义的东西了,我这里很简单的写个one,其它还可以更长一点,如one/101之类的。它负责去关联上那个provider类。另外,增加了setAction的调用设置操作为VIEW_ACTION,与Menifest中的< action>又挂上了。Android平台负责根据Intent的Data信息中的authorities,找到 ContentProvider,然后getType,用type和intent中的Action两个信息,再找到可以处理这个intent的消费者。

  OK,编译运行。

  其实,如果是在一个应用内部,这种隐性的intent实在有点别扭,个人觉得,这种松藕合的实现方法,只适用于那些较大的系统或者多个不同的应用之间的调用,可手机上又有什么“较大”的系统呢?无非是可以与不同来源的多个应用之间方便地互操作而已,那么会是什么样的场景呢?比如,给QQ好友发送gmail 邮件,用GoogleMap查找QQ好友所在的位置?看上去挺不错的。

  关于这个ContentProvider,其实还有话说,它主要是的那些看似数据库操作的方法我们都没真正去实现呢。不过今天就到这里了,等下回再去研究吧。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章