Intent 和Intent-filter


    今天上午有个朋友去面试,考题里就出到了Intent的用法,显示意图和隐式意图,还有如何用Intent传递数据。

那么我们就来看看这个Intent吧。

Intent封装Android应用程序需要启动某个组件的“意图”。Intent是应用程序组件之间通信的重要媒介。

Android的应用程序包含三种重要的组件:Activity,Service,BroadcastReceiver,都是依靠Intent来启动的,Intent还可用于与被启动的组件交换消息。我前面就写过Intent在Activity之间传递数据的文章。

http://blog.csdn.net/pangrongxian/article/details/50166835

Intent对象包含Component、Action、Category、Data、Type、Extra和Flag这7种属性。

为了演示,我们假设创建两个Activity,FirstActivity和SecondActivity。

1.Component(组件)目的组件

假设现在要从FirstActivity跳转到SecondActivity中,下面是代码:

//创建一个ComponentName对象
ComponentName  comp = new ComponentName(FirstActivity.this , SecondActivity.class);
Intent  intent  =  new Intent();

//为Intent设置Component属性
Intent.setComponent(comp);
startActivity();

上面 程序的代码创建 ComponentName对象,并将该对象设置成Intent对象的Component属性,这样应用程序即可根据该Intent的“意图”去启动指定的组件了。(这个例子指定的组件是: SecondActivity

上面的代码可以简写成:

Intent  intent  =  new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);

指定Component属性的Intent已经明确了它将要启动那个组件,因此这种Intent也被称为显示Intent,没有指定Component属性的Intent被称为隐式Intent——隐式Intent没有明确指定要启动那个组件,应用程序会根据Intent指定的规则去启动符合条件的组件,那些是符合条件的组件呢?下面说道到的Action属性就是了。


2.Action(动作):用来表示意图的动作

3.Category(类别):用于表示动作的类别

Intent的Action、Category属性都是一个普通的字符串而已,Action代表该Intent所要完成的一个抽象的动作,而Category则作为Action增加额外的附加条件。

Action属性和Category属性在哪里写呢?它们两个都是在Manifest.xml清单文件中<activity>中的intent-filter中写的:

 <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <strong><action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
 </activity>


在Intent类中定义了一批Action(动作),例如:

action常量                  对应字符串                    说明

ACTION_MAIN                 android.intent.action.MAIN ——应用程序入口

ACTION_VIEW                 android.intent.action.VIEW ——查看指定数据

ACTION_CALL                 android.intent.action.CALL ——直接向指定用户打电话

ACTION_SEND                android.intent.action.SEND ——向其他人发送数据

ACTION_INSERT             android.intent.action.INSERT ——插入数据

ACTION_DELETE            android.intent.action.DELETE ——删除数据

ACTION_RUN                  android.intent.action.RUN ——运行

ACTION_SYNC                android.intent.action.SYNC ——执行同步数据

ACTION_PICK_ACTIVITY    android.intent.action.PICK_ACTIVITY ——用于选择Activity

ACTION_SEARCH           android.intent.action.SEARCH ——执行搜索


下面我们使用action属性,通过action属性来查找组件SecondActivity。

用Intent的隐式意图跳转到另一个Activity

action字符串的是可以由你任意来写的,一般把action字符串的写成:包名.action.MY_ACTION

在FirstActivity中写如下代码:

Intent intent = new Intent();
setAction(包名.action.MY_ACTION);
startActivity(intent);


然后在Manifest.xml清单文件中SecondActivity的<activity>中的<intent-filter>中配置action属性

 <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <strong><action android:name="包名.action.MY_ACTION" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
 </activity>


只要 FirstActivity中setAction()方法中,括号中参数(即action属性的字符串)对应上Manifest.xml清单文件中SecondActivity的<activity>中的<intent-filter>中action属性的字符串,FirstActivity就能够通过action属性找到SecondActivity,然后通过Intent跳转到SecondActivity。

这种通过action属性去启动组件的方式称为隐式Intent(没有指定Component属性的Intent被称为隐式Intent)

注意:在<intent-filter>中,必须添加category 属性才行,category.LAUNCHER是默认的category 。


4.Data(数据):表示与动作要操作的数据

5.Type(数据类型):对于data数据范例的描述

6.Extra(扩展信息)

7.Flag(标志位)

其他的几个属性不是很常用,用到的时候再查资料就可以啦!

这里也有视频讲解这些属性的用法的,给一个扣丁学堂的连接:

http://www.codingke.com/course/167/learn#lesson/455


这篇就讲些纯理论的东西吧,我自己也不太喜欢太过理论的东西,还是比较喜欢实战。

最后来看看Intent 的API文档的简介:(我只是顺便学习一下英文的o( ̄ヘ ̄o#))

    An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases:

    一个Intent(意图)是一个消息对象,它通常能让你从另一个应用程序组件中请求一个动作。尽管Intents在好几个方面使应用程序组件之间的交流变得容易。这里有三个基本的使用例子:


  • To start an activity:启动一个Activity:
    An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity().

    一个Activity代表一个应用程序中的一个屏幕(一个界面)。你可以通过传递一个Intent来startactivity(),启动一个新的activity实例。


The Intent describes the activity to start and carries any necessary data.

Intent(意图)描述Activity的启动和携带任何必要的数据。


    If you want to receive a result from the activity when it finishes, call startActivityForResult()

    当一个activity结束掉的时候,如果你想要接收到一个activity返回的结果,你需要调用startActivityForResult()方法。


  • To start a service:启动一个服务
    A Service is a component that performs operations in the background without a user interface. 
    服务是运行在后台的没有用户界面的一个组件。

    You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService()
    你可以启动一个服务来执行一次操作(如下载文件),通过传递一个Intent来startservice()。

The Intent describes the service to start and carries any necessary data.
Intent(意图)描述 service 的启动和携带任何必要的数据。

    If the service is designed with a client-server interface, you can bind to the service from another component by passing an Intent to bindService()
    如果这个服务是被设计成客户端的服务器的接口,你可以从另外一个组件通过传递一个Intent来bindService()——绑定这个服务。


  • To deliver a broadcast:传送一个广播

    A broadcast is a message that any app can receive. 

    广播是一个消息,任何应用都能够接收。

    You can deliver a broadcast to other apps by passing an Intent to sendBroadcast()sendOrderedBroadcast(), or sendStickyBroadcast().

    你可以发送一个广播到其他的应用,通过传递一个Intent(意图),使用sendBroadcast()sendOrderedBroadcast(), or sendStickyBroadcast().方法。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值