那么我们就来看看这个Intent吧。
Intent封装Android应用程序需要启动某个组件的“意图”。Intent是应用程序组件之间通信的重要媒介。
Android的应用程序包含三种重要的组件:Activity,Service,BroadcastReceiver,都是依靠Intent来启动的,Intent还可用于与被启动的组件交换消息。我前面就写过Intent在Activity之间传递数据的文章。
http://blog.youkuaiyun.com/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();
上面的代码可以简写成:
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>
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>
这种通过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:
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:启动一个服务
Service
is a component that performs operations in the background without a user interface.
Intent
to startService()
.
Intent
describes the service to start and carries any necessary data.
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()
.
- 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()
.方法。