intent

来源:互联网 发布:html免费手机网站源码 编辑:程序博客网 时间:2024/05/17 08:02

http://www.cnblogs.com/engine1984/p/4146621.html

Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要。

一、什么是Intent

1、Intent的概念:

Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的activity, service和broadcast receiver之间的交互。Intent这个英语单词的本意是“目的、意向、意图”。Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来响应。

activity、service和broadcast receiver之间是通过Intent进行通信的,而另外一个组件Content Provider本身就是一种通信机制,不需要通过Intent。
如果Activity1需要和Activity2进行联系,二者不需要直接联系,而是通过Intent作为桥梁。通俗来讲,Intnet类似于中介、媒婆的角色。

这里写图片描述

2、对于向这三种组件发送intent有不同的机制:

使用Context.startActivity() 或 Activity.startActivityForResult(),传入一个intent来启动一个activity。使用 Activity.setResult(),传入一个intent来从activity中返回结果。将intent对象传给Context.startService()来启动一个service或者传消息给一个运行的service。将intent对象传给 Context.bindService()来绑定一个service。将intent对象传给 Context.sendBroadcast(),Context.sendOrderedBroadcast(),或者Context.sendStickyBroadcast()等广播方法,则它们被传给 broadcast receiver。

二、Intent的相关属性:

Intent由以下各个组成部分:component(组件):目的组件action(动作):用来表现意图的行动category(类别):用来表现动作的类别data(数据):表示与动作要操纵的数据type(数据类型):对于data范例的描写extras(扩展信息):扩展信息Flags(标志位):期望这个意图的运行模式

Intent类型分为显式Intent(直接类型)、隐式Intent(间接类型)。官方建议使用隐式Intent。上述属性中,component属性为直接类型,其他均为间接类型。

相比与显式Intent,隐式Intnet则含蓄了许多,它并不明确指出我们想要启动哪一个活动,而是指定一系列更为抽象的action和category等信息,然后交由系统去分析这个Intent,并帮我们找出合适的活动去启动。

Activity 中 Intent Filter 的匹配过程 :
这里写图片描述

三、Intent在Activity中的应用
1.每一个Activity都需要在AndroidManifest.xml中注册,在IntentFilter中指定相应的Action,以方便被其它活动启用自己

<activity android:name ="com.test.packagename.SecondActivity"   <intent-filter>      <action android:name="com.example.smyh006intent01.MY_ACTION"/>      <category android:name="android.intent.category.DEFAULT" />   </intent-filter> 

当activity的action为下面时,当前activity是这个项目的主活动

 <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.DEFAULT" />

2.intent在activity中的用法
(1)显式启动SecondActivity

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

或简化为

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

(2)隐式启动SecondActivity

Intent intent = new intent();intent.setAction(“com.example.smyh006intent01.MY_ACTION”)startActivity(intent);

或简化为

Intent intent=new intent(“com.example.smyh006intent01.MY_ACTION”);startActivity(intent);

四、Intent在BroadcastReceiver中的应用
1. BroadcastReceiver的注册,分为静态注册和动态注册, 在IntentFilter中指定相应的Action,使的自己能接收到相应的广播,即以方便广播找到对应的接收器,把相应内容给自己。

(1)动态注册(程序启动后才能接收到广播),在onCreate函数中注册

第一步:在类中声明全局变量intentFilter和广播接收器类 NetworkChangeReceiver(该类继承BroadcastReceiver)

private IntentFilter intentFilter;private NetworkChangeReceiver networkChangeReceiver;

第二步:在onCreate函数中注册广播接收器

intentFilter = new IntentFilter();intentFilter.addAction(“com.example.smyh006intent01.MY_ACTION”);networkChangeReceiver = new NetworkChangeReceiver();registerReceiver(networkChangeReceiver,intentFilter);

注意:当使用本地广播(广播的发送和接收只在应用程序内部,外部应用无法接收)时,只能使用动态注册,因为静态注册开机时就开始接收广播了。

LocalBroadcastManager localBroadcastManager;localBroadcastManager.sendBroadcast(intent);localBroadcastManager.registerBroadcastReceiver(networkChangeReceiver,intentFilter);

(2)静态注册(开机即接收到广播),在AndroidManifest.xml中注册

<receiver android:name ="com.test.packagename. NetworkChangeReceiver"   <intent-filter>      <action android:name="com.example.smyh006intent01.MY_ACTION"/>   </intent-filter> 

2.intent在广播程序中的用法

Intent intent=new intent(“com.example.smyh006intent01.MY_ACTION”);sendBroadcast(intent);

五、Intent传递数据
(1)发送方传字符串:

Intent intent = new intent(FirstActivity.this,SecondActivity.java); intent.putExtra("keyname",key);startActivity(intent);

(2)接收方收字符串:

Intent intent = getIntent();String data = getStringExtra("keyname")

(3)intent与bundle的结合
Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。由此看出bundle比intent更强大
Bundle类是一个key-value对

发送:Bundle mBundle = new Bundle(); mBundle.putString("Data", "data from TestBundle");Intent intent = new Intent();    intent.setClass(TestBundle.this, Target.class);    intent.putExtras(mBundle);  接收:Bundle bundle = new Bundle();bundle = this.getIntent().getExtras();

想对数据进行比较灵活的操作如批量操作的话就用bundle;
Bundle是可以对对象进行操作的,而Intent不可以。Bundle相对于Intent比较偏下层,比Intent接口更多,更灵活,但Bundle仍需要借助Intent才能在Activity之间传递。

概括一下,Intent旨在数据传递,bundle旨在存取数据,当然intent也提供一部分数据的存取,但比起bundle就显得很不灵活。

原创粉丝点击