Intent简介:                                                                               

在一个Android应用中,主要由四种组件组成(四种组件分别为:Activity、Broadcast、Service、ContentProvider),而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。在这些组件之间的通讯中,主要是由Intent协助完成的。

Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。

   Intent来实现Activity之间的跳转:                                           

1.无数据的简单跳转

通常在button的setOnClickListener中,通过构造一个新的intent实例,然后通过startActivity(Intent实例)来实现:

open.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this, FileBrowser.class);
                startActivity(intent);
            }
        });

  为什么需要指定两个活动呢?因为在Android中有一个活动栈,这样的构造方式才能确保正确的将前一个活动压入栈中,才能在触发返回键的时候活动能够正确出栈。

2.所有Activity都必须在AndroidManifest.xml中声明:

<activity android:name="SecondActivity"></activity>

  

3.Intent常用的构造函数:

A.Intent(String action) 指定action类型的构造函数

B.Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

C.Intent(Context packageContext, Class<?> cls) 传入组件的构造函数(示例一中提到的Activity之间的转换)

D.Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

Intent(String action, Uri uri)  的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。

1
2
Intent intent = new Intent(Intent.ACTION_EDIT, null);
  startActivity(intent);

  该示例所使用的是构造函数B,Intent.ACTION_EDIT表示的是一个Action,执行此代码的时候,程序就会在AndroidManifest.xml中寻找相应的URI下所属的Activity;

<action android:name="android.intent.action.EDIT" />对应的Activity,如果对应为多个activity具有<action android:name="android.intent.action.EDIT" />此时就会弹出一个dailog选择Activity,如下图:

 如果是用示例代码一那种方式进行发送则不会有这种情况。

   Activity之间数据的传递:                                                         

首先需要使用到的是Bundle

       String path = file.getAbsolutePath();
String name = file.getName();
Intent intent = new Intent(FileBrowser.this,MainActivity.class);
Bundle bundle=new Bundle();
bundle.putString("path",path );
bundle.putString("name", name);
intent.putExtras(bundle);      
startActivity(intent);

首先,示例中创建了一个Intent的实例,用于转换Activity,同时,从该Activity中的变量获取了需要传递的数据;

其次是bundle.putXXX()方法的调用,向bundle中填充数据

void putInt(String key, int value)

Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.
void putIntArray(String key, int[] value)

Inserts an int array value into the mapping of this Bundle, replacing any existing value for the given key.
void putLong(String key, long value)

Inserts a long value into the mapping of this Bundle, replacing any existing value for the given key.
void putBoolean(String key, boolean value)

Inserts a Boolean value into the mapping of this Bundle, replacing any existing value for the given key.
void putBooleanArray(String key, boolean[] value)

Inserts a boolean array value into the mapping of this Bundle, replacing any existing value for the given key.
void putAll(Bundle map)

Inserts all mappings from the given Bundle into this Bundle.
void putString(String key, String value)

Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key.
void putStringArray(String key, String[] value)

Inserts a String array value into the mapping of this Bundle, replacing any existing value for the given key.
void putStringArrayList(String key, ArrayList<String> value)

Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

然后调用intent.putExtras(bundle),将bundle绑定在intent上,最后startActivity();

数据的接收:

1
2
3
4
5
6
7
Bundle bd = intent.getExtras();
        String str_path = bd.getString("path");
        String str_name = bd.getString("name");
        text = (TextView)findViewById(R.id.tv_address);
        name = (TextView)findViewById(R.id.tv_name);
        text.setText(str_path);
        name.setText(str_name);

转载:   http://www.cnblogs.com/VortexPiggy/archive/2012/05/25/2509465.html

37.Activity之间的转换以及数据的传递(Intent)学习的更多相关文章

  1. android第一行代码-3.activity之间的调用跟数据传递

    前面两节所有应用都是同一个activity中的,是时候讲activity之间交互的操作了,此后会涉及到intent这个概念,这也算一个新的里程碑开始. 主要内容包括intent的使用,以及activi ...

  2. 多个Activity之间的切换与数据交互

    总结 两个activity之间切换我概括的分为两步: 1. 代码实现切换操作.2.配置中声明另外一个acitivity! 1. 代码实现切换操作 显示定义一个intent 对象,Intent 这个类的 ...

  3. 完成的设备扫描项目的几个关键程序,包括activity之间的转换

    module 的 gradle.build最后三行的compile 是关键dependencies { implementation fileTree(dir: 'libs', include: [' ...

  4. Android学习之Activity之间的数据传递

    Activity与Activity之间很多情况下都需要进行数据的传递,下面就用几个简单的例子来看一下. (一).一个Activity启动另一个Activity并将数据传递到这个Activity当中 思 ...

  5. 建立、配置和使用Activity——使用Bundle在Activity之间交换数据

    当一个Activity启动另一个Activity时,常常会有一些数据需要传过去——这就像Web应用从一个Servlet跳到另一个Serlvet时,Web应用习惯把需要交换的数据放入requestSco ...

  6. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  7. Android笔记(二十) Activity中的跳转和值传递

    我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...

  8. 转-Activity之间数据传递之Intent数据传递

    Intent意图 可用于Activity之间的数据传递,一般可分为下面两种情况,从当前Activity传递到目标Activity后有无返回值: 1.传递后无返回值的情况: 1 2 3 4 5 6 7 ...

  9. 【Android 复习】 : Activity之间传递数据的几种方式

    在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...

随机推荐

  1. Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA

    题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路 分析:先求一次最短路,铁路 ...

  2. 2014 Super Training #3 H Tmutarakan Exams --容斥原理

    原题: URAL 1091  http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...

  3. Z路径覆盖

    Z路径覆盖是路径覆盖的一个变体.路径覆盖是白盒测试最为典型的问题.着眼于路径分析的测试可称为路径测试.完成路径测试的理想情况是做到路径覆盖.对于比较简单的小程序实现路径覆盖是可能做到的.但是如果程序中 ...

  4. php file_get_contents 绕过

    http://www.shiyanbar.com/ctf/1837 想到了经常出现的残留文件问题,于是尝试了一下:index.php~,index.php.bak, $flag='xxx';extra ...

  5. Linux下CGroup使用说明梳理

    CGroup 介绍CGroup 是 Control Groups 的缩写,是 Linux 内核提供的一种可以限制.记录.隔离进程组 (process groups) 所使用的物力资源 (如 cpu m ...

  6. jquery.validate运用和扩展

    一.运用 默认校验规则 ().required:true 必输字段 ().remote:"remote-valid.jsp" 使用ajax方法调用remote-valid.jsp验 ...

  7. log4net 日志跟踪程序

    Log4net 是 Apache 下一个开放源码的项目,它是Log4j 的一个克隆版.我们可以控制日志信息的输出目的地.Log4net中定义了多种日志信息输出模式. 在做项目的时候令我最头疼的是在程序 ...

  8. 我的WCF摸爬滚打之路(1)

    等了好久终于等到今天!盼了好久终于把梦实现……哈哈,仅以此歌词来庆祝我为期3天的wcf学习之路圆满结束. 今天写这个文章的目的在于记录一下我自己在学习WCF的时候碰到的一些问题,俗话说,好记心不如烂笔 ...

  9. 装系统提示缺少所需的CD/DVD驱动器设备驱动程序

    昨晚用ultraISO和win7 旗舰版(ultimate)的镜像做了个启动U盘,插在自己新电脑上安装过程中提示“缺少所需的CD/DVD驱动器设备驱动程序”,用网上的很多办法都不行,最后找官网的客服问 ...

  10. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...