Intent对象主要用来在Android程序的Activity,Service和BroadcastReceiver这3大组件之间传输数据,

而针对这3大组件,有独立的Intent传输机制,分别如下:
1、Activity:通过将一个Intent对象传递给Context.startActivity()或Activity.startActivityForResult(),
启动一个活动或者使用一个已经存在的活动去做新的事情。
2、Service:通过将一个Intent对象传递给Content.startService(),初始化一个Service或者传递一个新的指令给正在运行的Service;
类似的,通过将一个Intent对象传递给ContentBindService(),可以建立调用组件和目标服务之间的连接
3、BroadcastReceiver:通过将一个Intent对象传递给任何广播方法
(如:Context.sendBroadcast(),Context.sendOrderedBroadcast(),Context.sendStickyBroadcast()等,可以传递到所有感兴趣的广播接收者) 注意:在每种传输机制下,Android程序会自动查找合适的Activity,Service或者BroadcastReceiver来响应Intent(意图),
如果有必要的话,初始化他们,这些消息系统之间没有重叠,即广播意图只会传递给广播接收者,而不会传递给活动或者服务,反之亦然。 Intent通过下面的属性来描述以上的某个意图:
action:用来表示意图的动作,如:查看、发邮件、打电话
category:用来表示动作的类别
data:用来表示与动作要操作的数据。如:查看 联系人
type:对data类型的描述
extras:附加信息,如:详细资料,一个文件,某事
component:目标组件(显示Intent) 显示Intent
指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context,Class)来指定)。
通过指定具体的组件类,通知应用启动对应的组件 隐式Intent
没有指定Component属性的Intent.这些Intent需要包含足够的信息,这样系统才能够根据这些信息,在所有的可用组件中,确定满足此Intent的组件。 对于显示的Intent,Android不需要去解析,因为目标组件已经很明确,
Android需要解析的是那些隐式的Intent,通过解析,将Intent映射给可以处理此Intent的Activity、IntentReceiver或者Service Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有Intent-filter及其定义的Intent,最终找到匹配的Intent。
1、如果Intent指明了action,则目标组件的Intent-Filter的action列表中就必须包含有这个action,否则不能匹配。
2、如果Intent没有提供type,系统将从data中得到数据类型,和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。
3、如果Intent的数据不是content:类型的URI,而且Intent也没有明确指定它的type,将根据Intent中的数据的scheme(比如http;或者mailto:)进行匹配。
同上,Intent的scheme必须出现在目标组件的scheme列表中。
4、如果Intent指定了一个或多个category,这些类别必须全部出现在组件的类别列表中。比如Intent中包含两个类别:LAUNCH_CATEGORY和ALTERNATIVE_CATEORY,
解析得到的目标组件必须至少包含这两个类别。 只有<action>和<category>中的内容同时能够匹配上Intent中指定的action和category时,这个活动才能够响应该Intent

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toMain"
android:text="显示Intent 启动自己"
android:textAllCaps="false" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setComponent"
android:text="启动别包下的activity"
android:textAllCaps="false" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="action"
android:text="ActionViewActivity响应此action"
android:textAllCaps="false" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="web"
android:text="浏览网页"
android:textAllCaps="false" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call1"
android:text="系统拨号界面"
android:textAllCaps="false" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call2"
android:text="拨号"
android:textAllCaps="false" /> </LinearLayout>

activity_main.xml

 public class MainActivity extends AppCompatActivity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void toMain(View v) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} public void setComponent(View v) {
//首先创建一个空参的Intent对象
Intent intent = new Intent();
//Component(包名,完整的类名)
ComponentName componentName = new ComponentName("com.example.lesson9_activitylaunchmode", "com.example.lesson9_activitylaunchmode.MainActivity");
//设置目标组件
intent.setComponent(componentName);
startActivity(intent); } public void action(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
//这里可能找不到能够响应这个ACTION_VIEW的目标组件,会报ActivityNotFound异常。所以可以做try-catch
startActivity(intent);
//创建一个ActionViewActivity活动,并注册,指定能响应ACTION_VIEW
} public void web(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
} //系统拨号界面,报错
public void call1(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");
startActivity(intent);
} //拨号界面
public void call2(View v) {
Uri uri = Uri.parse("tel:18822818871");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
}
}

MainActivity.java

 public class ActionViewActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("这是这是ActionViewActivity");
setContentView(tv);
}
}

ActionViewActivity .java

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActionViewActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter> </activity>
</application>

AndroidManifest.xml


Android Intent简介的更多相关文章

  1. Android 中的 Intent 简介

    Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据. ------------------------------- ...

  2. Intent简介-Android开发

    一.Intent介绍: Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  3. 【译】Android系统简介—— Activity

    续上一篇,继续介绍Android系统.上一篇: [译]Android系统简介 本文主要介绍构建Android应用的一些主要概念: Activity Activity是应用程序中一个单独的有UI的页面( ...

  4. Intent系列讲解---Intent简介以及相关属性

    一.Intent简介 Intent中文是"意图,意向",它是Android中四大组件通讯的纽带,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Androi ...

  5. Android插件简介

    /** * @actor Steffen.D * @time 2015.02.06 * @blog http://www.cnblogs.com/steffen */ Android插件简介 Andr ...

  6. Android精通教程-第一节Android入门简介

    前言 大家好,给大家带来Android精通教程-第一节Android入门简介的概述,希望你们喜欢 每日一句 If life were predictable it would cease to be ...

  7. Intent 简介 结构 传递数据 常见Action 常量 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. Android精通教程-Android入门简介

    前言 大家好,我是 Vic,今天给大家带来Android精通教程-Android入门简介的概述,希望你们喜欢 每日一句 If life were predictable it would cease ...

  9. Android BroadcastReceiver 简介

    Android BroadcastReceiver 简介  在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver     活动(A ...

随机推荐

  1. 知识库系统confluence5.8.10 安装与破解

    一直对知识库体系很在意,设想这样的场景,公司历年的研发资料只要一个搜索,相关的知识点就全部摆在面前,任君取用,想一想就无限迷人,只是从10年开始,由于种种原因,终究没能好好研究一下.最近机缘巧合,可以 ...

  2. mysqldump: command not found问题解决

    首先得知道mysql命令或mysqldump命令的完整路径,可以使用find命令查找除非你知道mysql安装路径可以略过这一步. find / -name mysql -print 例如我的mysql ...

  3. dedecms 发布文章时,关键字会自动加内链

    在后台找到:核心->批量维护->文档关键词维护 把关键字和链接网址删掉就可以了,生成更新后前端页面就不会再链接了.>_<.

  4. jQuery 元素移除empty() remove()与detach()的区别?

    @1.empty() 删除匹配元素集合中所有的后代字节点元素: <p>hello<span>world</span></p> $("p&quo ...

  5. PHP_EOL常量

    PHP_EOL 换行符 unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 如: <?php echo PHP ...

  6. 2基本概念--python深度机器学习

    参考彭亮老师的视频教程:转载请注明出处及彭亮老师原创 视频教程: http://pan.baidu.com/s/1kVNe5EJ 基本概念:训练集,测试集,特征值,监督学习,非监督学习,半监督学习,分 ...

  7. 【算法】简单选择排序 O(n^2) 不稳定的 C语言

    简单选择排序 一.算法描述 假设序列中有N个元素: 第1趟找到第1到N个元素之间最小的一个,与第1个元素进行交换 第2趟找到第2到N个元素之间最小的一个,与第2个元素进行交换 第3趟找到第3到N个元素 ...

  8. Linux——搭建PHP开发环境第四步:composer

    原文链接:https://my.oschina.net/jiangbianwanghai/blog/473249 1.下载composer.phar [root#localhost opt]# cur ...

  9. VLAN间通信----实验

        方法1.增加物理线路     需求:PC0连接SW的F0/1,PC1连接SW的F0/2; SW创建VLAN10,VLAN20; PC0划到VLAN10; PC1划到VLAN20; 现要求借用路 ...

  10. Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?

    Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...