一.Intent简介

  Intent中文是“意图,意向”,它是Android中四大组件通讯的纽带,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。

二.启动组件的方法

三.Intent的属性(7个)

  

component(组件):目的组件----Action(动作):用来表现意图的行动----category(类别):用来表现动作的类别----data(数据):表示与动作要操纵的数据

type(数据类型):对于data范例的描写----extras(扩展信息):扩展信息----flags(标志位):期望这个意图的运行模式

三.代码实现

(1)ComponentActivity.class

  

/**
* 直接查找法(通过组件名称)
*/
public class ComponentActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
} /**
* 直接查找法(通过组件名称)
* @param view
*/
public void componetClick(View view){
/*Intent intent = new Intent();
ComponentName componentName = new ComponentName(this,IntentActivity.class);
intent.setComponent(componentName);*/
Intent intent = new Intent(this,IntentActivity.class);
startActivity(intent);
} /**
* 使用间接法(通过action属性和category属性)
* @param view
* 在Activity的清单配置文件中,必须使用默认的类别
* android.intent.category.DEFAULT
*/
public void actionClick(View view){
/*Intent intent = new Intent();
intent.setAction("com.langdon.action.MY_ACTION");*/
Intent intent = new Intent("com.langdon.action.MY_ACTION");
intent.addCategory("com.langdon.category.MY_CATEGORY");
startActivity(intent);
} /**
* data属性,一般与action配合使用(最常见的一种使用方法)
* type属性,表示数据的类型
* @param view
*/
public void dataClick(View view){
Intent intent = new Intent();
//intent.setAction("com.langdon.action.MY_ACTION");
intent.setAction(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://www.baidu.com"); //intent.setData(data);//会把type属性设置为null
//intent.setType("text/html");//会把data属性设置为null
//要使用type和data,必须使用以下方法,匹配时必须两者同时匹配才能通过
intent.setDataAndType(data,"text/html");
startActivity(intent);
} /**
* 代码实现Activity的启动模式
* @param view
*/
public void flagClick(View view){
Intent intent = new Intent(this,FlagActivity.class);
//设置Activity的启动模式
//Intent.FLAG_ACTIVITY_NEW_TASK ---在新的任务中启动Activity,如果有,那么在本任务中启动
//Intent.FLAG_ACTIVITY_CLEAR_TASK ---相当于SingleTask
//Intent.FLAG_ACTIVITY_CLEAR_TOP ---相当于SingleTop
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }

  

(2)activity_component.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_componer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.langdon.taiyang.androidtest.Intent.ComponentActivity"> <Button
android:id="@+id/bt_component"
android:text="通过component组件名查找"
android:onClick="componetClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_action"
android:layout_below="@+id/bt_component"
android:text="通过action组件名查找"
android:onClick="actionClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_data"
android:layout_below="@+id/bt_action"
android:text="通过Data组件名查找"
android:onClick="dataClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_flag"
android:layout_below="@+id/bt_data"
android:text="flag启动模式"
android:onClick="flagClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

  

(3)AndroidManifest.xml(IntentActivity---NextActivity---DataActivity都只含有一个TextView,用来在ComponentActivity.class点击按钮显示不同的文字)

 <activity android:name=".Intent.ComponentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Intent.IntentActivity">
<intent-filter android:priority="-1">
<action android:name="com.langdon.action.MY_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Intent.NextActivity">
<intent-filter android:priority="2">
<action android:name="com.langdon.action.MY_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.langdon.category.MY_CATEGORY" />
</intent-filter>
</activity>
<activity android:name=".Intent.DataActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:host="www.baidu.com" android:mimeType="text/html"
android:scheme="http" />
</intent-filter>
</activity>
<activity android:name=".Intent.FlagActivity" />

  

运行结果如下:

        图1

       图2

        图3

其中图3是点击第三个按钮显示的,其余三个按钮点击后会有一个如图1的textView提

Intent系列讲解---Intent简介以及相关属性的更多相关文章

  1. 【转】Android总结篇系列:Activity Intent Flags及Task相关属性

    [转]Android总结篇系列:Activity Intent Flags及Task相关属性 同上文一样,本文主要引用自网上现有博文,并加上一些自己的理解,在此感谢原作者. 原文地址: http:// ...

  2. Android总结篇系列:Activity Intent Flags及Task相关属性

    同上文一样,本文主要引用自网上现有博文,并加上一些自己的理解,在此感谢原作者. 原文地址: http://blog.csdn.net/liuhe688/article/details/6761337 ...

  3. Activity Intent Flags及Task相关属性

    转自http://www.cnblogs.com/lwbqqyumidi/p/3775479.html 今天我们来讲一下Activity的task相关内容. 上次我们讲到Activity的四种启动模式 ...

  4. 我的Android 4 学习系列之Intent 和 Broadcast Reciever

    目录 Intent 简介 使用隐式和显式Intent启动Activity.子Acitivity和Service 使用Linkify 使用Broadcast Intent 广播事件 使用 Pending ...

  5. 最全面的Android Intent机制讲解

    对于大型软件开发经验较少的程序员来说,这可能是一个不太容易理解的抽象概念,因为它与我们平常使用的简单函数调用,或者通过库调用接口的方式不太一样.在 Intent 的使用中你看不到直接的函数调用,相对函 ...

  6. Intent的Component,Action和Category属性详解-android学习之旅(五十)

    Component属性 代码示例 public class MainActivity extends Activity{ @Override protected void onCreate(Bundl ...

  7. Android UI系列-----RelativeLayout的相关属性

    本篇随笔将主要记录一些RelatieLayout的相关属性,并将猜拳游戏通过RelativeLayout实现出来 RelativeLayout的几组属性 第一组属性:android:layout_be ...

  8. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  9. AndroidManifest.xml中<activity></activity>相关属性说明

    虽说,从事android开发有一定时间了,对于Activity大家也都不陌生,但是具体的对于Activity中相关属性的含义有必要做一个系统的总结: intent-filteraction: 来指定响 ...

随机推荐

  1. oschina服务器软件

    服务器软件 74Apache模块 54Nginx扩展模块 13Radius相关 94PaaS 系统 29服务发现/注册和协调 17Docker 扩展 7Docker 映像 83应用服务器 189HTT ...

  2. 源代码管理(Windows + VisualSVN Server + TortoiseSVN + VS2010)

    之前项目中使用过SVN管理源代码,但是都是链接别人搭建的服务器,现在感觉每周保留一个版本蛮麻烦的,就搭建一个,方便以后管理代码. 1.安装VisualSVN Server( VisualSVN Ser ...

  3. DELPHI7如何调用带参数的JAVA WebService

    对方提供的WebService地址是http://192.168.1.6:8080/test/pic?XH=XX用DELPHI如何调呢 ------解决方案--------------------通过 ...

  4. MySQL函数简介

    //将时间戳长整形数值转换为yyyy-MM-dd HH:mm:ss格式SELECT FROM_UNIXTIME(CREATE_TIME, '%Y-%m-%d %H:%i:%S') FROM TBLS ...

  5. Linux + Apache + MySql+ Php 配置虚拟主机

    win7:------------------------------------------------------------------------ NameVirtualHost *:80&l ...

  6. DB2高级安装

     学习从安装开始,哈哈.这里记下  Windows 和 UNIX/Linux安装DB2的各种方式,不同,及具体的一些细节.    Windows下向导化安装:            setup.exe ...

  7. I/O多路转接 --- UNIX环境高级编程

    I/O多路转接技术:先构造一张有关描述符的列表,然后调用一个函数,知道这些描述符中的一个已准备好进行I/O时,给函数才返回.在返回时,它告诉进程哪些描述符已准备好可以进行I/O. poll.selec ...

  8. muduo源代码分析--我对muduo的理解

    分为几个模块 EventLoop.TcpServer.Acceptor.TcpConnection.Channel等 对于EventLoop来说: 他仅仅关注里面的主驱动力,EventLoop中仅仅关 ...

  9. C#,.net获取字符串中指定字符串的个数、所在位置与替换字符串

    方法一: public static int indexOf (字符串/字符,int从第几位开始,int共查几位) string tests = "1absjjkcbfka2rsbcfak2 ...

  10. iis配置出现的问题及解决

    唯一密钥属性“value”设置…无法添加类型为add 在配置IIS7.5时,会出现 在唯一密钥属性“value”设置为“default.aspx”(或者index.asp等)时,无法添加类型为“add ...