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

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5.  
  6. <Button
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:onClick="toMain"
  10. android:text="显示Intent 启动自己"
  11. android:textAllCaps="false" />
  12.  
  13. <Button
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:onClick="setComponent"
  17. android:text="启动别包下的activity"
  18. android:textAllCaps="false" />
  19.  
  20. <Button
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:onClick="action"
  24. android:text="ActionViewActivity响应此action"
  25. android:textAllCaps="false" />
  26.  
  27. <Button
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:onClick="web"
  31. android:text="浏览网页"
  32. android:textAllCaps="false" />
  33.  
  34. <Button
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:onClick="call1"
  38. android:text="系统拨号界面"
  39. android:textAllCaps="false" />
  40.  
  41. <Button
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:onClick="call2"
  45. android:text="拨号"
  46. android:textAllCaps="false" />
  47.  
  48. </LinearLayout>

activity_main.xml

  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. }
  8.  
  9. public void toMain(View v) {
  10. Intent intent = new Intent(this, MainActivity.class);
  11. startActivity(intent);
  12. }
  13.  
  14. public void setComponent(View v) {
  15. //首先创建一个空参的Intent对象
  16. Intent intent = new Intent();
  17. //Component(包名,完整的类名)
  18. ComponentName componentName = new ComponentName("com.example.lesson9_activitylaunchmode", "com.example.lesson9_activitylaunchmode.MainActivity");
  19. //设置目标组件
  20. intent.setComponent(componentName);
  21. startActivity(intent);
  22.  
  23. }
  24.  
  25. public void action(View v) {
  26. Intent intent = new Intent(Intent.ACTION_VIEW);
  27. //这里可能找不到能够响应这个ACTION_VIEW的目标组件,会报ActivityNotFound异常。所以可以做try-catch
  28. startActivity(intent);
  29. //创建一个ActionViewActivity活动,并注册,指定能响应ACTION_VIEW
  30. }
  31.  
  32. public void web(View v) {
  33. Intent intent = new Intent(Intent.ACTION_VIEW);
  34. intent.setData(Uri.parse("http://www.baidu.com"));
  35. startActivity(intent);
  36. }
  37.  
  38. //系统拨号界面,报错
  39. public void call1(View v) {
  40. Intent intent = new Intent(Intent.ACTION_DIAL);
  41. intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");
  42. startActivity(intent);
  43. }
  44.  
  45. //拨号界面
  46. public void call2(View v) {
  47. Uri uri = Uri.parse("tel:18822818871");
  48. Intent intent = new Intent(Intent.ACTION_DIAL, uri);
  49. startActivity(intent);
  50. }
  51. }

MainActivity.java

  1. public class ActionViewActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(@Nullable Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. TextView tv = new TextView(this);
  6. tv.setText("这是这是ActionViewActivity");
  7. setContentView(tv);
  8. }
  9. }

ActionViewActivity .java

  1. <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
  2. android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
  3. <activity android:name=".MainActivity">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6.  
  7. <category android:name="android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </activity>
  10. <activity android:name=".ActionViewActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.VIEW"/>
  13. <category android:name="android.intent.category.DEFAULT"/>
  14. </intent-filter>
  15.  
  16. </activity>
  17. </application>

AndroidManifest.xml

  1.  

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. linux常用命令(查看某些软件是否已安装)

    查看imap是否已安装 rpm -qa | grep imap 以下为未安装的情形: 检查是否已安装sendmail: rpm -qa | grep sendmail 以下为已安装的返回:

  2. Mac使用rz、sz远程上传下载文件

    习惯了 在windows下的securecrt和xshell的rz 和sz. rz 可以很方便的从客户端传文件到服务器,sz也可以很方便的从服务器传文件到客户端,就算中间隔着跳板机也不影响.在mac下 ...

  3. jquery的select元素和option的相关操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 关于javascript输出中文乱码的问题

    今天找到一个引导效果.原来是用英文进行引导.但是我改了里面的英文为汉字就出现乱码的情况.英文提示是在js页面里面完成的.所以最后的解决办法 就是把js文件用记事本打开,然后把文件另存为utf-8的格式 ...

  5. js实现placeholder效果

    <form name="testForm" action="" method=""> <input type=" ...

  6. php框架练习

    框架的作用: 功能:操作数据库 模版引擎smarty 分析,应该具备什么功能? a:具体配置文件 ,配置文件的读取功能 b:数据库处理类 c:目录结构 先建一个includes文件放入,配置文件(co ...

  7. PHP 用户注册

    注册页面 reg.html 负责收集用户填写的注册信息.教程里只列出关键的代码片段,完整的代码附在本节最后. 注册表单 <fieldset> <legend>用户注册</ ...

  8. angular post发送请求和GET发送请求,服务器端接收不到信息的问题

    参数可能因为编码原因,服务器端无法接收到传递的值, 这时需要用到补丁来解决这个问题 1,下载一个http.patch.js文件,放入YII框架中的js/ng文件架内 2angularjs 创建模型部分 ...

  9. python自动开发之(ajax)第二十天

    1.Django请求的生命周期 路由系统 -> 试图函数(获取模板+数据=>渲染) -> 字符串返回给用户 2.路由系统 /index/ -> 函数或类.as_view() / ...

  10. DbUtils组件

    DbUtils组件 DbUtils组件, 1. 简化jdbc操作 2. 下载组件,引入jar文件 : commons-dbutils-1.6.jar |-- DbUtils 关闭资源.加载驱动 |-- ...