Android中目的地Intent的使用
一、什么是Intent?
Intent的中文意思是目的。在Android中也是“目的”的意思。就是我们要去哪里,从这个activity要前往另一个Activity就需要用到Intent。
示例代码一:
1: //定义一个Intent
2: Intent intent = new Intent(IntentDemo.this, AnotherActivity2.class);
3: //启动Activity
4: startActivity(intent);
以上示例代码的作用是从IntentDemo这个activity切换到AnotherActivity2。这是Intent其中一种构造方法,指定两个Activity。为什么需要指定两个活动呢?因为在Android中有一个活动栈,这样的构造方式才能确保正确的将前一个活动压入栈中,才能在触发返回键的时候活动能够正确出栈。
注意:所有的Activity都必须先在AndroidManifest.xml里面配置声明。一下为本文用到的程序配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.halzhang.android.intent" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IntentDemo" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity" android:label="another">
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<!-- category一定要配置,否则报错:找不到Activity -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity2" android:label="another2">
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<!--
上面配置的两个activity具有相同的action类型,都为“android.intent.action.EDIT”
当Intent的action属性为Intent.ACTION_EDIT时,系统不知道转向哪个Activity时,
就会弹出一个Dialog列出所有action为“android.intent.action.EDIT”的
Activity供用户选择
-->
</manifest>
二、Intent的构造函数
公共构造函数:
1、Intent() 空构造函数
2、Intent(Intent o) 拷贝构造函数
3、Intent(String action) 指定action类型的构造函数
4、Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider
5、Intent(Context packageContext, Class<?> cls) 传入组件的构造函数,也就是上文提到的
6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体
Intent有六种构造函数,3、4、5是最常用的,并不是其他没用!
Intent(String action, Uri uri) 的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。
示例代码二:
1: Intent intent = new Intent(Intent.ACTION_EDIT, null);
2: startActivity(intent);
示例代码二是用了第四种构造函数,只是uri参数为null。执行此代码的时候,系统就会在程序主配置文件AndroidMainfest.xml中寻找
<action android:name="android.intent.action.EDIT" />
对应的Activity,如果对应为多个activity具有
<action android:name="android.intent.action.EDIT" />
此时就会弹出一个dailog选择Activity,如下图:
如果是用示例代码一那种方式进行发送则不会有这种情况。
三、利用Intent在Activity之间传递数据
在Main中执行如下代码:
Bundle bundle = new Bundle();
bundle.putStringArray("NAMEARR", nameArr);
Intent intent = new Intent(Main.this, CountList.class);
intent.putExtras(bundle);
startActivity(intent);
在CountList中,代码如下:
1: Bundle bundle = this.getIntent().getExtras();
2: String[] arrName = bundle.getStringArray("NAMEARR");
以上代码就实现了Activity之间的数据传递!
Android中目的地Intent的使用的更多相关文章
- Android中的Intent Filter匹配规则介绍
本文主要介绍了隐式Intent匹配目标组件的规则,若有叙述不清晰或是不准确的地方希望大家指出,谢谢大家: ) 1. Intent简介 Intent用于在一个组件(Component,如Activity ...
- Android中的Intent详解
前言: 每个应用程序都有若干个Activity组成,每一个Activity都是一个应用程序与用户进行交互的窗口,呈现不同的交互界面.因为每一个Acticity的任务不一样,所以经常互在各个Activi ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
- Android中的intent属性
android之Intent的七大属性 2015年04月03日 ⁄ Android ⁄ 共 14866字 ⁄ 字号 小 中 大 ⁄ 1条评论 Intent用于封装程序的“调用意图”.两个Activit ...
- android中通过intent传递复杂数据
android中在各个service或者acitivity之间可以通过Intent来传递一些数据,intent原生直接提供了一些简单数据类型的数据的传递,使用起来也很方便,比如int boolean ...
- android中使用intent来实现Activity带数据跳转
大家都知道startActivity()是用来切换跳转Activity的.如果想要在另个Activity中出书数据的话.只需要在源activity中使用intent.putExtra()方法传出数据. ...
- Android 中的 Intent 简介
Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据. ------------------------------- ...
- 在Android中通过Intent使用Bundle传递对象
IntentBundle传递对象SerializableParcelable Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法.被传递的对象 ...
- Android中使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信
场景 点击拨打电话按钮,跳转到拨打电话页面 点击发送短信按钮,跳转到发送短信页面 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程 ...
随机推荐
- COCOS2D-X之帧动画的一种实现Demo
这个Demo主要是实现帧动画,建议游戏中少用帧动画.废话少说直接上代码. 一.我们直接在COCOS2D-X自带的HelloCpp的工程中添加代码即可.我们在初始化中添加如下代码并附上图片资源. CCS ...
- 监控apache虚拟主机进程
mod_status模块能输出例如以下内容: 正在伺服请求的工作者(线程或进程)数量 空暇的工作者数量 每一个工作者的状态:已完毕的请求数.已发送的字节数.(*) 总訪问数和已发送的字节总数(*) s ...
- HDU1849 Rabbit and Grass()
用异或看取得的值是否为0推断 思想换没搞懂 #include<stdio.h> int main() { int ans,n,a; while(scanf("%d",& ...
- 彻底卸载McAfee和Agent的方法
1.控制面板中-添加或删除程序中-删除"McAfee VirusScan Enterprise"和"McAfee AntiSpyware Enterprise Modul ...
- C++基础之---union联合体大小分析
#include <iostream> using namespace std; union un { int a[7]; double b; char c[10]; int d[3]; ...
- Api之Cors跨域以及其他跨域方式
Web Api之Cors跨域以及其他跨域方式(三) 我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...
- drupal 7 模块开发,hook_form
因为不是系统学习,只能把每天自己学习到的东西零碎的记录下来. 一来方便自己记忆,二来可供大家查阅. 后续有精力再去做进一步的整理. 1 开发一个模块分为有下面几个文件 hook.admin.inc h ...
- phpStorm打开提示 failed to create JVM 的解决的方法
phpStorm 软件打开执行提示 failed to create JVM的解决的方法. 改动文件 D:\Program Files (x86)\JetBrains\PhpStorm 7.1.3\b ...
- nginx & flup & django & python3.x @ window7配置备忘录
最近考虑原Prism建筑(非职业.半专业人士认为C/S建筑)至B/S迁移,主要是由于部署问题,包括两个因素:已经做,虽然一键安装和部署的一个因素,心存顾虑,虽然我一再声明这是一个绿色软件.还有一个因素 ...
- 位运算之 C 与或非异或
与运算:& 两者都为1为1,否则为0 1&1=1, 1&0=0, 0&1=0, 0&0=0 或运算:| 两者都为0为0,否则为1 1|1 = 1, ...